简体   繁体   English

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

[英]WooCommerce - Calculate cart total after Ajax update

I have a small bit of code which calculates the cart total (excluding taxes) and outputs a Free Shipping upsell, if the cart total is less than $50. 我有一小部分代码计算购物车总额(不含税)并输出免费送货加售,如果购物车总额低于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);

The code above works great on the initial page load, but after changing the quantity on the cart page and selecting 'Update Cart' the code I have above (in functions.php) does not adjust itself based on the new cart total. 上面的代码在初始页面加载时效果很好,但在更改购物车页面上的数量并选择“更新购物车”后,我上面的代码(在functions.php中)不会根据新的购物车总数自行调整。 在此输入图像描述

I believe the update cart button uses AJAX and my code is not able to tap into that. 我相信更新购物车按钮使用AJAX,我的代码无法进入。 How can AJAXIFY my code so that it works based on the dynamic cart total? 如何根据动态购物车总数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);
?>

Try this code snippet 试试这段代码

If you're using WooCommerce 2.6 and above, WC has created a way to do this without additional code on your end. 如果您正在使用WooCommerce 2.6及更高版本,WC已经创建了一种方法来实现此目的,而无需额外的代码。

You'll need to setup "Shipping Zones" under "WooCommerce" -> "Settings" -> "Shipping" -> "Shipping Zones". 您需要在“WooCommerce” - >“设置” - >“运输” - >“运输区域”下设置“运输区域”。

For example, I have "Local" and "United States" shipping zones. 例如,我有“本地”和“美国”运输区。

  1. For the "United States" shipping zone, then I created a "Shipping Method" (click on the zone to add a shipping method, then click the grey button titled "Add shipping method"). 对于“美国”运输区域,我创建了“运输方法”(单击区域以添加运输方法,然后单击标题为“添加运输方法”的灰色按钮)。
  2. Select the "Free Shipping" method and click "Add shipping method". 选择“免费送货”方法,然后单击“添加送货方式”。
  3. Then click "Settings" under the "Free Shipping" method. 然后单击“免费送货”方法下的“设置”。
  4. At this point, you have the option to set when the free shipping method applies. 此时,您可以选择何时应用免运费方式。 Use the dropdown for "Free Shipping Requires..." and select "A minimum order amount" and type "50" in the "Minimum Order Amount" field. 使用“免费送货要求...”的下拉菜单,选择“最小订单金额”,然后在“最小订单金额”字段中输入“50”。
  5. Then click "Save Changes". 然后单击“保存更改”。

Now when the cart's total is $50 or more, then free shipping will be available. 现在,当购物车的总价为50美元或更多时,将提供免费送货服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM