【问题标题】:Dynamic information based on shipping method showing twice基于运输方式的动态信息显示两次
【发布时间】:2020-09-05 19:32:40
【问题描述】:

我已尝试使用此示例 (Dynamic information based on shipping method and cart total In Woocommerce),但是我的动态消息显示了两次。非常感谢任何帮助。

基本上我要做的是在用户的送货超出送货区域时显示一条消息(使用 WooCommerce 的“您的其他区域未覆盖的位置”送货区域)。仅当用户不在定义的运输区域中时,该消息才会正确显示 - 但它会加倍。一个似乎在 ajax 刷新之内,另一个似乎在它之外。不完全确定它是如何或为什么这样做的。

//add message to CHECKOUT if outside delivery area
add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
function outside_delivery_checkout() {
    global $woocommerce;
    if ( did_action( 'woocommerce_review_order_after_shipping' ) >= 2 ) {
        return once;
    }

    $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
    $cart_subtotal             = WC()->cart->subtotal;

    if ($cart_subtotal && $chosen_shipping_method === 'free_shipping') {
        echo '<div class="outside-delivery checkout"><b>PLEASE NOTE:</b><br /> Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge. <strong>Please refresh the page after updating your shipping settings.</strong></div>';
    } elseif ($cart_subtotal && $chosen_shipping_method != 'free_shipping') {
        // do nothing
    }
}

这里是问题的链接:https://redstarrolloffqc.com/dumpster-sizes-rates/ - 添加产品并转到结帐页面。

【问题讨论】:

    标签: php html ajax wordpress woocommerce


    【解决方案1】:

    这里的主要问题是你试图插入一个带有一些内容的&lt;div&gt; html标签,在一个html &lt;table&gt;中,紧跟在一个关闭的&lt;/tr&gt; html标签之后......

    因此,显示的 Html 中断,即使它看起来没有。

    您应该使用以下 html 结构:

    <tr><td colspan="2"><div>Your formatted text (message)</div></td></tr>
    

    现在did_action() 条件对您的代码没有影响,并且global $woocommerce 不是必需的(并且已经过时了一段时间)。

    这样您的内容将按原样显示在表格行中。

    这将彻底解决您的问题...

    所以你的代码会是这样的:

    //add message to CHECKOUT if outside delivery area
    add_action( 'woocommerce_review_order_after_shipping', 'outside_delivery_checkout', 20);
    function outside_delivery_checkout() {
        $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];
        $chosen_shipping_method    = explode(':', $chosen_shipping_method_id)[0];
        $cart_subtotal             = WC()->cart->subtotal;
    
        if ( $cart_subtotal && $chosen_shipping_method === 'free_shipping' ) {
            $html  = '<tr class="delivery-message"><td colspan="2"><div class="outside-delivery checkout">
                <strong>' . __("PLEASE NOTE:") . '</strong><br />';
            $html .= __("Your shipping destination is outside of our normal delivery area. Our team will call you to calculate an additional fuel surcharge.");
            $html .= ' <strong>' . __("Please refresh the page after updating your shipping settings.") . '</strong>';
            echo $html . '</div></td></tr>';
        }
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

    【讨论】:

    • 非常感谢!这就像一个魅力。位置不同,但这对我来说并不重要。赞一个!
    • @Trisha 该位置是woocommerce_review_order_after_shipping动作钩子的真实位置……你可以在checkout/review-order.php相关模板中看到。
    猜你喜欢
    • 2019-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    相关资源
    最近更新 更多