繁体   English   中英

从结帐页面 woocommerce 更新购物车中的产品

[英]Update product in cart from checkout page woocommerce

在使用 ajax 进行付款(从订单审查表)之前,我正在尝试从结帐页面更新购物车中的产品。

但我做不到。

  1. 首先,我使用自定义元功能“存储自定义字段”添加到购物车。

    add_filter( 'woocommerce_add_cart_item_data', 'save_custom_data_with_add_to_cart', 10, 2 ); 功能 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;
    返回 $cart_item_meta; }

  2. 然后我想在结帐页面更改产品(订单评论选择)

    购物车->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 有助于发送数据并获得响应..

    $('body').on('click', '.edit_product', function(e){ $('#plan').show(); }); /// 用于订单审查的 js 更新产品 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 car_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, 成功: function(data){ alert(data); } }); }); });
  4. 更新功能

    //==============结帐订单审核更新 ============// 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 ) ) { //这里我想更新产品id } print_r( $product_); 死(); } add_action('wp_ajax_update_order_review', 'update_order_review'); add_action('wp_ajax_nopriv_update_order_review', 'update_order_review');

Q. 在这个函数中给出更改产品id和项目自定义元数据的解决方案。??

更新功能代码

//+++==============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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM