我已经在WooCommerce上成功实现了AJAX添加到购物车功能,该功能适用于单个产品和可变产品。
尝试将自定义产品数据添加到购物车时会出现问题。 “产品交货日期”插件,可在产品单页上添加一个日期选择器框,允许用户选择产品的交货日期。尚未将此数据添加到购物车。
我已经测试了Storefront主题,并且产品meta已存储在购物车中,所以我确定这与传递给AJAX函数的var有关。
我的AJAX知识是基础知识,因此,如果我缺少明显的内容,请原谅我。
JS用于添加到购物车:
jQuery(document).ready(function() {
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('select[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').html(cartcount);
jQuery('.single_add_to_cart_button').removeClass('adding-cart');
// jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
jQuery('body').addClass('cart-active');
}
});
} 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(data) {
console.log(results);
jQuery('.cart-dropdown-inner').append(results);
var cartcount = jQuery('.item-count').html();
jQuery('.cart-totals').html(cartcount);
jQuery('.single_add_to_cart_button').removeClass('adding-cart');
// jQuery('html, body').animate({ scrollTop: 0 }, 'slow');
jQuery('body').addClass('cart-active');
}
});
}
});
});
PHP用于添加到购物车:
function crispshop_add_cart_single_ajax() {
$product_id = $_POST['product_id'];
$variation_id = $_POST['variation_id'];
$variation = $_POST['variation'];
$quantity = $_POST['quantity'];
if ($variation_id) {
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation);
} else {
WC()->cart->add_to_cart( $product_id, $quantity);
}
$items = WC()->cart->get_cart();
$item_count = count($items); ?>
<span class="hide item-count"><?php echo $item_count; ?></span>
<div class="flex items-center justify-between">
<h4 class="h4 grey">Bag</h4>
<div class="exit">×</div>
</div>
<div class="cart-details">
<div class="flex justify-between item-headers pt3 pb1">
<span class="h6 grey block item-title">Item</span>
<span class="h6 grey block quantity-title">Quantity</span>
<span class="h6 grey block price-title">Price</span>
</div>
<?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']; ?>
</div>
<div class="dropdown-cart-right flex justify-between items-center">
<div class="items">
<h5 class="h6 grey block"><?php echo $_product->post_title; ?></h5>
</div>
<div class="quantites">
<span class="block select-qty select-qty--dis">
<?php echo $values['quantity']; ?>
</span>
</div>
<div class="prices">
<?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);
$varPrice = get_post_meta( $variation, '_regular_price', true);
?>
<?php if($sale) { ?>
<span class="h6 grey price"><strong></strong> <del><?php echo $currency; echo $price; ?></del> <?php echo $currency; echo $sale; ?></span>
<?php } if($price) { ?>
<span class="h6 grey price"><strong></strong> <?php echo $currency; echo $price; ?></span>
<?php } if($varPrice) { ?>
<span class="h6 grey price"><strong></strong> <?php echo $currency; echo $varPrice; ?></span>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
<div class="flex justify-between items-center py2">
<h6 class="h6 grey">Subtotal</h6>
<h6 class="h6 grey"><?php echo WC()->cart->get_cart_total(); ?></h6>
</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="">
<!-- <a href="<?php echo $cart_url; ?>">View Cart</a> -->
<a class="btn btn--dark block" href="<?php echo $checkout_url; ?>">Checkout</a>
</div>
</div>
</div>
</div>
<?php die();
}
在此先感谢您的帮助!