[英]WooCommerce Custom Field in Checkout
我想在WooCommerce結帳頁面中添加自定義字段,但是對於選定的產品。
例如,如果客戶在購物車中只有產品A ,那么此自定義字段應出現在WooCommerce Checkout頁面中
我在functions.php中使用以下代碼來添加自定義字段:
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解決了我的問題
/**
* 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.