In woocommerce 2.x I was using the woocommerce_before_calculate_totals and woocommerce_get_price filters to adjust the prices of variable products in my store. These filters stopped working properly in WC 3, so I modified my code to use the new woocommerce_product_variation_get_price filter. The rest of the code essentially remained the same. I already knew that the code affected site performance, but after the filter change, the hit seems to be far more significant. I’m just wondering if there’s a way I can modify the below code to be more performance friendly. The full code uses a series of if statements to identify each possible attribute combination for the variable products in my store and adjusts prices accordingly. I’ve only included 2 of those if statements below.

add_filter('woocommerce_product_variation_get_price','change_price', 10, 2);
function change_price($price, $product){        

//find attributes
            $option1 = 'pa_license-options';
            $option2 = 'pa_delivery-format';
            $getmeta1 = get_post_meta($product->variation_id , 'attribute_'.$option1, true);
            $getmeta2 = get_post_meta($product->variation_id , 'attribute_'.$option2, true);
            $attribute1 = get_term_by('slug', $getmeta1, $option1);
            $attribute2 = get_term_by('slug', $getmeta2, $option2);
            $license = $attribute1->name;
            $format = $attribute2->name;



    //adjust prices of variable products


        if ($format === "mp3" && $license === "Demo License" ){

                //apply price adjustment and return the new value

                $price = 5;         
        }
        else if ($format === "mp3" && $license === "Basic License" ){

                $price = 10;            
        }

return $price;

}

Read more here: Performance friendly way to adjust variable product pricing in Woocommercce 3.x?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: