| 1 |
<?php |
| 2 |
/** |
| 3 |
* Description of WpmlController |
| 4 |
* |
| 5 |
* @author Ali2Woo Team |
| 6 |
* |
| 7 |
* @autoload: a2wl_admin_init |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace AliNext_Lite;; |
| 11 |
|
| 12 |
class WpmlController { |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
add_action('wcml_after_sync_product_data', array($this, 'sync_product_data'), 100, 3); |
| 16 |
} |
| 17 |
|
| 18 |
/* This function will be called after the product has been transferred. |
| 19 |
* |
| 20 |
* In this place need to build attributes values and update swatches IDs, and do other cool stuff |
| 21 |
*/ |
| 22 |
public function sync_product_data($or_product_id, $tr_product_id, $language){ |
| 23 |
global $wpdb; |
| 24 |
|
| 25 |
// Build attributes values and update swatches IDs |
| 26 |
|
| 27 |
$or_product_attributes = get_post_meta($tr_product_id, '_product_attributes',true); |
| 28 |
$var_attrs = array(); |
| 29 |
if ($or_product_attributes) { |
| 30 |
foreach($or_product_attributes as $key=>$val) { |
| 31 |
if ($val['is_variation']){ |
| 32 |
$var_attrs[$key] = array(); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if (!empty($var_attrs)) { |
| 38 |
// Collect attributes value from original product variations |
| 39 |
$arrts_str = implode(",", array_map( |
| 40 |
function($v) { |
| 41 |
return "'attribute_$v'"; |
| 42 |
}, |
| 43 |
array_keys($var_attrs)) |
| 44 |
); |
| 45 |
|
| 46 |
$sql = $wpdb->prepare( |
| 47 |
"SELECT DISTINCT pm.meta_key, pm.meta_value FROM {$wpdb->posts} p " . |
| 48 |
"LEFT JOIN {$wpdb->postmeta} pm ON (p.ID = pm.post_id) " . |
| 49 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 50 |
"WHERE post_parent=%d AND meta_key IN ($arrts_str)", $or_product_id); |
| 51 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 52 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 53 |
foreach($results as $res) { |
| 54 |
$attr_name = substr($res['meta_key'], strlen('attribute_')); |
| 55 |
$var_attrs[$attr_name][] = $res['meta_value']; |
| 56 |
} |
| 57 |
|
| 58 |
$tr_swatch = get_post_meta($tr_product_id, '_swatch_type_options',true); |
| 59 |
|
| 60 |
// Replace default lang swatches ID to translated swatches ID |
| 61 |
foreach ($var_attrs as $attr_slug=>$values) { |
| 62 |
$attr_id = md5($attr_slug); |
| 63 |
foreach ($values as $val) { |
| 64 |
$val_id = md5($val); |
| 65 |
$new_val_id = md5($val.'-'.$language); |
| 66 |
if (isset($tr_swatch[$attr_id]['attributes'][$val_id])) { |
| 67 |
$tr_swatch[$attr_id]['attributes'][$new_val_id] = $tr_swatch[$attr_id]['attributes'][$val_id]; |
| 68 |
unset($tr_swatch[$attr_id]['attributes'][$val_id]); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
update_post_meta($tr_product_id, '_swatch_type_options', $tr_swatch); |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
|