Vidhya Moorthy
Vidhya Moorthy

Reputation: 99

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

Upvotes: 0

Views: 440

Answers (1)

Shan
Shan

Reputation: 475

In the php page ajax url page,

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

Upvotes: 2

Related Questions