I am trying to update the product in the cart from the checkout page before proceeding the payments (from order review table) using ajax.
But I am unable to do it.
Firstly I am adding to cart with custom meta function "Store the custom field".
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_with_add_to_cart', 10, 2 ); function save_custom_data_with_add_to_cart( $cart_item_meta, $product_id ) { global $woocommerce; $category_id = urldecode(base64_decode($_GET['c'])); $board_id = urldecode(base64_decode($_GET['b'])); $board_name = get_the_title( $board_id ); $cart_item_meta['slot_id'] = $category_id; $cart_item_meta['board_id'] = $board_id; $cart_item_meta['board_title'] = $board_name;
return $cart_item_meta; }then i want to change product in checkout page (order review selection)
cart->get_cart());
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key ); if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_checkout_cart_item_visible', true, $cart_item, $cart_item_key ) ) { ?> <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>"> <td class="product-name"> <a href="#" class="edit_product"><i class="fa fa-pencil" aria-hidden="true"></i></a> <?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . ' '; ?> <?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '× %s', $cart_item['quantity'] ) . '</strong>', $cart_item, $cart_item_key ); ?> <?php echo WC()->cart->get_item_data( $cart_item ); ?> <!--haredra code here --> <select name="plan" id="plan" class="plan" > <option >change product</option> <option value="1">gold</option> <option value="2">sliver</option> <option value="3">bronze</option> </select> <input type="hidden" class="board_id" value="<?php echo $cart_item['board_id'] ;?>"> <input type="hidden" class="slot_id" value="<?php echo $cart_item['slot_id'] ;?>"> <input type="hidden" class="cart_item_key" value="<?php echo $cart_item['key'] ;?>"> </td> <td class="product-total"> <?php echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); ?> </td> </tr> <?php } } ?>
JQuery helps to send data and get response..
$('body').on('click', '.edit_product', function(e){ $('#plan').show(); }); /// js for order review update products jQuery(document).ready(function($){ $('body').on('change', '#plan', function(e){ var product_id = $('.plan').val(); var board_id = $('.board_id').val(); var slot_id = $('.slot_id').val(); var cart_item_key = $('.cart_item_key').val(); var ajaxurlsb = "" $.ajax({ type: "POST", url: ajaxurlsb+"?action=update_order_review", data: "product_id="+product_id+"&board_id="+board_id+"&slot_id="+slot_id+"&cart_item_key="+cart_item_key, success: function(data){ alert(data); } }); }); });Update function
//==============checkout order review update ===========// function update_order_review() { $product_id = $_POST['product_id']; $board_id = $_POST['board_id']; $board_name = get_the_title( $board_id ); $slot_id = $_POST['slot_id']; $cart_item_key = $_POST['cart_item_key']; if ( !empty($product_id) && !empty($board_id ) ) { //here i want to update product id } print_r( $product_); die(); } add_action('wp_ajax_update_order_review', 'update_order_review'); add_action('wp_ajax_nopriv_update_order_review', 'update_order_review');
Q. Give a solution to change products id and item custom meta data.in this function.??