diff --git a/README.md b/README.md index c499651..6318aaa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # WooCommerce Multilingual API Sync -**Version:** 1.0.1 +**Version:** 1.0.2 **Author:** NuoBiT Solutions, S.L. **License:** GPLv3 or later @@ -11,7 +11,7 @@ WooCommerce Multilingual API Sync ensures seamless integration between the WooCo ## Core Functionality - **Translatable Attributes**: Automatically marks product attributes added via WooCommerce REST API as translatable in WooCommerce Multilingual. -- **Product Synchronization Control**: Disables product synchronization hooks in WooCommerce Multilingual to ensure only data sent through the REST API is processed. +- **Product Synchronization Control**: Disables the automatic synchronization of products and variations in WooCommerce Multilingual and enforces the link between products and variations through the translation_of parameter. ## Use Cases diff --git a/inc/wc-rest-product-controller-sync.php b/inc/wc-rest-product-controller-sync.php index 579e641..b3131e0 100644 --- a/inc/wc-rest-product-controller-sync.php +++ b/inc/wc-rest-product-controller-sync.php @@ -1,7 +1,8 @@ 'post_product', + 'woocommerce_rest_insert_product_variation_object' => 'post_product_variation', + ]; - if ( isset( $wp_filter[ $hook_name ] ) ) { - foreach ( $wp_filter[ $hook_name ]->callbacks as $priority => $callbacks ) { - foreach ( $callbacks as $key => $callback ) { - if ( is_array( $callback['function'] ) && $callback['function'][1] === 'insert' ) { - remove_action( $hook_name, $callback['function'], $priority ); + foreach ( $hook_map as $hook_name => $element_type ) { + if ( isset( $wp_filter[ $hook_name ] ) ) { + foreach ( $wp_filter[ $hook_name ]->callbacks as $priority => $callbacks ) { + foreach ( $callbacks as $key => $callback ) { + if ( is_array( $callback['function'] ) && $callback['function'][1] === 'insert' ) { + remove_action( $hook_name, $callback['function'], $priority ); + } } } } + + add_action( $hook_name, function( $object, $request, $creating ) use ( $element_type ) { + insert_product_translation( $object, $request, $creating, $element_type ); + }, 10, 3 ); + } +} + +function insert_product_translation( $object, $request, $creating, $element_type ) { + $params = $request->get_params(); + + $lang_code = isset( $params['lang'] ) ? $params['lang'] : null; + $translation_of = isset( $params['translation_of'] ) ? (int) $params['translation_of'] : null; + + if ( $lang_code && $translation_of ) { + global $wpdb; + + $row = $wpdb->get_row( $wpdb->prepare( + "SELECT trid, language_code + FROM {$wpdb->prefix}icl_translations + WHERE element_id = %d + AND element_type = %s", + $translation_of, + $element_type + )); + + if ( $row ) { + $trid = $row->trid; + $source_lang = $row->language_code; + $new_id = $object->get_id(); + + $existing = $wpdb->get_var( $wpdb->prepare( + "SELECT translation_id + FROM {$wpdb->prefix}icl_translations + WHERE element_id = %d + AND element_type = %s", + $new_id, + $element_type + )); + + if ( $existing ) { + $wpdb->update( + "{$wpdb->prefix}icl_translations", + [ + 'trid' => $trid, + 'language_code' => $lang_code, + 'source_language_code' => $source_lang + ], + [ + 'element_id' => $new_id, + 'element_type' => $element_type + ] + ); + } + } } } diff --git a/woocommerce-multilingual-api-sync.php b/woocommerce-multilingual-api-sync.php index 6a61aa7..ea2a47d 100644 --- a/woocommerce-multilingual-api-sync.php +++ b/woocommerce-multilingual-api-sync.php @@ -2,7 +2,7 @@ /** * Plugin Name: WooCommerce Multilingual API Sync * Description: Manages pending synchronization processes in WooCommerce Multilingual. - * Version: 1.0.1 + * Version: 1.0.2 * Author: NuoBiT Solutions S.L. * Author URI: https://www.nuobit.com */