结帐中的WooCommerce自定义字段 [英] WooCommerce Custom Field in Checkout
本文介绍了结帐中的WooCommerce自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在WooCommerce结帐页面中添加自定义字段,但要添加所选产品。
I want add Custom field in WooCommerce checkout page but for selected products.
例如,如果客户的购物车中只有产品A ,此自定义字段应显示在 WooCommerce Checkout页面
For e.g if Client have Product A in cart only than this custom field should appear in WooCommerce Checkout Page
我正在使用functions.php中的以下代码添加自定义字段:
I am using following code in functions.php to add custom field:
add_action('woocommerce_before_order_notes', 'my_delivery_checkout_field');
function my_delivery_checkout_field( $checkout ) {
echo '<div id="my_local_club"><h3>'.__('Delivery Options').'</h3>';
woocommerce_form_field( 'delivery_options', array(
'type' => 'select',
'class' => array('my-club-class form-row-wide'),
'label' => _('<br><br>Please select your options', 'woocommerce'),
'required' => true,
'placeholder' => _x('Please Select An Option...', 'placeholder','woocommerce'),
'options' => array(
'option1' => 'Option 1',
'option_2' =>'Option 2',
'option_3' =>'Option 3'
)
), $checkout->get_value( 'delivery_options' ));
echo '</div>';
}
推荐答案
代码通过StackOverFlow.com解决了我的问题
So following code solve my problem thanks to StackOverFlow.com
/**
* Add the field to the checkout
**/
add_action('woocommerce_before_order_notes', 'my_delivery_checkout_field');
function my_delivery_checkout_field( $checkout ) {
//Check if gift card is in cart
$book_in_cart = conditional_product_in_cart( 7267 );
if ( $book_in_cart === true ) {
echo '<div id="my_local_club"><h3>'.__('Delivery Options').'</h3>';
woocommerce_form_field( 'delivery_options', array(
'type' => 'select',
'class' => array('my-club-class form-row-wide'),
'label' => _('<br><br>Please select your options', 'woocommerce'),
'required' => true,
'placeholder' => _x('Please Select An Option...', 'placeholder','woocommerce'),
'options' => array(
'option1' => 'Option 1',
'option_2' =>'Option 2',
'option_3' =>'Option 3'
)
), $checkout->get_value( 'delivery_options' ));
echo '</div>';
}
}
// Check if Gift card is in the cart
function conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
//flag no book in cart
$book_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
//book is in cart!
$book_in_cart = true;
}
}
return $book_in_cart;
}
这篇关于结帐中的WooCommerce自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文