首先,您使用的条件函数代码确实很旧、过时并且不管用当购物车商品是产品变体时(对于可变产品也是如此)。下面是紧凑且有效的条件函数......
它可以使用任何产品类别术语 ID、slug、名称或值数组:
function is_product_cat_in_cart( $categories ) {
foreach ( WC()->cart->get_cart() as $cart_item ) {
if (has_term ( $categories, 'product_cat', $cart_item['product_id'] ) )
return true;
}
return false;
}
现在,您的其余代码有很多错误或很小的错误。
它还使用过时或已弃用的挂钩,例如:
-
woocommerce_checkout_update_order_meta
取而代之的是一个非常合适的钩子。
-
woocommerce_email_order_meta_keys
很长时间以来已被弃用。
您还可以将一些代码合并到相同的挂钩函数中。
您不需要到处都使用条件函数。它只是结账字段条件显示所需要的。
这是您重新访问的代码(适用于 woocommerce 版本 3 及更高版本):
// Add fields for Emergency Contact & Medical Information to the checkout page
add_action('woocommerce_after_order_notes', 'customise_checkout_field', 20, 1 );
function customise_checkout_field( $checkout ){
$domain = 'woocommerce';
// There is a class in the cart so show additional fields
if ( is_product_cat_in_cart( 'class' ) ):
echo '<div id="customise_checkout_field">
<h3>' . __( 'Emergency Contact & Medical Information', $domain ) . '</h3>';
woocommerce_form_field( 'emergency_contact', array(
'type' => 'text',
'class' => array( 'emergency-contact form-row-wide' ),
'label' => __( 'Emergency Contact', $domain ) ,
'placeholder' => __( 'Please enter first & last name', $domain ),
'required' => true,
), $checkout->get_value('emergency_contact') );
woocommerce_form_field( 'emergency_contact_relationship', array(
'type' => 'text',
'class' => array( 'emergency-contact-relationship form-row-wide' ),
'label' => __( 'What is your relationship with this person?', $domain ),
'placeholder' => __( 'Example: Mother', $domain ) ,
'required' => true,
), $checkout->get_value('emergency_contact_relationship') );
woocommerce_form_field( 'emergency_contact_phone', array(
'type' => 'text',
'class' => array( 'emergency-contact-phone form-row-wide' ),
'label' => __( 'What is their phone number?', $domain ),
'placeholder' => __( '(555) 555-5555', $domain ),
'required' => true,
), $checkout->get_value('emergency_contact_phone') );
woocommerce_form_field( 'medical_medicine', array(
'type' => 'textarea',
'class' => array( 'medical-medicine form-row-wide' ) ,
'label' => __( 'Do you have any medical conditions and are you taking any medications we need to be aware of?', $domain ),
'placeholder' => __( 'If not please write in "none"', $domain ),
'required' => true,
) , $checkout->get_value('medical_medicine') );
echo '</div>';
endif;
}
// Add custom checkboxes to woocommerce checkout page for Photo Release, Mailing List & Release of Liability
add_action( 'woocommerce_review_order_before_submit', 'custom_checkout_fields' );
function custom_checkout_fields() {
$checkout = WC()->checkout;
$domain = 'woocommerce';
echo '<div id="custom_checkout_fields">
<h3>'.__( 'Mailing Lists', $domain ).'</h3>
<p>'.__( 'Mailing List boilerplate', $domain ).'</p>';
woocommerce_form_field( 'mailing_consent', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __( 'Please add me to Nonprofit\'s electronic and paper mailing lists.', $domain ),
'required' => false,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), $checkout->get_value( 'mailing_consent' ) );
// There is a class in the cart so show additional fields
if ( is_product_cat_in_cart( 'class' ) ):
echo '<h3>'.__( 'Photo Release', $domain ).'</h3>
<p>'.__( 'Photo Release Boilerplate', $domain ).'</p>';
woocommerce_form_field( 'photo_consent', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __( 'I agree to the Photo Release as outlined above.', $domain ),
'required' => false,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), $checkout->get_value( 'photo_consent' ) );
echo '<h3>'.__( 'Release of Liability', $domain ).'</h3>
<p>'.__( 'Release of Liability Boilerplate', $domain ).'</p>';
woocommerce_form_field( 'liability_release', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __( 'I agree to the Photo Release as outlined above.', $domain ),
'required' => true,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), $checkout->get_value( 'liability_release' ) );
endif;
echo '</div>';
}
// Custom checkout fields validation
add_action('woocommerce_checkout_process', 'custom_checkout_fields_process');
function custom_checkout_fields_process() {
$domain = 'woocommerce';
if ( isset($_POST['emergency_contact']) && empty($_POST['emergency_contact']) )
wc_add_notice( __( 'Please list an emergency contact.', $domain ) , 'error' );
if ( isset($_POST['emergency_contact_relationship']) && empty($_POST['emergency_contact']) )
wc_add_notice( __( 'Please indicate your relationship with your emergency contact.', $domain ), 'error' );
if ( isset($_POST['emergency_contact_phone']) && empty($_POST['emergency_contact']) )
wc_add_notice( __( 'Please list a phone number for your emergency contact.', $domain ), 'error' );
if ( isset($_POST['medical_medicine']) && empty($_POST['emergency_contact']) )
wc_add_notice( __( 'Please list any medications or write in "none".', $domain ), 'error' );
// Other checkout fields
if ( ! isset( $_POST['liability_release'] ) && ! $_POST['liability_release'] && isset($_POST['photo_consent']) )
wc_add_notice( __( 'You must agree to the Release of Liability to register for this class. Please contact us with any questions.', $domain ), 'error' );
}
// Save custom checkout fields in the order meta data
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_fields_in_order_meta_data', 20, 2 );
function custom_checkout_fields_in_order_meta_data( $order, $data ) {
if ( isset($_POST['emergency_contact']) && ! empty($_POST['emergency_contact']) )
$order->update_meta_data( 'emergency_contact', sanitize_text_field($_POST['emergency_contact']) );
if ( isset($_POST['emergency_contact_relationship']) && ! empty($_POST['emergency_contact_relationship']) )
$order->update_meta_data( 'emergency_contact_relationship', sanitize_text_field($_POST['emergency_contact_relationship']) );
if ( isset($_POST['emergency_contact_phone']) && ! empty($_POST['emergency_contact_phone']) )
$order->update_meta_data( 'emergency_contact_phone', sanitize_text_field($_POST['emergency_contact_phone']) );
if ( isset($_POST['medical_medicine']) && ! empty($_POST['medical_medicine']) )
$order->update_meta_data( 'medical_medicine', sanitize_text_field($_POST['medical_medicine']) );
if ( isset($_POST['mailing_consent']) && ! empty($_POST['mailing_consent']) )
$order->update_meta_data( 'mailing_consent', '1' );
if ( isset( $_POST['photo_consent']) && ! empty($_POST['photo_consent']) )
$order->update_meta_data( 'photo_consent', '1' );
if ( isset( $_POST['liability_release']) && ! empty($_POST['liability_release']) )
$order->update_meta_data( 'liability_release', '1' );
}
// Add the emergency contact fields to email notifications
add_filter( 'woocommerce_email_order_meta_fields', 'custom_checkout_field_email_order_meta', 20, 3 );
function custom_checkout_field_email_order_meta( $fields, $sent_to_admin, $order ) {
$domain = 'woocommerce';
if( ! $order->get_meta( 'emergency_contact' ) )
return $fields; // Exit if not set in the order
echo '<h2>'.__( 'Emergency Contact & Medical Information', $domain ).'</h2>';
$fields[] = array( 'label' => __( 'Emergency contact', $domain ),
'value' => $order->get_meta( 'emergency_contact' ) );
$fields[] = array( 'label' => __( 'Emergency Contact Relationship', $domain ),
'value' => $order->get_meta( 'emergency_contact_relationship' ) );
$fields[] = array( 'label' => __( 'Emergency Contact Phone', $domain ),
'value' => $order->get_meta( 'emergency_contact_phone' ) );
$fields[] = array( 'label' => __( 'Medical Conditions & Medications', $domain ),
'value' => $order->get_meta( 'medical_medicine' ) );
return $fields;
}
// Display some custom checkout fields in Order edit pages
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 20, 1 );
function display_custom_field_on_order_edit_pages( $order ){
$domain = 'woocommerce';
$billing_name = $order->get_billing_first_name().' '.$order->get_billing_last_name();
if( $order->get_meta('mailing_consent') )
echo '<p>' . $billing_name . __( ' agreed to the be added to Nonprofit\'s mailing lists.', $domain ).'</p>';
if( $order->get_meta('photo_consent') )
echo '<p>' . $billing_name . __( ' agreed to the Photo Release.', $domain ).'</p>';
if( $order->get_meta('liability_release') )
echo '<p>' . $billing_name . __( ' agreed to the Release of Liability.', $domain ).'</p>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
结论:如果某些代码在 function.php 文件中不起作用,那么它在插件中也不会更好地工作。但如果您愿意,可以将其添加到插件中。