2016-01-01 152 views
4

我想在WooCommerce結帳頁面添加自定義字段,但對於選定的產品。WooCommerce自定義字段在結帳

對於例如,如果客戶有產品A在車只比這個自定義字段應該頁出現在WooCommerce結帳

我使用下面的代碼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>'; 

} 
+0

交貨選項應該可能使用(非常強大)運輸系統處理。 –

+0

嗨cale_b實際上它不是用於傳遞選項,客戶端想要在結帳頁面添加額外的用戶輸入字段只是爲了得到通知它是 –

回答

1

所以下面的代碼解決了我問題歸功於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; 

} 
相關問題