Quote from admin on November 3, 2024, 12:56
Hello
Here is one idea: you can use hook 'woobe_before_update_product_field' described here https://bulk-editor.com/document/more-settings-implicit on the main site. In to file functions.php of your main site add next code:
add_filter('woobe_before_update_product_field', function($value, $product_id, $field_key) {
// Data to send
$product_data = [
'id' => $product_id,
'field_key' => $field_key,
'value' => $value,
];
// URLs for five sites
$site_urls = [
'https://site1.com/wp-json/custom-endpoint/v1/update-product',
'https://site2.com/wp-json/custom-endpoint/v1/update-product',
'https://site3.com/wp-json/custom-endpoint/v1/update-product',
'https://site4.com/wp-json/custom-endpoint/v1/update-product',
'https://site5.com/wp-json/custom-endpoint/v1/update-product'
];
// REST API keys for each site (can be created in WooCommerce > Settings > Advanced > REST API)
$api_keys = [
'consumer_key' => 'ck_your_consumer_key_here',
'consumer_secret' => 'cs_your_consumer_secret_here'
];
// Sending data to each site
foreach ($site_urls as $url) {
wp_remote_post($url, [
'method' => 'POST',
'headers' => [
'Authorization' => 'Basic ' . base64_encode($api_keys['consumer_key'] . ':' . $api_keys['consumer_secret']),
'Content-Type' => 'application/json',
],
'body' => json_encode($product_data),
]);
}
return $value;
}, 10, 3);
On each receiving site, you need to register a REST API endpoint to process the received data and update products. Add this code to the functions.php file or a separate plugin on each of the five sites.
add_action('rest_api_init', function() {
register_rest_route('custom-endpoint/v1', '/update-product', [
'methods' => 'POST',
'callback' => 'update_product_data',
'permission_callback' => function() {
return current_user_can('edit_products'); // Allows access only to users who can edit products
},
]);
});
function update_product_data($request) {
$params = $request->get_json_params();
$product_id = isset($params['id']) ? intval($params['id']) : 0;
$field_key = isset($params['field_key']) ? sanitize_text_field($params['field_key']) : '';
$value = isset($params['value']) ? sanitize_text_field($params['value']) : '';
if (!$product_id || !$field_key) {
return new WP_Error('missing_data', 'Product ID or Field Key is missing.', ['status' => 400]);
}
// Update the desired product field depending on field_key
switch ($field_key) {
case 'title':
wp_update_post([
'ID' => $product_id,
'post_title' => $value,
]);
break;
case 'short_description':
wp_update_post([
'ID' => $product_id,
'post_excerpt' => $value,
]);
break;
case 'description':
wp_update_post([
'ID' => $product_id,
'post_content' => $value,
]);
break;
default:
// Updating a meta field for custom data
update_post_meta($product_id, $field_key, $value);
break;
}
return new WP_REST_Response('Product updated successfully', 200);
}
We not tested the code, but this idea should work :)
Hello
Here is one idea: you can use hook 'woobe_before_update_product_field' described here https://bulk-editor.com/document/more-settings-implicit on the main site. In to file functions.php of your main site add next code:
add_filter('woobe_before_update_product_field', function($value, $product_id, $field_key) {
// Data to send
$product_data = [
'id' => $product_id,
'field_key' => $field_key,
'value' => $value,
];
// URLs for five sites
$site_urls = [
'https://site1.com/wp-json/custom-endpoint/v1/update-product',
'https://site2.com/wp-json/custom-endpoint/v1/update-product',
'https://site3.com/wp-json/custom-endpoint/v1/update-product',
'https://site4.com/wp-json/custom-endpoint/v1/update-product',
'https://site5.com/wp-json/custom-endpoint/v1/update-product'
];
// REST API keys for each site (can be created in WooCommerce > Settings > Advanced > REST API)
$api_keys = [
'consumer_key' => 'ck_your_consumer_key_here',
'consumer_secret' => 'cs_your_consumer_secret_here'
];
// Sending data to each site
foreach ($site_urls as $url) {
wp_remote_post($url, [
'method' => 'POST',
'headers' => [
'Authorization' => 'Basic ' . base64_encode($api_keys['consumer_key'] . ':' . $api_keys['consumer_secret']),
'Content-Type' => 'application/json',
],
'body' => json_encode($product_data),
]);
}
return $value;
}, 10, 3);
On each receiving site, you need to register a REST API endpoint to process the received data and update products. Add this code to the functions.php file or a separate plugin on each of the five sites.
add_action('rest_api_init', function() {
register_rest_route('custom-endpoint/v1', '/update-product', [
'methods' => 'POST',
'callback' => 'update_product_data',
'permission_callback' => function() {
return current_user_can('edit_products'); // Allows access only to users who can edit products
},
]);
});
function update_product_data($request) {
$params = $request->get_json_params();
$product_id = isset($params['id']) ? intval($params['id']) : 0;
$field_key = isset($params['field_key']) ? sanitize_text_field($params['field_key']) : '';
$value = isset($params['value']) ? sanitize_text_field($params['value']) : '';
if (!$product_id || !$field_key) {
return new WP_Error('missing_data', 'Product ID or Field Key is missing.', ['status' => 400]);
}
// Update the desired product field depending on field_key
switch ($field_key) {
case 'title':
wp_update_post([
'ID' => $product_id,
'post_title' => $value,
]);
break;
case 'short_description':
wp_update_post([
'ID' => $product_id,
'post_excerpt' => $value,
]);
break;
case 'description':
wp_update_post([
'ID' => $product_id,
'post_content' => $value,
]);
break;
default:
// Updating a meta field for custom data
update_post_meta($product_id, $field_key, $value);
break;
}
return new WP_REST_Response('Product updated successfully', 200);
}
We not tested the code, but this idea should work :)