【问题标题】:Require shipping cost calculation in cart - but only if there are physical products in it需要在购物车中计算运费 - 但前提是其中有实物产品
【发布时间】:2026-01-19 07:50:02
【问题描述】:

在我的客户 WooCommerce 网站上,我们运行一个脚本,强制在购物车页面上计算运费:

add_action('wp_head','prevent_proceed_to_checkout');

function prevent_proceed_to_checkout() { 

echo '  <script>
            jQuery(function(){  
                jQuery(".woocommerce-cart .wc-proceed-to-checkout a.checkout-button").on("click",function(e){
                    var _this = this;
                    alert("Bitte berechne die Versandkosten!");
                    e.preventDefault();
                    jQuery(".shipping-calculator-form .button").on("click",function(){
                        jQuery(_this).off();
                    });
                 });
            });
        </script>';
}

我需要更改此设置,以便在购物车中没有实物产品时不执行此操作。我尝试使用 javascript 检查 .woocommerce-shipping-totals - 类是否在 dom 中,但我想出的解决方案没有成功。

有什么想法吗?

【问题讨论】:

    标签: php jquery woocommerce


    【解决方案1】:

    我现在通过向 onclick 函数添加一个 if 语句来解决这个问题,该函数基本上检查“.woocommerce-shipping-totals”类是否在单击结帐按钮后立即显示在页面上。这是我添加的代码:(就在警报之前)

    var shipping = document.getElementsByClassName("woocommerce-shipping-totals").length;      
    if (shipping > 0) {
    

    【讨论】: