gpt4 book ai didi

php - WooCommerce - 在 Ajax 更新后计算购物车总数

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:03 25 4
gpt4 key购买 nike

我有一小段代码可以计算购物车总价(不含税)并在购物车总价低于 50 美元时输出免费送货加售。

// 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 中)不会根据新的购物车总数进行 self 调整. enter image description here

我相信更新购物车按钮使用了 AJAX,而我的代码无法利用它。如何 AJAXIFY 我的代码,使其基于动态购物车总数工作?

最佳答案

<?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(&quot;/wp-content/plugins/woocommerce/assets/images/ajax-loader@2x.gif&quot;) 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);
?>

试试这个代码片段

关于php - WooCommerce - 在 Ajax 更新后计算购物车总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com