我正在尝试使用 WordPress 之外的 php/mysql 脚本更新 woocommerce 变体价格。
我有一个脚本,可以更新
_price
表中 _regular_price
的一个 product_variation
的 product
和 wp_postmeta
值。
如果我有多个变体,该变体的正确价格会显示在网页上 - 但来自 woocommerce`s Price.php 的价格/价格范围不会更新。
但是,如果我有只有这个变体,表中的价格会更新,但渲染的网页上根本不会更新。
我还尝试编辑
product
本身的价格。但是:我仍然在渲染的网页上得到旧价格。
基本上我现在在这些领域都有相同的新价格:
在 产品变体 -> postmeta:
_price, _regular_price
并在产品->后元中:
_price, _regular_price, _min_variation_price, _max_variation_price, _min_variation_regular_price, _max_variation_regular_price
我找不到任何其他有价格的字段 - 我被困住了......
我错过了什么吗?还有其他表/字段需要更新吗?
感谢您的帮助!
-编辑-
也许这有帮助:显然,当只有一种变体时,我的价格用
echo $product->get_price_html();
而不是 echo $value['price_html'];
呈现。那么$product->get_price_html();
中使用的价格存储在哪里?
好吧,经过一番挖掘后,感谢@helgatheviking,他为我指明了正确的方向,我尝试了这个:
WC_Product_Variable::sync( $post_id );
-> 不幸的是这对我不起作用。我只是把它放在这里,因为我认为这是正确的方法 - 希望它对其他人有帮助。
真正帮助我的是将其包含在我的functions.php中:
// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
if ($value['price_html'] == '') {
$value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
}
return $value;
}, 10, 3);
// Hook into price html
add_filter( 'woocommerce_get_price_html', 'wpa83367_price_html', 100, 2 );
function wpa83367_price_html( $price, $product ){
WC_Product_Variable::sync( $product->id );
$myPrice = $product->min_variation_price;
$priceFormat = str_replace(utf8_encode('.'), ",", $myPrice);
return '<span class="amount">from ' . $priceFormat . ' €</span>';
}
请使用以下脚本批量更新产品变体。
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;
}