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

时间:2018-01-08 14:35:16

标签: php wordpress woocommerce

我正在尝试使用ajax从结帐页面更新购物车中的产品,然后再继续付款(来自订单查看表)。

但我无法做到。

1.首先我添加到自定义meta这个功能的购物车。 ///存储自定义字段

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。然后我想在结帐页面(订单审核选择)中更改产品

<?php
            do_action( 'woocommerce_review_order_before_cart_contents' );
             //print_r( WC()->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帮助发送数据并获得响应..

<script>
 $('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 = "<?php echo admin_url( 'admin-ajax.php' );?>"
        $.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);
            } 
    });
});
});
</script>

4.更新功能

//==============chackout oder review upadte===========//
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值。提供一个解决方案来更改产品ID和项目自定义元数据。在此功能中。??

1 个答案:

答案 0 :(得分:0)

更新的功能代码

// +++ ===============扩展阅读评论=========== //

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');