skip to Main Content

I have a sheet that is exported from my customers Back end stock system.

It includes sale price and retail price. The problem is for the sale pricing if there is no price they are using “0” as the value which in turn means that when the product update runs the product essentially becomes free

I have tried a few options from here. This one seemed to work the best

add_action ('woocommerce_before_shop_loop_item', 'check_sale_price', 40 );
add_action('woocommerce_before_single_product', 'check_sale_price', 40 );
function check_sale_price() {
    global $product;
    if ( $product->sale_price == '0' ) {
        $price = $product->regular_price;
        $product->sale_price = $price;
        $product->price = $price;
        global $wpdb;
        $wpdb->get_results( 'UPDATE wp_postmeta SET meta_value='.$price.' WHERE meta_key="_sale_price" AND post_id='.$product->id, OBJECT );
        $wpdb->get_results( 'UPDATE wp_postmeta SET meta_value='.$price.' WHERE meta_key="_price" AND post_id='.$product->id, OBJECT );
    }
}

But this did not update all my variable products only simple products

It would be great if this code could also update product sale pricing that is “0” in product variations as well

3

Answers


  1. Tested and works with query you can use it in your function.

    Try this (where, $price = "set you price" , $product->id = "product id")

    UPDATE wp_postmeta as pm
        JOIN  wp_postmeta as pm2 ON pm.post_id = pm2.post_id
        SET pm.meta_value = ( $price )
        WHERE pm.meta_key LIKE '_sale_price'
          AND pm2.meta_key LIKE '_price'
          AND pm.post_id IN
            ( SELECT p2.ID
             FROM wp_posts AS p
             JOIN wp_posts AS p2 ON p2.post_parent = p.ID
             WHERE p.post_type = 'product'
               AND p.post_status = 'publish'
               AND p.ID = $product->id
               AND p2.post_type = 'product_variation'
               AND p2.post_status = 'publish' ); 
    
    Login or Signup to reply.
  2. Try this for update product price

        add_action('woocommerce_before_single_product', 'check_sale_price', 40 );
    function check_sale_price() {
    global $product;
    //update product price 
    if ( $product->get_sale_price() == '0' ) {
            $price = $product->get_price();
            $product->set_sale_price($price); 
            $product->set_price($price); 
            $product->save(); 
    }
    }
    
    Login or Signup to reply.
  3. Try This

      add_action('woocommerce_before_single_product', 'check_sale_price', 40 );
    function check_sale_price() {
        global $product;
        //update product price 
        $available_variations = $product->get_available_variations();
        foreach($available_variations as $variation ){
            $variation_id = $variation['variation_id'];
            $_product = new WC_Product_Variation($variation_id);
    
            if ( $product->get_sale_price() == '0' ) {
                    $price = $_product->get_price();
                    $_product->set_sale_price($price); 
                    $_product->set_price($price); 
                    $_product->save(); 
            }
        }
        $product->save(); 
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search