Bulk Updating Woocommerce Variation Prices

I’m trying to update woocommerce variation prices with a php/mysql script outside of wordpress.

I have a script that updates the _price and _regular_price value of one product_variation of a product in wp_postmeta table.

Read More

If I have more than one variation the correct price of the variation is displayed on the webpage – but the the price / price range coming from woocommerce`s price.php is not updated.

However, if I have only this one variation, the price in the table is updated, but not at all on the rendered webpage.

I also tried to edit the prices of the product itself. But: I still get the old price on the rendered webpage.

Basically I now have the same new price in these fields:

in product-variation -> postmeta: _price, _regular_price

and in product -> postmeta: _price, _regular_price, _min_variation_price, _max_variation_price, _min_variation_regular_price, _max_variation_regular_price

I couldn’t find any other fields with a price – I’m stuck…

Did I miss anything? Is there any other table/field to be updated?

Thanks for your help!

-EDIT-

Maybe this helps: Apparently when having only one Variation my price is rendered with echo $product->get_price_html(); instead of echo $value['price_html'];. So where is the price used in $product->get_price_html(); stored?

Related posts

2 comments

  1. OK after some digging around and thanks to @helgatheviking who pointed me in the right direction I tried this:

    1. included wp-load.php in script
    2. called WC_Product_Variable::sync( $post_id );

    -> unfortunately this didn’t work for me. I just put it here because I think this is the right approach – hopefully it helps someone else.

    What actually helped me was including this in my 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>';
    }
    
  2. Please use the following script to bulk update the product variations.
    Click here for full code. 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;
    
    }
    

Comments are closed.