加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 15:59:59 所属栏目:PHP教程 来源:网络整理
导读:我想在WooCommerce结帐页面中添加自定义字段,但是对于选定的产品. 例如,如果客户在购物车中只有产品A,则此自定义字段应出现在WooCommerce Checkout Page中 我在functions.php中使用以下代码来添加自定义字段: add_action('woocommerce_before_order_notes',
我想在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>';

}

解决方法

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

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读