Created
November 3, 2022 17:42
-
-
Save InpsydeNiklas/1fec066c527ef32650bd3091a1363042 to your computer and use it in GitHub Desktop.
MultilingualPress - Automatically copy WooCommerce product SKU & Price to all connected products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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