Php 为定义的产品类别购物车项目取消设置结帐帐单字段
只有当我有某个特定类别的产品时,我才需要显示此自定义字段。那么只需要字段 下面的代码只是设置为显示:无字段,事实上,如果我单击checkout,会出现类似“字段myfield is required”的错误,但我需要删除我的字段以不设置显示无 有什么想法吗 我有WooCommerce的最新版本 这是我的代码:Php 为定义的产品类别购物车项目取消设置结帐帐单字段,php,wordpress,woocommerce,checkout,categories,Php,Wordpress,Woocommerce,Checkout,Categories,只有当我有某个特定类别的产品时,我才需要显示此自定义字段。那么只需要字段 下面的代码只是设置为显示:无字段,事实上,如果我单击checkout,会出现类似“字段myfield is required”的错误,但我需要删除我的字段以不设置显示无 有什么想法吗 我有WooCommerce的最新版本 这是我的代码: /** * Add the field to the checkout */ add_filter( 'woocommerce_checkout_fields' , 'custom_o
/**
* 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;
}
/**
*将该字段添加到签出
*/
添加_过滤器('woocommerce_checkout_fields'、'custom_override_checkout_fields');
//我们的挂钩函数-$字段通过过滤器传递!
函数自定义\覆盖\签出\字段($fields){
$fields['billing']['my_field_name']=数组(
“标签”=>“‘Codice medico’、‘woocommerce’”,
“占位符”=>\u x('Inserisci il codice del tuo medico'、'placeholder'、'WOOCOMERCE'),
“必需”=>true,
'class'=>array('form-row-wide'),
“清除”=>true
);
返回$fields;
}
/**
*处理结帐
*/
添加_操作('woocmerce_checkout_process'、'my_custom_checkout_field_process');
函数my\u custom\u checkout\u field\u process(){
//检查是否已设置,如果未设置,则添加错误。
如果(!$\u POST['my\u field\u name'])
wc_添加通知(________________________________________________________;
}
/**
*使用字段值更新订单元
*/
添加操作('woocommerce\u checkout\u update\u order\u meta'、'my\u custom\u checkout\u field\u update\u order\u meta');
函数my\u custom\u checkout\u field\u update\u order\u meta($order\u id){
如果(!空($\u POST['my\u field\u name'])){
更新帖子元($订单id,'Codice Medico',清理文本字段($帖子['my\u字段名称]);
}
}
/**
*在订单编辑页面上显示字段值
*/
添加操作('woocommerce\u admin\u order\u data\u after\u billing\u address'、'my\u custom\u checkout\u field\u display\u admin\u order\u meta',10,1);
函数my\u custom\u checkout\u field\u display\u admin\u order\u meta($order){
回显“”。(“Codice Medico”):”。获取“post”meta($order->id,“Codice Medico”,true)。“”;
}
/*向电子邮件添加自定义字段
1.将此代码段添加到主题的functions.php文件中
2.更改代码段中的元键名称
3.在订单post中创建自定义字段-例如key=“Tracking Code”value=abcdefg
4.下次更新状态时,或在向用户发送电子邮件的任何其他事件中,他们将在电子邮件中看到此字段
*/
添加过滤器('woocommerce\u email\u order\u meta\u keys'、'my\u custom\u order\u meta\u keys');
功能我的\自定义\顺序\元\键($keys){
$keys[]=“Codice Medico”;//这将查找名为“跟踪代码”的自定义字段,并将其添加到电子邮件中
返回$keys;
}
/**
*有条件地删除签出字段
*/
添加_过滤器('woocommerce_checkout_fields'、'conditional_unset_checkout_field');
函数条件\未设置\签出\字段($fields){
$categories=数组('prodotti-in-polvere-e-bastine','gel-e-creme','prodotti-in-capsule','prodotti-plantari','prodotti liquidi','area riservata');
$has_cat=false;
//迭代每个购物车项目(产品)
foreach(WC()->cart->get_cart()作为$cart_项目){
//遍历数组中的每个类别
foreach($categories作为$category){
if(具有术语($category,$product\u cat',$cart\u item['product\u id')){
$has_cat=true;
打破
}
}
如果($has_cat)中断;
}
//如果其中一个类别在一个购物车项目中,我们将取消设置该字段
如果($has_cat){
未设置($fields['billing']['my_field_name']);
}
返回$fields;
}
仅当购物车项目中存在已定义的类别时,才取消设置该字段,而如果您需要,还存在其他类别,则不取消设置该字段
首先:您在这里使用了我对您昨天的问题的回答的代码:
你在这里没提到 在那之后,我已经测试了你的代码(做了一些小的修改),并且它工作得非常完美正如预期的那样!如果您在“海报”类别中添加商店“飞行忍者”的第一个产品,您的自定义结账字段将根据需要删除…如果您从购物车中删除“飞行忍者”,并添加例如不在“海报”类别中的第二个产品“快乐忍者”,您的自定义字段将再次显示 这是您稍加修改的代码:
/**
* 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', 'custom_checkout_field_process');
function 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', 'custom_checkout_field_update_order_meta' );
function custom_checkout_field_update_order_meta( $order_id ) {
$my_field_name = $_POST['my_field_name'];
if ( !empty($my_field_name) )
update_post_meta( $order_id, 'Codice Medico', sanitize_text_field( $my_field_name ) );
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_checkout_field_in_admin_order_meta', 10, 1 );
function display_custom_checkout_field_in_admin_order_meta($order){
echo '<p><strong>'.__('Codice Medico').':</strong> ' . get_post_meta( $order->id, 'Codice Medico', true ) . '</p>';
}
/**
* 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', 'posters' );
$has_cat = false;
$has_other_cat = false;
// Iterating through each cart items (products)
foreach(WC()->cart->get_cart() as $cart_item){
// Getting the item product categories
$item_categories = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );
foreach($item_categories as $item_category){
if(in_array($item_category->slug, $categories))
$has_cat = true;
else
$has_other_cat = true;
}
}
// If one of your defined categories is in the cart item
// and not the others categories, we unset the field
if ( $has_cat && !$has_other_cat ) {
unset( $fields['billing']['my_field_name'] );
}
return $fields;
}
/**
*将该字段添加到签出
*/
添加_过滤器('woocommerce_checkout_fields'、'custom_override_checkout_fields');
//我们的挂钩函数-$字段通过过滤器传递!
函数自定义\覆盖\签出\字段($fields){
$fields['billing']['my_field_name']=数组(
“标签”=>“‘Codice medico’、‘woocommerce’”,
“占位符”=>\u x('Inserisci il codice del tuo medico'、'placeholder'、'WOOCOMERCE'),
“必需”=>true,
'class'=>array('form-row-wide'),
“清除”=>true
);
返回$fields;
}
/**
*处理结帐
*/
添加_操作('woocmerce_checkout_process'、'custom_checkout_field_process');
函数自定义\签出\字段\进程(){
//检查是否已设置,如果未设置,则添加错误。
如果(!$\u POST['my\u field\u name'])
wc_添加通知(________________________________________________________;
}
/**
*使用字段值更新订单元
*/
添加操作('woocommerce\u checkout\u update\u order\u meta'、'custom\u checkout\u field\u update\u order\u meta');
函数自定义\签出\字段\更新\订单\元($order\ id){
$my_field_name=$\u POST['my_field_name'];
如果(!空($my_field_name))
更新\u post\u meta(