使用woocommerce中的ajax删除自定义购物车中的产品

时间:2017-09-23 07:25:38

标签: php jquery ajax wordpress woocommerce

我从购物车中删除产品时遇到问题。我的代码删除了ajax中的产品,但没有刷新购物车。这是我的代码:

add_filter('woocommerce_add_to_cart_fragments','woocommerce_header_add_to_cart_fragment'); function woocommerce_header_add_to_cart_fragment( $fragments ) {
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
ob_start();
$cart_count = sprintf(_n('%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);
?>
<div data-toggle="dropdown" class="cart-customlocation" [...] ?></span>
    <div class="dropdown-menu custom-cart">

        <?php  foreach($cart as $cart_item_key => $item){ ?>

            <?php $img_url = $item['data']->image_id; ?>
            <img src="<?php echo wp_get_attachment_url( $img_url ); ?>" alt="">  
            [...] <- content
            <a  class="remove-product" data-product_id="<?php echo $cart_item_key ?>">X</a>
        <?php } ?>
        <script type="text/javascript">
            jQuery('.remove-product').click(function(){
                var product_id = jQuery(this).attr("data-product_id");
                console.log(product_id);
                jQuery.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: "myURL",
                    data: { action: "product_remove", 
                            product_id: product_id
                    },success: function(data){
                    }
                });
                return false;
            });
        </script>
    </div>
</div>
<?php $fragments['.cart-customlocation'] = ob_get_clean();
return $fragments;

这是一个在ajax中调用的函数:

function product_remove() {

global $woocommerce;

$id = $_POST['product_id'];

$woocommerce->cart->remove_cart_item($id);}

2 个答案:

答案 0 :(得分:0)

您正在从php对象中删除该项,但是如果要从当前视图中删除它,则必须选择该项并使用jquery在您的ajax调用的success函数内删除它。有点像...

,success: function(data){
    $([selector for your product line]).remove();
}

我无法看到您的产品系列的html(以查看您要删除的内容的格式)。如果你粘贴它我会帮你一把。

答案 1 :(得分:0)

嗯。我将此代码添加到我的文件中:

        <?php  foreach($cart as $cart_item_key => $item){ ?>
            <div class="menu-product-id-<?php echo $cart_item_key ?>">
                <?php $img_url = $item['data']->image_id; ?>
                <img src="<?php echo wp_get_attachment_url( $img_url ); ?>" alt="">  

                <?php echo $name = $item['data']->title; ?>

                <?php echo $price = $item['line_total'] / $item['quantity']; ?>

                <?php echo $quantity = $item['quantity']; ?>

                <?php echo $item_key = $cart_item_key; ?>

                <?php echo $item_id = $item['product_id']; ?>

                <a class="remove-product" data-product_id="<?php echo $cart_item_key ?>">REMOVE</a>
            </div>
        <?php } ?>

和ajax的代码:

<script type="text/javascript">
            jQuery('.remove-product').click(function(){
                var product_id = jQuery(this).attr("data-product_id");
                console.log(product_id);
                jQuery.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: "myURL",
                    data: { action: "product_remove", 
                            product_id: product_id
                    },success: function(data){
                        $('menu-product-id-'+product_id).remove();
                    }
                });
                return false;
            });
        </script>

但不起作用