更新WooCommerce车添加可变产品通过Ajax后 [英] Update WooCommerce Cart After Adding Variable Product Via AJAX
问题描述
我组装的链接到一个变量产品添加到我的购物车像这样,但我在如何再刷新车无需重新加载页面的损失。我的猜测是,我不是正确使这一AJAX添加到购物车的要求,因此,在woocommerce_add_to_cart_fragments(其中,我相信我的购物车的HTML将被放置刷新)不会被调用。
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();
});
如果任何人都可以只点我在正确的方向,我会大大AP preciative。变量/ AJAX / WooCommerce文档显得较为稀疏。
If anyone can just point me in the right direction I'd be greatly appreciative. Variable/AJAX/WooCommerce docs seems to be rather sparse.
推荐答案
@helgatheviking - 谢谢,我发现了几个小时后,我张贴。这也提供了一个大型的辅助的https://github.com/wp-plugins/woocommerce-ajax-add-to-cart-for-variable-products/blob/master/js/add-to-cart-variation.js.
@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.
我存储我的var_id和的product_id的数据属性数量和VAR_NAME很难codeD在这种情况下。 .doodleVarButton是我申请到我的所有变量项类添加到购物车链接。
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.
的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);
});
}
});
});
扩展WC行动(扔在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();
}
然后 - 关键 - WC_AJAX :: get_refreshed_fragments调用关键woocommerce_header_add_to_cart_fragment功能,您更新您的购物车。我的车是模块化在一个单独的PHP文件。
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.
在$碎片只要确保'XXX'[XXX]匹配的容器,您的购物车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' );
这篇关于更新WooCommerce车添加可变产品通过Ajax后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!