Update WooCommerce Cart After Adding Variable Prod

2019-02-19 06:39发布

问题:

I've assembled the link to add a variable product to my cart like so but I'm at a loss on how to then "refresh" the cart without reloading the page. My guess is that I'm not properly making this AJAX add to cart request and therefore, the woocommerce_add_to_cart_fragments (where I believe my cart HTML would be placed for refresh) isn't being called.

$addToCartLink = '?add-to-cart=' . $prod->id . '&variation_id=' . $var_id . '&attribute_pa_quantity-attribute=' . $var_quantity;

jQuery.get($addToCartLink, function() {
    //refreshCart();
}); 

If anyone can just point me in the right direction I'd be greatly appreciative. Variable/AJAX/WooCommerce docs seems to be rather sparse.

回答1:

@helgatheviking - Thanks, I found that a few hours after I posted. This also provided a mega assist https://github.com/wp-plugins/woocommerce-ajax-add-to-cart-for-variable-products/blob/master/js/add-to-cart-variation.js.

Here was my finished solution in case it helps anyone.

I'm storing my var_id and product_id as data attributes and quantity and the var_name are hardcoded in this instance. .doodleVarButton is the class I apply to all my variable item add to cart links.

The JS

jQuery( function( $ ) {

    $( document ).on( 'click', '.doodleVarButton', function() {

        var var_id = $(this).data("varid");
        var product_id = $(this).data("prodid");

        var data = {
            action: 'woocommerce_add_to_cart_variable_rc',
            product_id: product_id,
            quantity: 1,
            variation_id: var_id,
            variation: 'quantity-attribute'
        };

        jQuery.post( woocommerce_params.ajax_url, data, function( response ) {

            var fragments = response.fragments;

            if ( fragments ) {

                $.each(fragments, function(key, value) {
                    $(key).replaceWith(value);
                });

            }

        });

    });

The extended WC action (threw in functions.php)

add_action( 'wp_ajax_woocommerce_add_to_cart_variable_rc','woocommerce_add_to_cart_variable_rc_callback' );
function woocommerce_add_to_cart_variable_rc_callback() {

    $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
    $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
    $variation_id = $_POST['variation_id'];
    $variation  = $_POST['variation'];
    $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );

    if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation  ) ) {

        do_action( 'woocommerce_ajax_added_to_cart', $product_id );
        if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
            wc_add_to_cart_message( $product_id );
    }

        WC_AJAX::get_refreshed_fragments();

    } else {
        //$this->json_headers();
        header('Content-Type: application/json');

    $data = array(
        'error' => true,
        'product_url' => apply_filters(  'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
        );
        echo json_encode( $data );
     }

        die();
}  

Then - the key - WC_AJAX::get_refreshed_fragments calls the crucial woocommerce_header_add_to_cart_fragment function where you update your cart. My cart is modularized in a separate PHP file.

Just make sure 'xxx' in $fragments[xxx] matches the container for your cart code.

function woocommerce_header_add_to_cart_fragment( $fragments ) {

ob_start();

include( dirname(__FILE__) . "/../views/checkout-bar.php");

$fragments['div.checkout'] = ob_get_clean();

return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment' );