$translated_swatch_options = $this->translate_custom_attribute_value( $attribute_key, $attribute_value, $attribute_value_md5, $attribute_name_hash, $swatch_options, $translated_swatch_options, $original_product_id, $translated_product_id );
class WCML_Variation_Swatches_And_Photos {
private $woocommerce_wpml;
public function __construct( woocommerce_wpml $woocommerce_wpml ) {
$this->woocommerce_wpml = $woocommerce_wpml;
public function add_hooks() {
add_action( 'wcml_after_sync_product_data', [ $this, 'sync_variation_swatches_and_photos' ], 10, 3 );
public function sync_variation_swatches_and_photos( $original_product_id, $translated_product_id, $language ) {
$swatch_options = maybe_unserialize( get_post_meta( $original_product_id, '_swatch_type_options', true ) );
$translated_swatch_options = $swatch_options;
$original_product_attributes = $this->woocommerce_wpml->attributes->get_product_attributes( $original_product_id );
wpml_collect( $original_product_attributes )->each(
function ( $attribute, $attribute_key ) use ( $swatch_options, &$translated_swatch_options, $language, $original_product_id, $translated_product_id ) {
$attribute_name_hash = md5( sanitize_title( $attribute['name'] ) );
if ( $this->woocommerce_wpml->attributes->is_a_taxonomy( $attribute ) ) {
$translated_swatch_options = $this->translate_taxonomy_attributes( $attribute_key, $attribute_name_hash, $swatch_options, $translated_swatch_options, $language );
$translated_swatch_options = $this->translate_custom_attributes( $attribute_key, $attribute, $attribute_name_hash, $swatch_options, $translated_swatch_options, $original_product_id, $translated_product_id );
update_post_meta( $translated_product_id, '_swatch_type_options', $translated_swatch_options );
private function translate_taxonomy_attributes( $taxonomy, $attribute_name_hash, $swatch_options, $translated_swatch_options, $language ) {
$attribute_terms = get_terms( [ 'taxonomy' => $taxonomy ] );
wpml_collect( $attribute_terms )->each(
function ( $term ) use ( $taxonomy, $attribute_name_hash, $swatch_options, &$translated_swatch_options, $language ) {
$attribute_term_slug_md5 = md5( $term->slug );
$translated_swatch_options = $this->translate_taxonomy_term( $term, $attribute_term_slug_md5, $taxonomy, $attribute_name_hash, $swatch_options, $translated_swatch_options, $language );
return $translated_swatch_options;
private function translate_taxonomy_term( $term, $attribute_term_slug_md5, $taxonomy, $attribute_name_hash, $swatch_options, $translated_swatch_options, $language ) {