0

I added in my theme custom ajax function add to cart. Just same like instruction

$product_id        = apply_filters( 'woocommerce_add_to_cart_product_id', absint( ($_POST['variation_id']) ? $_POST['variation_id'] : $_POST['product_id']  ) );
$product           = wc_get_product( $product_id );
$quantity          = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ) );
if ( $product && 'variation' === $product->get_type() ) {
            $variation_id = $product_id;
            $product_id   = $product->get_parent_id();
            $variation    = $product->get_variation_attributes();
        }
WC()->cart->add_to_cart( $product_id, $quantity, $variation_id );

Simple product works fine. But variation product added twice in cart (((

Why is it? What i did wrong? Please help!

My js ajax code:

$(document).on('submit', 'form.cart', function(e) {
        stop();
        e.preventDefault();
        let data = new FormData(this);
        data.append("action", 'add_to_cart');
        let form = $(this);
        $(document).find('.single_add_to_cart_button').addClass('loading').block({
                message: null,
                overlayCSS  : {
                    background: '#fff url(' + yith_qv.loader + ') no-repeat center',
                    opacity   : 0.5,
                    cursor    : 'none'
                }
            });
        let atributes = {};

        form.find('select').each(function(i){
            if( $(this).val()=='' ){
                $(this).closest('.field-input-container').addClass('error');
                console.log( $(this).attr('name') );
                ajax=1;
                $(document).find('.single_add_to_cart_button').removeClass('loading').unblock();
                return false;
            }
            atributes[$(this).attr('name')] = $(this).val();
            if(i+1 == form.find('select').length && form.find('input.error').length == 0 && form.find('textarea.error').length == 0 && form.find('select.error').length == 0 ){
                console.log(form);

                ajax=0;
                
            }
        });

        if(!ajax){

            $.ajax({
                url: wc_add_to_cart_params.ajax_url,  
                type:'POST',
                dataType: 'json',
                processData: false, //require for formdata
                contentType: false,
                data: data,

                success: function(response) {
                    if ( ! response || response.error )
                        return;


                    var fragments = response.fragments;
                    if ( fragments ) {
                        $.each( fragments, function( key, value ) {
                            $( key ).replaceWith( value );
                        });
                    }
                    $('.modal-window').removeClass('open').removeClass('modal-show');
                    $(document).find('.single_add_to_cart_button').removeClass('loading').unblock();
                    $(document).find('.widget_shopping_cart_content').stop().addClass('show');
                    if($(window).width() > 1199) {
                        setTimeout(function() {
                            $('.widget_shopping_cart_content .cart_list').mCustomScrollbar();
                        }, 0);
                    }
                }
            });
        }
        return false;
    });
  • I'd guess you're either calling this code twice or also calling some other code that also adds the product to the cart. I'd either check the browser dev tools network tab to see what requests were made when you clicked the button, or add error_log traces to all of the add_to_carts in your code. I assume your AJAX add to cart isn't also submitting the form too? – Rup Aug 24 at 11:18
  • Thank for your answer. But i checked network tab - sent 1 request my. and when commented //WC()->cart->add_to_cart( $product_id, $quantity, $variation_id ); - product don't add in cart any more. So i know - working just my code. – Galina Bublik Aug 25 at 9:45

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.