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

Php WooCommerce-在Ajax更新后计算购物车总数,php,ajax,wordpress,woocommerce,Php,Ajax,Wordpress,Woocommerce,我有一小段代码,可以计算购物车总额(不含税),如果购物车总额低于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

我有一小段代码,可以计算购物车总额(不含税),如果购物车总额低于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);
//发货追加销售
/**
*检查购物车中不含税的总额
*@param$total_必需
*@returnbool
*/
函数根据购物车总数($total\u required)限定{
/*
*我们只想在购物车或结帐页面上运行此功能
*如果购物车小计小于免费送货所需的$total\u,则返回true
*/
if(is_cart()| | is_checkout()){
如果(WC()->购物车->小计税费<$total\u所需){
返回true;
}
}
//无所事事
返回false;
}
函数shipp(){
//测试合计是否低于$50,如果查询返回true,则显示upsell
if(符合基本条件,共50辆){
echo“免费送货\n对所有超过50美元的订单免费送货!

”; } } 添加行动(“woocommerce\u cart\u抵押品”,“Shippup”,1);
上面的代码在初始页面加载时效果很好,但是在更改购物车页面上的数量并选择“更新购物车”后,我上面的代码(在functions.php中)不会根据新购物车总数进行自我调整


我相信更新购物车按钮使用AJAX,而我的代码无法利用它。如何根据动态购物车总数调整我的代码?

如果您使用的是WooCommerce 2.6及更高版本,WC已经创造了一种方法,可以在不增加代码的情况下实现这一点

您需要在“WooCommerce”->“设置”->“配送”->“配送区”下设置“配送区”

例如,我有“本地”和“美国”航运区

  • 对于“美国”配送区域,我创建了一个“配送方法”(单击该区域添加配送方法,然后单击标题为“添加配送方法”的灰色按钮)
  • 选择“免费配送”方式,然后单击“添加配送方式”
  • 然后单击“免费送货”方法下的“设置”
  • 此时,您可以选择何时应用免费送货方式。使用“免费送货要求…”下拉列表,选择“最低订单金额”,并在“最低订单金额”字段中键入“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(&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);
    ?>
    
    jQuery(文档).ready(函数($){ $(“.product quantity.quantity\u select.qty”).change(函数(){ $(“section.entry.woocommerce”)。附加(“”); $(“section.entry.woocommerce”)。附加(“”); $(“.actions.input.button”)。单击(); }); });

    请尝试此代码段

    ,这是刚刚提取的代码,没有任何更改。这是你的链接,包括更新你的答案:;你测试过了吗?如果没有,也说未经测试。