使用AJAX添加到购物车以获取网格中的可变产品

时间:2019-08-14 09:44:46

标签: ajax wordpress woocommerce add cart

我正在使用此功能从产品网格AJAX向我的cart添加简单产品:

//Ajax add to cart
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
// Get the necessary classes
$class = implode( ' ', array_filter( array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports( 'ajax_add_to_cart' ) ? 'ajax_add_to_cart' : '',
) ) );

// Adding embeding <form> tag and the quantity field
    $html = sprintf( '%s%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>%s',
    '<form class="cart">',
        woocommerce_quantity_input( array(), $product, false ),
        esc_url( $product->add_to_cart_url() ),
        esc_attr( isset( $quantity ) ? $quantity : 1 ),
        esc_attr( $product->get_id() ),
        esc_attr( $product->get_sku() ),
        esc_attr( isset( $class ) ? $class : 'button' ),
        esc_html( $product->add_to_cart_text() ),
        '</form>'
    );
    }
    return $html;
    }

add_action( 'wp_footer' , 'archives_quantity_fields_script' );
function archives_quantity_fields_script(){ ?>
<script type='text/javascript'>
    jQuery(function($) {
        // Update quantity
        $(".add_to_cart_button.product_type_simple").on('click input', function() {
            $(this).data('quantity', $(this).parent().find('input.qty').val());
        });

        // On "adding_to_cart" delegated event, removes others "view cart" buttons 
        $(document.body).on("adding_to_cart", function() {
            $(".added_to_cart").remove();
        });
    });

</script>
<?php //endif;
}

要从我的productgrid向购物车中添加可变产品(选择变体并添加到购物车),我正在使用此功能:

/**
 * Replace add to cart button in the loop.
 */
function iconic_change_loop_add_to_cart() {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_after_shop_loop_item', 'iconic_template_loop_add_to_cart', 10 );
}

add_action( 'init', 'iconic_change_loop_add_to_cart', 10 );

/**
 * Use single add to cart button for variable products.
 */
function iconic_template_loop_add_to_cart() {
    global $product;

    if ( ! $product->is_type( 'variable' ) ) {
        woocommerce_template_loop_add_to_cart();
        return;
    }

    remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    add_action( 'woocommerce_single_variation', 'iconic_loop_variation_add_to_cart_button', 20 );

    woocommerce_template_single_add_to_cart();
}

/**
 * Customise variable add to cart button for loop.
 *
 * Remove qty selector and simplify.
 */
function iconic_loop_variation_add_to_cart_button() {
    global $product;

    ?>
<div class="woocommerce-variation-add-to-cart variations_button">
    <button type="submit" class="single_add_to_cart_button button ajax_add_to_cart"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
    <input type="hidden" name="add-to-cart" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="product_id" value="<?php echo absint( $product->get_id() ); ?>" />
    <input type="hidden" name="variation_id" class="variation_id" value="0" />
</div>
<?php
}

最后一个功能未通过AJAX添加到购物车。是否可以通过AJAX

修复此问题

0 个答案:

没有答案