Problema con l'aggiunta al carrello Ajax in WooCommerce

1 dic 2015, 07:22:13
Visualizzazioni: 14.4K
Voti: 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
Commenti

Il tuo link è locale.

tao tao
1 dic 2015 07:27:46

scusa :) http://izi-dev.fr/forreygalland/product/macaroons/

Mohamed Rihan Mohamed Rihan
1 dic 2015 07:28:49

Suppongo che tu debba aggiungere abbastanza "Macarons" fino a riempire la scatola (8|16|32) per sbloccare il pulsante Aggiungi al carrello. Suppongo anche che tu non sia ancora molto familiare con il sito web? L'"errore" più divertente oggi, di gran lunga!

tao tao
1 dic 2015 07:34:33

Non si tratta di riempire la scatola, per favore prova con il carrello vuoto e cancella tutti i cookie del browser, poi è difficile aggiungere i macarons come prodotto, ma altri prodotti possono essere aggiunti semplicemente.

Mohamed Rihan Mohamed Rihan
1 dic 2015 07:40:56

Ti trovi in una pagina di prodotto personalizzabile. Non puoi acquistare una scatola vuota di Macarons. Devi specificare quali macarons vuoi all'interno. Seleziona la scatola "Piccola", premi il pulsante + su qualsiasi macaron fino a riempire la scatola e potrai effettuare l'ordine. Lo script responsabile di questo si trova in plugins/woocommerce/assets/js/frontend. Purtroppo sono tutti minificati.

tao tao
1 dic 2015 07:49:33

Per favore cancella i cookie di quel sito e prova ad aggiungere la scatola di macarons al carrello. FYI so che devi riempire la scatola con i macarons per attivare il pulsante e non è questa la domanda. La domanda è perché non aggiunge il prodotto al carrello quando il carrello è vuoto.

Mohamed Rihan Mohamed Rihan
1 dic 2015 08:05:01

Non riesco a riprodurre il tuo bug. Funziona perfettamente per me.

tao tao
1 dic 2015 08:41:13
Mostra i restanti 2 commenti
Tutte le risposte alla domanda 1
2

Penso che la tua funzione php add_to_cart non stia impostando i cookie del carrello, puoi farlo manualmente aggiungendo questa riga all'interno della condizione if prima della

//$added 

riga

aggiungi questa riga

do_action( 'woocommerce_set_cart_cookies', TRUE );

questo imposterà i cookie del carrello.

1 dic 2015 09:12:16
Commenti

Sì, ora funziona.

Mohamed Rihan Mohamed Rihan
1 dic 2015 09:15:53

Grazie!! Ho risolto il problema del carrello vuoto per gli utenti guest, che aveva smesso di funzionare dopo l'aggiornamento a WC 3.0

Gotschi Gotschi
13 giu 2018 15:12:53