art
art

Reputation: 167

Change variation price when custom selection is made

I have a woocommerce variable product outside of loop and not on single product page, I want to change variation price based on the selection of a form. If an area is selected change variation price with the custom price added in the custom field created for that variation(area1 or area2). My code works for displaying the correct price after selection but when I add the variation to cart the default price of the variation is added to cart not the custom price.

<form class="areas-form" method="POST" action="">
    Select Your Area 
    <select name="area" onchange="this.form.submit()">
        <option value="" disabled selected>--select--</option>
        <option value="area1">Area 1</option>
        <option value="area2">Area 2</option>
    </select>
</form>
 

add_filter( 'woocommerce_product_variation_get_price', 'varient_price', 99, 2 ); // this code is added in functions.php

function varient_price($price, $variation){
if (  $variation->product_type == 'variation'  ) {
      if(isset($_POST["area"])){
       $area=$_POST["area"];
      } 
    $price = get_post_meta( $variation->variation_id, $area.'_price',true);
    
        return $price;
}
}

Upvotes: 0

Views: 797

Answers (1)

Kairav Thakar
Kairav Thakar

Reputation: 1001

As per your question comment, rest display and other things you are managing. Here, I have just attached the woocommerce hooks and calculation to get your area price to set in your selected variation.

Firstly, you need to get the dynamic price in your select option as per area wise.

Secondly, below code will help you to set your parameter request to be add into cart meta:

function add_area_data_cart_meta( $cart_item_data, $product_id ) {
     
    if( isset( $_POST['area'] ) ) {
        $cart_item_data[ "area" ] = $_POST['area'];     
    }

    
    return $cart_item_data;
     
}
add_filter( 'woocommerce_add_cart_item_data', 'add_area_data_cart_meta', 99, 2 );

Third, before product get add to cart. The above code will fetch your area price value which we have added using above code and below code will set your area price as new price in the cart.

function calculate_area_price_as_variation_price( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = 100;
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( isset( $value["area"] ) ) {                
                if( method_exists( $value['data'], "set_price" ) ) {
                                        /* Woocommerce 3.0 + */
                    $value['data']->set_price( $value["area"] );
                } else {
                        /* Version before 3.0 */
                    $value['data']->price = ( $value["area"] );                    
                }           
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_area_price_as_variation_price', 99 );

Please let me know if you find any issues.

Thanks.

Upvotes: 0

Related Questions