我正在为一个非营利机构设计网站,使用Woocommerce售卖课程和活动门票。当有人报名课程时,需要填写紧急联系信息并同意责任豁免协议。当他们购买活动门票时,非营利机构不需要紧急联系信息或责任豁免协议。所以...他们希望这些字段仅在购买课程门票时出现在Woocommerce结账页面上。明白吗?
几个月前,当他们第一次将班级添加到网站时,我已经想出了如何添加自定义字段和自定义责任豁免协议。我在Woocommerce中创建了“班级”产品类别,并编写了一个函数来检测购物车中是否有该类别的任何产品,以便可以有条件地显示这些字段。
所有这些功能都在我的functions.php文件中,现在我正在每个函数中运行条件语句来检查“班级”类别。我需要帮助学习如何检查“班级”类别一次,然后运行显示字段、验证字段、将数据添加到数据库并生成新订单电子邮件的函数。明白吗?
以下是我当前拥有的内容:
对于每个函数进行重复。我知道这不是高效的方法,但我不确定如何修复它。我想做的是:
感谢您能提供的任何想法和帮助。
几个月前,当他们第一次将班级添加到网站时,我已经想出了如何添加自定义字段和自定义责任豁免协议。我在Woocommerce中创建了“班级”产品类别,并编写了一个函数来检测购物车中是否有该类别的任何产品,以便可以有条件地显示这些字段。
所有这些功能都在我的functions.php文件中,现在我正在每个函数中运行条件语句来检查“班级”类别。我需要帮助学习如何检查“班级”类别一次,然后运行显示字段、验证字段、将数据添加到数据库并生成新订单电子邮件的函数。明白吗?
以下是我当前拥有的内容:
// Add fields for Emergency Contact & Medical Information to the checkout page
add_action('woocommerce_after_order_notes', 'customise_checkout_field');
function customise_checkout_field($checkout)
{
// Check to see if there is a class in the cart
// function is at the end
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
echo '<div id="customise_checkout_field"><h3>' . __('Emergency Contact & Medical Information') . '</h3>';
woocommerce_form_field('emergency_contact', array(
'type' => 'text',
'class' => array(
'emergency-contact form-row-wide'
) ,
'label' => __('Emergency Contact') ,
'placeholder' => __('Please enter first & last name') ,
'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?') ,
'placeholder' => __('Example: Mother') ,
'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?') ,
'placeholder' => __('(555) 555-5555') ,
'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?') ,
'placeholder' => __('If not please write in "none"') ,
'required' => true,
) , $checkout->get_value('medical_medicine'));
echo '</div>';
}
}
// Process emergency contact fields
add_action('woocommerce_checkout_process', 'custom_checkout_fields_process');
function custom_checkout_fields_process() {
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
// if the field is set, if not then show an error message.
if (!$_POST['emergency_contact']) wc_add_notice(__('Please list an emergency contact.') , 'error');
if (!$_POST['emergency_contact_relationship']) wc_add_notice(__('Please indicate your relationship with your emergency contact.') , 'error');
if (!$_POST['emergency_contact_phone']) wc_add_notice(__('Please list a phone number for your emergency contact.') , 'error');
if (!$_POST['medical_medicine']) wc_add_notice(__('Please list any medications or write in "none".') , 'error');
}
}
// Add emergency contact information to the database
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta');
function custom_checkout_fields_update_order_meta($order_id) {
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
if (!empty($_POST['emergency_contact'])) {
update_post_meta($order_id, 'emergency_contact', sanitize_text_field($_POST['emergency_contact']));
}
if (!empty($_POST['emergency_contact_relationship'])) {
update_post_meta($order_id, 'emergency_contact_relationship', sanitize_text_field($_POST['emergency_contact_relationship']));
}
if (!empty($_POST['emergency_contact_phone'])) {
update_post_meta($order_id, 'emergency_contact_phone', sanitize_text_field($_POST['emergency_contact_phone']));
}
if (!empty($_POST['medical_medicine'])) {
update_post_meta($order_id, 'medical_medicine', sanitize_text_field($_POST['medical_medicine']));
}
}
}
// Add the emergency contact fields to order email
add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
echo '<h2>Emergency Contact & Medical Information:</h2>';
$keys['Emergency Contact'] = 'emergency_contact';
$keys['Emergency Contact Relationship'] = 'emergency_contact_relationship';
$keys['Emergency Contact Phone'] = 'emergency_contact_phone';
$keys['Medical Conditions & Medications'] = 'medical_medicine';
return $keys;
} // end class in cart condition
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
// Add custom checkboxes to woocommerce checkout page for Photo Release, Mailing List & Release of Liability
// add_action( 'woocommerce_after_order_notes', 'custom_checkout_fields' );
add_action( 'woocommerce_review_order_before_submit', 'custom_checkout_fields' );
function custom_checkout_fields() {
echo '<div id="custom_checkout_fields">
<h3>Mailing Lists</h3>
<p>Mailing List boilerplate';
woocommerce_form_field( 'mailing_consent', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Please add me to Nonprofit\'s electronic and paper mailing lists.'),
'required' => false,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), WC()->checkout->get_value( 'mailing_consent' ) );
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
echo '<h3>Photo Release</h3>
<p>Photo Release Boilerplate</p>';
woocommerce_form_field( 'photo_consent', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I agree to the Photo Release as outlined above.'),
'required' => false,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), WC()->checkout->get_value( 'photo_consent' ) );
echo '<h3>Release of Liability</h3>
<p>Release of Liability Boilerplate</p>';
woocommerce_form_field( 'liability_release', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I agree to the Photo Release as outlined above.'),
'required' => true,
'clear' => true,
'default' => 1 //This will pre-select the checkbox
), WC()->checkout->get_value( 'liability_release' ) );
} // end class in cart condition
echo '</div>';
}
// Show notice if customer doesn't check the Release of Liability checkbox
add_action( 'woocommerce_checkout_process', 'liability_release_not_given' );
function liability_release_not_given() {
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
if ( ! (int) isset( $_POST['liability_release'] ) ) {
wc_add_notice( __( 'You must agree to the Release of Liability to register for this class. Please contact us with any questions.' ), 'error' );
}
} // end class in cart condition
}
// Save the custom checkout field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['mailing_consent'] ) )
update_post_meta( $order_id, 'mailing_consent', $_POST['mailing_consent'] );
if ( ! empty( $_POST['photo_consent'] ) )
update_post_meta( $order_id, 'photo_consent', $_POST['photo_consent'] );
if ( ! empty( $_POST['liability_release'] ) )
update_post_meta( $order_id, 'liability_release', $_POST['liability_release'] );
}
/*-----------------------------------------------------------------------------------------------------------------------------------------------*/
// Display custom field results on the order edit page (backend)
// for various liability fields
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
$mailing_consent = get_post_meta( $order->get_id(), 'mailing_consent', true );
if( $mailing_consent == 1 )
echo '<p>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' agreed to the be added to Nonprofit\'s mailing lists.</p>';
$photo_consent = get_post_meta( $order->get_id(), 'photo_consent', true );
if( $photo_consent == 1 )
echo '<p>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' agreed to the Photo Release.</p>';
$liability_release = get_post_meta( $order->get_id(), 'liability_release', true );
if( $liability_release == 1 )
echo '<p>' . $order->billing_first_name . ' ' . $order->billing_last_name . ' agreed to the Release of Liability.</p>';
}
/**
* Check if Class is In cart
*
* https://wordimpress.com/create-conditional-checkout-fields-woocommerce/
* https://businessbloomer.com/woocommerce-check-product-category-cart/
*
* @param $product_id
*
* @return bool
*/
function is_conditional_product_in_cart( $category_name ) {
//Check to see if user has a class in their cart
global $woocommerce;
//flag no class in cart
$class_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
// if ( $_product->cat === $category_id ) {
// //class is in cart!
//$class_in_cart = true;
if (has_term ( $category_name, 'product_cat', $_product->get_id() ) ) {
//class is in cart!
$class_in_cart = true;
}
}
return $class_in_cart;
}
我可以看出这篇文章是从网上不同的来源拼凑而成的,我知道它有些混乱。目前,条件语句如下:
// Check to see if there is a class in the cart
$class_in_cart = is_conditional_product_in_cart( 'class' );
// There is a class in the cart so show additional fields
if ( $class_in_cart === true ) {
对于每个函数进行重复。我知道这不是高效的方法,但我不确定如何修复它。我想做的是:
- 测试属于“类别”类别的产品
- 如果是“类别”,则运行所有函数以显示和处理紧急联系人字段、照片发布和责任豁免字段
- 无论购物车中是否有“类别”,都会显示“加入邮件列表”协议并处理该字段。
感谢您能提供的任何想法和帮助。