php – 结帐时的WooCommerce自定义字段

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 结帐时的WooCommerce自定义字段脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在WooCommerce结帐页面添加自定义字段,但是对于选定的产品.

例如,如果客户在购物车中只有产品A,则此自定义字段应出现在WooCommerce Checkout Page中

我在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','options' => array(
'option1' => 'Option 1','option_2' =>'Option 2','option_3' =>'Option 3'
)
),$checkout->get_value( 'delivery_options' ));

echo '</div>';

}@H_403_16@

解决方法

因此,以下代码通过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',$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;

}@H_403_16@

脚本宝典总结

以上是脚本宝典为你收集整理的php – 结帐时的WooCommerce自定义字段全部内容,希望文章能够帮你解决php – 结帐时的WooCommerce自定义字段所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。