我正在建立一个网站,我正在使用WFCM插件将其用作市场. 我想当一个产品的价格发生变化时,我也想改变所有同名的产品. 到目前为止,我实现了改变所有产品的价格,无论名称.
我使用的最新代码是:
add_action('save_post', 'update_prices_by_product_name', 10, 2);
function update_prices_by_product_name($post_id, $post) {
if ($post->post_type === 'product') {
$product_name = $post->post_title;
$new_price = get_post_meta($post_id, '_regular_price', true);
$related_products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'exclude' => $post_id,
'post_title' => $product_name,
));
foreach ($related_products as $related_product) {
$related_product_id = $related_product->ID;
$update_data = array(
'ID' => $related_product_id,
'post_title' => $product_name,
);
wp_update_post($update_data);
}
}
}
它不起作用,它会改变所有产品的价格.我知道我已经很接近了,但我需要帮助.