更新变体产品价格 - 在产品页面中不可见 - Woocommerce
问题描述:
我制作了可变类型的自定义导入产品。然后我创建了一个文件来更新变化的产品价格。我正在使用update_post_meta方法。这些值出现在每个变体字段的编辑产品页面中,但似乎并未在产品的首页中更新价格。更新变体产品价格 - 在产品页面中不可见 - Woocommerce
我必须在管理面板中更新产品(通过单击更新按钮)以使用新的价格。
我试过使用$ product-> variable_product_sync();但它似乎没有工作。有任何想法吗?
样品我的代码:
foreach ($variations as $variationProduct) {
$variationProductId = $variationProduct["variation_id"];
$productPrice = number_format($productPrice, 2, '.', '');
update_post_meta($variationProductId, '_regular_price', $productPrice);
}
任何帮助或解决方案?
答
解决!最后我通过woocommerce API找到它。如果您使用的是woocommerce 2.7或更新版本,则可以使用以下行:
$product->save();
答
请使用以下脚本更新变化价格。点击这里获取完整的代码。 https://www.pearlbells.co.uk/bulk-update-product-variation-price-woocommerce/
function getExistingProducts($updatedPrices,$skuArray) {
$loop = new WP_Query(array('post_type' => array('product', 'product_variation'), 'posts_per_page' => -1));
while ($loop->have_posts()) : $loop->the_post();
$id = get_the_ID();
$product = wc_get_product($id);
$sku = get_post_meta($id, '_sku', true);
if(in_array($sku, $skuArray )) {
$attributes = $product->get_attributes();
$attributes['medium-quantity-price']['value'] = $updatedPrices[$sku][4];
$attributes['low-quantity-price']['value'] = $updatedPrices[$sku][3];
$attributes['v-low-quantity-price']['value'] = $updatedPrices[$sku][2];
update_post_meta($id,'_product_attributes',$attributes);
echo ' Update Sku : '.$sku.' '.PHP_EOL;
}
endwhile;
}