只有当我有一个来自指定类别的产品时,我才需要显示此自定义字段 . 那么只需要字段 .
下面的代码只是设置为显示:无字段,事实上,如果我点击结帐有一个错误,如“字段myfield是必需的”,但我需要删除我的字段不设置显示无 .
请问有什么想法?
我有最新版的WooCommerce .
这是我的代码:
/**
* Add the field to the checkout
*/
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['my_field_name'] = array(
'label' => __('Codice medico', 'woocommerce'),
'placeholder' => _x('Inserisci il codice del tuo medico', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['my_field_name'] )
wc_add_notice( __( '<h4 style="color:red;">Stai acquistando un prodotto per cui è necessaria la prescrizione medica. Per favore inserisci il codice del tuo medico per proseguire. Grazie.</h4>' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, 'Codice Medico', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Codice Medico').':</strong> ' . get_post_meta( $order->id, 'Codice Medico', true ) . '</p>';
}
/* Adding Custom Fields to Emails
1. Add this snippet to your theme's functions.php file
2. Change the meta key names in the snippet
3. Create a custom field in the order post - e.g. key = "Tracking Code" value = abcdefg
4. When next updating the status, or during any other event which emails the user, they will see this field in their email
*/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');
function my_custom_order_meta_keys( $keys ) {
$keys[] = 'Codice Medico'; // This will look for a custom field called 'Tracking Code' and add it to emails
return $keys;
}
/**
* Conditionally remove checkout fields
*/
add_filter( 'woocommerce_checkout_fields' , 'conditional_unset_checkout_field' );
function conditional_unset_checkout_field( $fields ) {
$categories = array( 'prodotti-in-polvere-e-bustine', 'gel-e-creme', 'prodotti-in-capsule', 'prodotti-plantari', 'prodotti-liquidi', 'area-riservata' );
$has_cat = false;
// Iterating through each cart items (products)
foreach(WC()->cart->get_cart() as $cart_item){
// Iterating through each category in your array
foreach($categories as $category){
if ( has_term( $category, 'product_cat', $cart_item['product_id'] ) ) {
$has_cat = true;
break;
}
}
if ( $has_cat ) break;
}
// If one of the categories is in one cart item, we unset the field
if ( $has_cat ) {
unset( $fields['billing']['my_field_name'] );
}
return $fields;
}
1 回答
仅在购物车项目中有已定义的类别时取消设置字段,如果还有其他类别,则取消设置,如您所示 .
First: 您在这里使用 my answer 的 my answer 代码到您昨天的问题:
Remove a checkout field if cart items are from some specific product categories
你这里没有提到它 .
After that, 我已按预期测试了您的代码(有一些未成年人修改)on this test server和 It's working perfect !如果您添加'posters'类别的商店"Flying Ninja"的第一个产品,您的自定义结帐字段将根据需要删除...如果您从购物车中删除"Flying Ninja"并添加例如'posters'类别中的第二个产品"Happy Ninja",您的自定义字段将再次显示...
那是你轻微改变的代码: