Abdul Rehman
Abdul Rehman

Reputation: 648

Apply logical Pricing on Woocommerce Attributes

Dear I've a Pizza site and I'm using Woocommerce plugin. I want to apply some price check on attributes.
My Question is
I've a Pizza product which attributes are Size and Meat. Size(Small,large) Meat(Chicken, donair meat etc). I've applied a check on attributes that when a customer select a product size, small and one piece of meat, the price will be $10 on default rate. But I want to apply this logic here that, when a customer select a more than one piece of meat (Extra topping), Then I want to add $2 on total price like ($10 + $2 = 12).
please help me??

Upvotes: 2

Views: 608

Answers (1)

Shankaranand
Shankaranand

Reputation: 83

Please use this action for adding the extra price on woocommerce cart.Now you need to work on the commented loop code and apply the simple login.Now I have added a summary for how you will work.

I have use it and now it is working on my local system.

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {
    $custom_price = 10; // This will be your custome price  

    foreach ( $cart_object->cart_contents as $key => $value ) {

        $productid = $value['product_id'];
        $variationArr =$value['variation']; //it is an array have the value : meat and size i.e chicken and small

        //In every loop you can count the number of products .
       //Note: In array($variationArr),It have all the active attribute of this product which is created by admin in woocommece. 
        //so you can search it by loop;
        /*
         * Like
         * foreach($variationArr as $k=>$v)
         * {
         * //here is the variation product size
         * }
         * 
         */

        //Now increase the price.
        $price = $value['data']->price;
        $extraPrice = 2;
        $newPrice   = $price + $extraPrice;
        $value['data']->price = $newPrice; 
    }
}

Upvotes: 1

Related Questions