0

I'm trying to make my variable products more expensive when they fall under a certain quantity. Something like a reverse-discount.

I found out how to change the price and a way to get an array of the variation stock quantity, only how to get both things to work together is still a mystery to me. Can someone help?

// Variable
add_filter( 'woocommerce_product_get_price', 'jp_custom_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'jp_custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_regular_price', 'jp_custom_price', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'jp_custom_price', 10, 2 );

function jp_custom_price( $price, $product ) {

if ($product->is_type( 'variable' )){
    $variations = $product->get_available_variations();

    foreach ( $variations as $variation ) {
                    $variation_o = new WC_Product_Variation( $variation['variation_id'] );
                    $variations_stock = $variation_o->get_stock_quantity();

                    if ($variations_stock < 100) {
                            return $price * 1.2;
                    } elseif ($variations_stock < 50) {
                            return $price * 1.5;
                    } else {
                            return $price;
                    }
                }

    } else {
     return $price;
 }

}

The idea is if variation has a stock quantity of less than 100 the price is multiplied by 1.2. When the quantity falls under 50 it shall be multiplied by 1.5.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.