1
votes

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.

  1. 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; }

  2. 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> &nbsp; <?php echo apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) . '&nbsp;'; ?>
                            <?php echo apply_filters( 'woocommerce_checkout_cart_item_quantity', ' <strong class="product-quantity">' . sprintf( '&times; %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
                }
            } 
        ?>
    
  3. 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); } }); }); });
  4. 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.??

1
what is not working? what is your error? what is your requirement? Be clear when asking a question.RamC
Maybe this will helpAmin
hello Thanks for replay..Harendra Singh
hi actually i am trying to add a product to cart and save some additional data in cart meta with every product in cart. and i want to make a option on order review section of checkout page so that user can change the product in cart from checkout page if they want to change. I want that user can only change product in cart but the additional cart meta remain same. is this possible with woo commerce?Harendra Singh
If I understood correctly, you have custom data attached to every cart item and on the order review page the customer can change the items but you want to keep the previously added custom data to get copied to these new (replaced products) cart items. Am I right? If yes, then how are you adding the custom data to cart items in the first place and what is your code on the order_review page through which you are allowing to change the cart items(products)?Dhaval Shah

1 Answers

0
votes

Updated function code

//+++==============chackout oder review upadte===========//

function update_order_review() {
    global $woocommerce;
    global $wpdb;
      $product_id = $_POST['product_id'];
      $_product = wc_get_product( $product_id );
      $price = $_product->get_price();
      $cart_item_key = $_POST['cart_item_key']; 
        /*===set value in cart====*/
        WC()->cart->cart_contents[$cart_item_key]['product_id'] = $product_id ;
        WC()->cart->cart_contents[$cart_item_key]['line_subtotal'] = $price ;
        WC()->cart->cart_contents[$cart_item_key]['line_total'] = $price ;
        $woocommerce->cart->set_session();

die();
}
add_action('wp_ajax_update_order_review', 'update_order_review');
add_action('wp_ajax_nopriv_update_order_review', 'update_order_review');