简体   繁体   中英

Add to cart in woocommerce

I am trying to do the add to cart function on an image which is a link to cart page. I have written the following code

simple.php

<form class="cart" method="post" onSubmit="return validateIn('<?= $product->id?>');" enctype='multipart/form-data'>
    <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

    <?php
        if ( ! $product->is_sold_individually() )
            woocommerce_quantity_input( array(
                'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
                'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
            ) );
    ?>

I have done an animation on add to cart after which I am trying to add the code for add to cart

function validateIn(p_id){

//code for animation
event.preventDefault();
    addToCart(p_id);
  return false;




}
function addToCart(p_id) {
        jQuery.ajax({
          type: 'POST',

          url: 'addtocart.php',

          data: { 'product_id':  p_id,
          },
          success: function(response, textStatus, jqXHR){


            console.log("Product added");
            }
        }); 
  }

Can anybody tell me how to proceed!!Pls Help

在php页面ajax url页面中,

global $woocommerce; $woocommerce->cart->add_to_cart($product_id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM