WooCommerce - AJAX add to cart not storing product

2019-08-17 06:02发布

I have successfully implemented AJAX add to cart functionality on WooCommerce which works for both single and variable products.

An issue arises when attempting to add custom product data to the cart e.g. 'Product Delivery Date' plugin which adds a date picker box to the product single page allowing the user to select a delivery date for the product. The data for this is not being added to the cart.

I have tested with the Storefront theme and the product meta is being stored in the cart so I'm certain it's to do with the vars I am passing to the AJAX function.

My AJAX knowledge is basic, so please forgive me if I am missing something obvious.



JS being used for adding to cart:

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 being used for adding to cart:

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">&#215;</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();
}



Any help would be massively appreciated, thanks in advance!

0条回答
登录 后发表回答