【问题标题】:Add a checkbox below a specific shipping method in WooCommerce 3在 WooCommerce 3 中的特定运输方式下方添加一个复选框
【发布时间】:2019-01-26 07:31:56
【问题描述】:

我正在尝试自定义 WC 结帐页面。在我的结帐页面中,我设置了 2 种送货方式。 我的目标是在第一种运输方式下方添加一个自定义字段。为此,我使用了下面提到的钩子。

woocommerce_after_shipping_rate

但我的问题是,它只是为每种可用的运输方式添加了相同的字段。我怎样才能设法为特定运输方式添加输入字段?

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce shipping-method


    【解决方案1】:

    使用作为WC_Shipping_Rate 对象实例的$method 参数,您可以使用get_id() 方法定位任何运输方法ID,如下例所示:

    add_action( 'woocommerce_after_shipping_rate', 'checkout_shipping_additional_field', 20, 2 );
    function checkout_shipping_additional_field( $method, $index )
    {
        if( $method->get_id() == 'flat_rate:12' ){
            echo '<br>
            <input type="checkbox" name="shipping_custom_1" id="shipping_custom_1" value="1" class="shipping_method shipping_custom_1">
            <label for="shipping_custom_1">Custom label</label>';
        }
    }
    

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


    类似的答案:

    【讨论】:

    • 谢谢@LoicTheAztec!这正是我所需要的。感谢您的帮助!
    • 选择此自定义字段后如何更改运费值?
    • 这个问题是一旦你选择一个选项,内容就会消失。有没有办法解决这个问题?
    猜你喜欢
    • 2021-05-17
    • 2015-10-28
    • 2017-12-23
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 1970-01-01
    相关资源
    最近更新 更多