0

I am using woocommerce latest version. I am developing a woocommerce supported theme from the scratch. Now I have a little problem. On archive page Ajax for "Add to cart" button is not working. Whenever I click "Add to cart" button the page get reloaded. I check the product setting and I got the "Enable AJAX add to cart buttons on archives " is checked. But the ajax is still not working. Then I have tried another way. I have written the following code at the bottom of my shop page.

    $('.button').click(function(e){

    $.ajax({
                    url:"<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
                    type:'POST',
                    data:'action  =add_to_cart',
                    error: function (request, error) {
                        console.log(arguments);
                        alert(" Can't do because: " + error);
                    },
                    success:function(results)
                        {
                            alert(results);
                        }
                    });

        e.preventDefault();
});

Then I wrote this code in functions.php page. These are here.

function add_to_cart(){



defined( 'ABSPATH' ) || exit;

global $product;

if ( ! $product->is_purchasable() ) {
    return;
}

echo wc_get_stock_html( $product ); 

if ( $product->is_in_stock() ) :

 do_action( 'woocommerce_before_add_to_cart_form' ); 

echo '<form class="cart" action="<?php echo esc_url( apply_filters( "woocommerce_add_to_cart_form_action", $product->get_permalink() ) ); ?>" method="post" enctype="multipart/form-data">';

     do_action( 'woocommerce_before_add_to_cart_button' ); 


    do_action( 'woocommerce_before_add_to_cart_quantity' );

    woocommerce_quantity_input( array(
        'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
        'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
        'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
    ) );

    do_action( 'woocommerce_after_add_to_cart_quantity' );


    echo '<button type="submit" name="add-to-cart" value="<?php esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt">';

    echo '<i class="fa fa-shopping-cart"></i>';

    echo esc_html( $product->single_add_to_cart_text() );

    echo '</button>';

    do_action( 'woocommerce_after_add_to_cart_button' ); 

echo '</form>';

do_action( 'woocommerce_after_add_to_cart_form' ); 

endif; 

}

add_action('wp_ajax_add_to_cart','add_to_cart');
add_action('wp_ajax_nopriv_add_to_cart','add_to_cart');

It is still not working. So what can I do now?

Note: I am a new bee in woocommerce type work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.