WooCommerce - 在Ajax更新后计算购物车总额
问题描述:
如果购物车总额低于50美元,我有一小部分代码可计算购物车总额(不含税)并输出免费送货优惠。WooCommerce - 在Ajax更新后计算购物车总额
// Shipping Upsell
/**
* Checks the cart for the Total excluding taxes
* @param $total_required
* @return bool
*/
function qualifies_basedon_cart_total($total_required) {
/*
* We only want to run this on the cart or checkout page
* If the cart subtotal is less than $total_required for free shipping, return true
*/
if(is_cart() || is_checkout()) {
if(WC()->cart->subtotal_ex_tax < $total_required) {
return true;
}
}
// do nothing
return false;
}
function shipup(){
// tests if total is below $50 and displays upsell if query returns ture
if(qualifies_basedon_cart_total(50)) {
echo "<div class =\"shipup\"><h3>Free Shipping</h3>\n<p>Get free shipping on all orders over $50!</p></div>";
}
}
add_action ('woocommerce_cart_collaterals','shipup', 1);
代码在初始页面加载上面的伟大工程,但改变车页面的数量并选择“更新购物车”我有以上(在functions.php中)的代码基于不调整后本身新购物车总数。
我相信更新购物车按钮使用AJAX,我的代码无法使用它。如何AJAXIFY我的代码,以便它的基础上动态购物车总计?
答
如果您使用的是WooCommerce 2.6或更高版本,WC已经创建了一种方法来做到这一点,而无需额外的代码。
您需要在“WooCommerce” - >“设置” - >“发货” - >“运输区域”下设置“运输区域”。
例如,我有“本地”和“美国”运输区。
- 对于“美”航运区,然后我创建了一个“航运法”(点击区增加了送货方式,然后点击标题为“添加运输方式”的灰色按钮)。
- 选择“免运费”方式并点击“添加送货方式”。
- 然后点击“免费送货”方法下的“设置”。
- 此时,您可以选择适用免费送货方式。使用“Free Shipping Requires ...”下拉菜单并选择“A minimum order amount”,并在“Minimum Order Amount”字段中输入“50”。
- 然后点击“保存更改”。
现在当购物车的总额是50美元或更多,然后免费送货将可用。
答
<?php
/* Update cart total on quantity change */
function cart_select_change_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$(".product-quantity .quantity_select select.qty").change(function(){
$("section.entry .woocommerce").append('<div style="display:none" class="blockUI"></div>');
$("section.entry .woocommerce").append('<div style="z-index: 1000; border: medium none; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; background: url("/wp-content/plugins/woocommerce/assets/images/[email protected]") no-repeat scroll center center/16px 16px rgb(255, 255, 255); opacity: 0.6; cursor: wait; position: absolute;" class="blockUI blockOverlay"></div>');
$(".actions input.button").click();
});
});
</script>
<?php
}
add_action('woocommerce_after_cart', 'cart_select_change_js', 10);
?>
试试这个代码片断
请,这是捡来的代码没有任何变化。这里是你的链接,包括更新你的答案:https://gist.github.com/rours/637b722debac38037766;你有没有测试过它?如果没有,也可以说未经测试。 – LoicTheAztec