Skip to content

Instantly share code, notes, and snippets.

@InpsydeNiklas
Created November 3, 2022 17:42
Show Gist options
  • Save InpsydeNiklas/1fec066c527ef32650bd3091a1363042 to your computer and use it in GitHub Desktop.
Save InpsydeNiklas/1fec066c527ef32650bd3091a1363042 to your computer and use it in GitHub Desktop.
MultilingualPress - Automatically copy WooCommerce product SKU & Price to all connected products
<?php
add_action('multilingualpress.metabox_after_relate_posts', function($context, $request) {
// get post meta value from source site
$sku = (string)$request->bodyValue(
'_sku',
INPUT_POST,
FILTER_SANITIZE_STRING
);
$regularPrice = (string)$request->bodyValue(
'_regular_price',
INPUT_POST,
FILTER_SANITIZE_STRING
);
if (empty($sku) && empty($regularPrice)) {
return;
}
// switch to remote sites and save post meta
$remoteSiteId = $context->remoteSiteId();
$remotePostId = $context->remotePostId();
switch_to_blog($remoteSiteId);
if (!empty($sku)) {
update_post_meta($remotePostId, '_sku', $sku);
}
if (!empty($regularPrice)) {
update_post_meta($remotePostId, '_regular_price', $regularPrice);
}
restore_current_blog();
}, 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment