How to update product variation in cart-review page in woocommerce , wordpress

Today i was searched for how to update cart product's  variation on cart-review page and checkout page,
after searching a lot didnt get any best solution so i made this script for me..
you need to call your action after all cart page or checkout page request call , so i setted 99 as third parameter in add_action ;

below is example code which i made for my client site

add_action('init','update_cart_product_rj_funciton',99);
 function update_cart_product_rj_funciton()
{
   
    global $wpdb,$woocommerce;
   
    if(isset($_REQUEST['cart_update_variation']) && wp_verify_nonce($_REQUEST['cart_update_variation'],'cart_item_variation_update'))
    {
       
        if($_REQUEST['product_id']!='' && $_REQUEST['cart_id']!='')
        {
            $cart_pro_key = '';
     
            foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item )
            {
                if($cart_item['product_id']==$_REQUEST['product_id'])
                {
                    $cart_pro_key = $cart_item_key;
                }
            }
       
              $woocommerce->cart->set_quantity($cart_pro_key,0);
           
          $arr = array();
                    $arr['attribute_pa_size'] = $_REQUEST['cart_id_new_var'];
              $woocommerce->cart->add_to_cart( $_REQUEST['product_id'], $_REQUEST['pro_quantity'], $_REQUEST['variation_id'], $arr, null );
     
        }
    }
   
}

Comments

Popular posts from this blog

How to add image option in nav menu in wordpress

How to add a custom email functionality in woocommerce,wordpress for example new email template for shipping order

How to change user id on checkout page for assign order to different user in woocommerce wordpress