Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
78 changes: 69 additions & 9 deletions inc/wc-rest-product-controller-sync.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
/**
* Disables product synchronization hooks in WooCommerce Multilingual
* to ensure only API-sent data is processed.
* Disables the automatic synchronization of products and variations in
* WooCommerce Multilingual and enforces the link between products and
* variations through the translation_of parameter.
*/

namespace Woocommerce_Multilingual_Api_Sync;
Expand All @@ -10,20 +11,79 @@
exit;
}

add_action( 'rest_api_init', __NAMESPACE__ . '\\disable_product_insert_hook', 20 );
add_action( 'rest_api_init', __NAMESPACE__ . '\\disable_product_insert_hook', 20);

function disable_product_insert_hook() {
global $wp_filter;

$hook_name = 'woocommerce_rest_insert_product_object';
$hook_map = [
'woocommerce_rest_insert_product_object' => '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
]
);
}
}
}
}
2 changes: 1 addition & 1 deletion woocommerce-multilingual-api-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down