在WooCommerce 3中的特定运送方式下方添加一个复选框 [英] Add a checkbox below a specific shipping method in WooCommerce 3

查看:124
本文介绍了在WooCommerce 3中的特定运送方式下方添加一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to customize the WC checkout page. In my checkout page I've set 2 shipping methods. My goal is to add a custom field down below the first shipping method. To achieve this I used the hook I mentioned below.

woocommerce_after_shipping_rate

但是我的问题是,它只是为每种可用的送货方式添加了相同的字段.如何管理添加特定运送方式的输入字段?

But my problem with this is, it just adds the same field for each shipping methods available. How can I manage to add an input field for a particular shipping method?

推荐答案

使用作为

Using the $method argument that is an instance of the WC_Shipping_Rate Object you can target any shipping method Id using get_id() method, like in this example:

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

类似的答案:

  • Shipping carrier custom fields validation in Woocommerce checkout page
  • Dynamic shipping fee based on custom radio buttons in Woocommerce
  • Update fee dynamically based on radio buttons in Woocommerce checkout

这篇关于在WooCommerce 3中的特定运送方式下方添加一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆