在 WooCommerce 3 中的特定运输方式下方添加一个复选框
Add a checkbox below a specific shipping method in WooCommerce 3
我正在尝试自定义 WC 结帐页面。在我的结帐页面中,我设置了 2 种送货方式。 我的目标是在第一种送货方式下方添加一个自定义字段。为此,我使用了下面提到的钩子。
woocommerce_after_shipping_rate
但我的问题是,它只是为每种可用的运输方式添加了相同的字段。我如何设法为特定的送货方式添加输入字段?
使用作为 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 文件。已测试并有效。
相似答案:
- Update fee dynamically based on radio buttons in Woocommerce checkout
我正在尝试自定义 WC 结帐页面。在我的结帐页面中,我设置了 2 种送货方式。 我的目标是在第一种送货方式下方添加一个自定义字段。为此,我使用了下面提到的钩子。
woocommerce_after_shipping_rate
但我的问题是,它只是为每种可用的运输方式添加了相同的字段。我如何设法为特定的送货方式添加输入字段?
使用作为 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 文件。已测试并有效。
相似答案:
- Update fee dynamically based on radio buttons in Woocommerce checkout