批量更新Woocommerce变体价格


Bulk Updating Woocommerce Variation Prices

我正在尝试使用wordpress之外的php/mysql脚本更新wooccommerce变体价格。

我有一个脚本,用于更新wp_postmeta表中product的一个product_variation_price_regular_price值。

如果我有多个变体,则该变体的正确价格会显示在网页上,但不会更新来自wooccommerce的price.php的价格/价格范围。

但是,如果我只有这一个变体,表中的价格会更新,但不会在渲染的网页上更新。

我还试图编辑product本身的价格。但是:我仍然在渲染网页上得到旧的价格。

基本上,我现在在这些领域有相同的新价格:

产品变体->postmeta中:_price, _regular_price

产品->postmeta中:_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为我指明了正确的方向,我尝试了这个:

  1. 在脚本中包含wp-load.php
  2. 称为WC_Product_Variable::sync( $post_id );

->不幸的是,这对我不起作用。我把它放在这里是因为我认为这是正确的方法——希望它能帮助其他人。

真正帮助我的是将其包含在我的函数中。hp:

// 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>';
}

请使用以下脚本批量更新产品变体。单击此处查看完整代码。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;
}