1

In woocommerce ajax add to cart method not adding when cart is empty, if cart contains something then my ajax function adds but if cart is empty it doesn't add. And no error.

Here is the link. PHP add cart function:

function add_custom_product_meta() {
    global $woocommerce, $wpdb;

    $item_id = isset($_POST['item_id']) ? apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['item_id'] ) ) : FALSE;
    $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] );
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $item_id, $quantity );
    $product_status    = get_post_status( $item_id );

    $variation_id = isset($_POST['variation_id']) ? $_POST['variation_id'] : 0;
    $atributes = isset($_POST['atributes']) ? $_POST['atributes'] : 'Small';
    $attribute_pa_macaroons = isset($_POST['attribute_pa_macaroons']) ? $_POST['attribute_pa_macaroons'] : FALSE;

    if($attribute_pa_macaroons && $passed_validation && $woocommerce->cart->add_to_cart( $item_id, $quantity, $variation_id, array( 'attribute_size' => $atributes,  "macaroons" => $attribute_pa_macaroons))){

        //$added = $woocommerce->cart->add_to_cart( $item_id, $quantity, $variation_id, array( 'attribute_size' => $atributes,  "macaroons" => $attribute_pa_macaroons));

        do_action( 'woocommerce_ajax_added_to_cart', $item_id );

        if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            wc_add_to_cart_message( $item_id );
        }

        WC_AJAX::get_refreshed_fragments();

        echo json_encode(array(
            'atributes' => $atributes,
            'macaroons' => $attribute_pa_macaroons,
            'product_added' => TRUE,
            'ajax_complete' => TRUE,
            'posted' => isset($_POST['item_id']) ? $woocommerce->cart : 'item_id is empty'
        ));
        wp_die();
    }

    echo json_encode(array(
        'product_id' => FALSE,
        'product_added' => FALSE,
        'ajax_complete' => TRUE,
        'posted' => $attribute_pa_macaroons
    ));
    wp_die();
}

Javascript Ajax call:

$.ajax({
                        type : 'post',
                        dataType : 'json',
                        url : woocommerce_params.ajax_url,
                        data : {
                            'action' : 'elephas_addtocart_box',
                            'item_id' : itemID,
                            'quantity' : quantity,
                            'variation_id' : variathionID,
                            'atributes' : atributes,
                            'attribute_pa_macaroons' : itemData
                        }
                    })
                    .done(function(response){
                        if(response.ajax_complete){
                            document.location.reload(true);
                        }
                    })
                    .error(function(error){
                        console.log(error);
                    })
                    .always(function(response){
                        if(response.ajax_complete){
                            document.location.reload(true);
                        }
                    });
7
  • Your link is local.
    – tao
    Dec 1, 2015 at 5:27
  • sorry :) izi-dev.fr/forreygalland/product/macaroons Dec 1, 2015 at 5:28
  • I'm guessing you need to add enough "Macaroons" until you fill the box (8|16|32) in order to unlock the Add to cart button. I'm also guessing you are not exactly familiar with the website yet? Funniest "error" today, by far!
    – tao
    Dec 1, 2015 at 5:34
  • Its not about filling the box, please try with emty cart and emty all cookies on your browser then its difficult to add macaroon as product, but other products can be add simply. Dec 1, 2015 at 5:40
  • You are on a customizable product page. You cannot buy an empty box of Macaroons. You have to specify what macaroons you want inside. Select "Small" box, press the + button from any macaroons until box is full and you can order. The script responsible for this is in plugins/woocommerce/assets/js/frontend. Unfortunatelly they're all minified.
    – tao
    Dec 1, 2015 at 5:49

1 Answer 1

2

I think your php add_to_cart function not setting cart cookies, you can do this manually by adding this line inside the if condition before

//$added 

line

add this line

do_action( 'woocommerce_set_cart_cookies', TRUE );

this will set cart cookies.

2
  • Yes its working now. Dec 1, 2015 at 7:15
  • thank you!! Fixed my empty cart for guest users, which stopped working after a WC 3.0 update
    – Gotschi
    Jun 13, 2018 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.