WooCommerce AJAX删除了nav-cart的按钮

时间:2018-06-13 19:31:55

标签: php ajax wordpress woocommerce

我在主题中添加了一些Ajax代码用于Nav Menu cart。添加产品时效果很好,但是;

1-没有添加产品的删除按钮 2-即使我从购物车中取出产品,它也不会提升ajaxify。

如何直接从顶部菜单购物车添加移除添加的商品以及如何在从任何地方移除产品时更新此购物车。

我用于单页产品的所有代码:

标题代码:

    <div class="secondary-cart">
    <?php $items = WC()->cart->get_cart();
    global $woocommerce;
    $item_count = $woocommerce->cart->cart_contents_count; ?>
    <a class="cart-totals" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>">Cart (<span><?php echo $item_count; ?></span>)</a>
    <div class="cart-dropdown">
    <div class="cart-dropdown-inner">
    <?php if ($items) { ?>
    <h4>Shopping Bag</h4>
    <?php foreach($items as $item => $values) { 
    $_product = $values['data']->post; ?>
    <div class="dropdown-cart-wrap">
    <div class="dropdown-cart-left">
    <?php $variation = $values['variation_id'];
    if ($variation) {   
    echo get_the_post_thumbnail( $values['variation_id'], 'thumbnail' ); 
    } else {
    echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); 
    } ?>
    </div>
    <div class="dropdown-cart-right">
    <h5><?php echo $_product->post_title; ?></h5>
    <p><strong>Quantity:</strong> <?php echo $values['quantity']; ?></p>
    <?php global $woocommerce;
    $currency = get_woocommerce_currency_symbol();
    $price = get_post_meta( $values['product_id'], '_regular_price', true);
    $sale = get_post_meta( $values['product_id'], '_sale_price', true);
    ?>
        <?php if($sale) { ?>
    <p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
    <?php } elseif($price) { ?>
    <p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>    
    <?php }     ?>
    </div>
    <div class="clear"></div>
    </div>
    <?php } ?>
    <div class="dropdown-cart-wrap dropdown-cart-subtotal">
    <div class="dropdown-cart-left">
    <h6>Subtotal</h6>
    </div>
    <div class="dropdown-cart-right">
    <h6><?php echo WC()->cart->get_cart_total(); ?></h6>
    </div>
    <div class="clear"></div>
    </div>
    <?php $cart_url = $woocommerce->cart->get_cart_url();
    $checkout_url = $woocommerce->cart->get_checkout_url(); ?>
    <div class="dropdown-cart-wrap dropdown-cart-links">
    <div class="dropdown-cart-left dropdown-cart-link">
    <a href="<?php echo $cart_url; ?>">View Cart</a>
    </div>
    <div class="dropdown-cart-right dropdown-checkout-link">
    <a href="<?php echo $checkout_url; ?>">Checkout</a>
    </div>
    <div class="clear"></div>
    </div>
    <?php } else { ?>
    <h4>Shopping Bag</h4>
    <div class="dropdown-cart-wrap">
    <p>Your cart is empty.</p>
    </div>
    <?php } ?>
    </div>
    </div>
    </div>

功能代码:

    function crispshop_add_cart_single_ajax() {
        $product_id = $_POST['product_id'];
        $variation_id = $_POST['variation_id'];
        $quantity = $_POST['quantity'];

        if ($variation_id) {
            WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );
        } else {
            WC()->cart->add_to_cart( $product_id, $quantity);
        }

        $items = WC()->cart->get_cart();
        global $woocommerce;
        $item_count = $woocommerce->cart->cart_contents_count; ?>

        <span class="item-count"><?php echo $item_count; ?></span>

        <h4>Shopping Bag</h4>

        <?php foreach($items as $item => $values) { 
            $_product = $values['data']->post; ?>

            <div class="dropdown-cart-wrap">
                <div class="dropdown-cart-left">
                    <?php $variation = $values['variation_id'];
                    if ($variation) {
                        echo get_the_post_thumbnail( $values['variation_id'], 'thumbnail' ); 
                    } else {
                        echo get_the_post_thumbnail( $values['product_id'], 'thumbnail' ); 
                    } ?>
                </div>

                <div class="dropdown-cart-right">
                    <h5><?php echo $_product->post_title; ?></h5>
                    <p><strong>Quantity:</strong> <?php echo $values['quantity']; ?></p>
                    <?php global $woocommerce;
                    $currency = get_woocommerce_currency_symbol();
                    $price = get_post_meta( $values['product_id'], '_regular_price', true);
                    $sale = get_post_meta( $values['product_id'], '_sale_price', true);
                    ?>

                    <?php if($sale) { ?>
                        <p class="price"><strong>Price:</strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></p>
                    <?php } elseif($price) { ?>
                        <p class="price"><strong>Price:</strong> <?php echo $currency; echo $price; ?></p>    
                    <?php } ?>
                </div>

                <div class="clear"></div>
            </div>
        <?php } ?>

        <div class="dropdown-cart-wrap dropdown-cart-subtotal">
            <div class="dropdown-cart-left">
                <h6>Subtotal</h6>
            </div>

            <div class="dropdown-cart-right">
                <h6><?php echo WC()->cart->get_cart_total(); ?></h6>
            </div>

            <div class="clear"></div>
        </div>

        <?php $cart_url = $woocommerce->cart->get_cart_url();
        $checkout_url = $woocommerce->cart->get_checkout_url(); ?>

        <div class="dropdown-cart-wrap dropdown-cart-links">
            <div class="dropdown-cart-left dropdown-cart-link">
                <a href="<?php echo $cart_url; ?>">View Cart</a>
            </div>

            <div class="dropdown-cart-right dropdown-checkout-link">
                <a href="<?php echo $checkout_url; ?>">Checkout</a>
            </div>

            <div class="clear"></div>
        </div>

        <?php die();
    }

    add_action('wp_ajax_crispshop_add_cart_single', 'crispshop_add_cart_single_ajax');
    add_action('wp_ajax_nopriv_crispshop_add_cart_single', 'crispshop_add_cart_single_ajax');

Jquery代码:

    jQuery('.single_add_to_cart_button').click(function(e) {
        e.preventDefault();
        jQuery(this).addClass('adding-cart');
        var product_id = jQuery(this).val();
        var variation_id = jQuery('input[name="variation_id"]').val();
        var quantity = jQuery('input[name="quantity"]').val();
        console.log(quantity);
        jQuery('.cart-dropdown-inner').empty();

        if (variation_id != '') {
            jQuery.ajax ({
                url: crispshop_ajax_object.ajax_url,
                type:'POST',
                data:'action=crispshop_add_cart_single&product_id=' + product_id + '&variation_id=' + variation_id + '&quantity=' + quantity,

                success:function(results) {
                    jQuery('.cart-dropdown-inner').append(results);
                    var cartcount = jQuery('.item-count').html();
                    jQuery('.cart-totals span').html(cartcount);
                    jQuery('.single_add_to_cart_button').removeClass('adding-cart');
                    jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
                    jQuery('.cart-dropdown').addClass('show-dropdown');
                    setTimeout(function () { 
                        jQuery('.cart-dropdown').removeClass('show-dropdown');
                    }, 3000);
                }
            });
        } else {
            jQuery.ajax ({
                url: crispshop_ajax_object.ajax_url,  
                type:'POST',
                data:'action=crispshop_add_cart_single&product_id=' + product_id + '&quantity=' + quantity,

                success:function(results) {
                    jQuery('.cart-dropdown-inner').append(results);
                    var cartcount = jQuery('.item-count').html();
                    jQuery('.cart-totals span').html(cartcount);
                    jQuery('.single_add_to_cart_button').removeClass('adding-cart');
                    jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
                    jQuery('.cart-dropdown').addClass('show-dropdown');
                    setTimeout(function () { 
                        jQuery('.cart-dropdown').removeClass('show-dropdown');
                    }, 3000);
                }
            });
        }
    });

0 个答案:

没有答案