【问题标题】:Add to cart is slow due to shipping cost calculation由于运费计算,添加到购物车速度很慢
【发布时间】:2023-01-09 14:23:58
【问题描述】:

由于 DHL 插件计算运费,添加到购物车速度很慢。 我们在主题 function.php 中添加了以下代码行

/*
Remove recalculation of the shipping cost when Add-to-Cart. This prevents slow add-to-cart.
*/
function filter_need_shipping ($val) {
    $prevent_after_add = WC()->cart->prevent_recalc_on_add_to_cart;
    return $val && !$prevent_after_add;
}
add_filter( 'woocommerce_cart_needs_shipping', 'filter_need_shipping' );

function mark_cart_not_to_recalc ($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
    WC()->cart->prevent_recalc_on_add_to_cart = true;
}
add_action('woocommerce_add_to_cart', 'mark_cart_not_to_recalc', 10, 6);

添加到购物车现在很快,但只要 WooCommerce 插件处于活动状态,以下磨损就会显示在仪表板中。

Warning: Attempt to read property "prevent_recalc_on_add_to_cart" on null in /home/website/public_html/wp-content/themes/woodmart-child/functions.php on line 568

您的帮助将不胜感激

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:
    function filter_need_shipping($val) {
        $prevent_after_add = (isset(WC()->cart->prevent_recalc_on_add_to_cart)) ? true : false;
        return $val && !$prevent_after_add;
    }
    
    add_filter('woocommerce_cart_needs_shipping', 'filter_need_shipping');
    
    function mark_cart_not_to_recalc($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
        WC()->cart->prevent_recalc_on_add_to_cart = true;
    }
    
    add_action('woocommerce_add_to_cart', 'mark_cart_not_to_recalc', 10, 6);
    

    您需要检查设置了 prevent_recalc_on_add_to_cart 对象以禁用警告。在您将一件产品添加到购物车之前,它不会被设置。 woocommerce_cart_needs_shipping 钩子将在每个产品页面加载时触发

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多