我正在尝试在结帐屏幕中显示以两件事为条件的自定义字段。
- 如果产品 #1769 和/或产品 #1770 在客户的购物车中。
- 当前购物车中 #1769 和/或 #1770 的数量将决定要显示的自定义字段的数量。
现在我已经处理了上面的#1 问题:
/**
* Check to see what is in the cart
*
* @param $product_id
*
* @return bool
*/
function conditional_product_in_cart( $product_id ) {
//Check to see if user has product in cart
global $woocommerce;
$check_in_cart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id === $product_id ) {
$check_in_cart = true;
}
}
return $check_in_cart;
}
function checkout_register_names( $checkout ) {
$check_in_cart = conditional_product_in_cart(1769);
// Product id 1769 is in cart so show custom fields
if ($check_in_cart === true ) {
// Display custom fields for 1769...
woocommerce_form_field( 'golf_field_one', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Golfer #1'),
'placeholder' => __('Name'),
), $checkout->get_value( 'golf_field_one' ));
woocommerce_form_field( 'golf_field_two', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Golfer #2'),
'placeholder' => __('Name'),
), $checkout->get_value( 'golf_field_two' ));
//etc...
}
$check_in_cart = conditional_product_in_cart(1770);
// Product id 1770 is in cart so show custom fields
if ($check_in_cart === true ) {
// Display custom fields for 1770...
woocommerce_form_field( 'dinner_field_one', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Dinner Name #1'),
'placeholder' => __('Name'),
), $checkout->get_value( 'dinner_field_one' ));
woocommerce_form_field( 'dinner_field_two', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Dinner Name #2'),
'placeholder' => __('Name'),
), $checkout->get_value( 'dinner_field_two' ));
//etc...
}
}
只有当该产品在客户的购物车中时,上面的代码才会有条件地显示我在每个产品下设置的所有 woocommerce_form_field()。
现在,我需要做的是根据购物车中每种产品 #1769 或 #1770 的数量显示一定数量的 woocommerce_form_field() 。
因此,如果有两 (2) 个产品 #1769,则应该显示两个字段。如果有两 (2) 个产品 #1769 和一 (1) 个产品 #1770,则应该显示三个总字段(两个用于产品 #1769,一个用于产品 #1770)。
至多,每个给定客户的购物车中最多只会添加四个产品,因此将每个表单字段包装在一个 if() 中并没有什么大不了的,它会检查如下内容:
if([quantity of product 1769] >= 1) {
show first woocommerce_form_field()
}
if([quantity of product 1769 >= 2) {
show second woocommerce_form_field()
} //etc...
// Repeat for product 1770...
我尝试将 $qty_in_cart = $values['quantity'];
添加到第一个 function conditional_product_in_cart
中的 foreach()
,但是那似乎不想给我任何东西。当我检查 if (isset($qty_in_cart))
时,它没有设置。
我觉得我很接近,但就是想不通我错过了什么。任何帮助将不胜感激。
最佳答案
我肩负着同样的使命。 我在 WooTickets 和 WooCommerce 上使用的这段代码可能对您有所帮助:
if (
in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) &&
in_array( 'wootickets/wootickets.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
) {
/**
* Add the field to the checkout
**/
add_action('woocommerce_after_order_notes', 'wt_attendee_details');
function wt_attendee_details( $checkout ) {
$attendee_count = wt_count_attendees();
if($attendee_count > 0) {
echo "</div></div>"; //close the Billing Address section to create new group of fields
echo "<div id='attendee_details'><div>"; //automatically be closed from 2 Billing Address's div - </div></div>
echo '<h3>'.__('All Event Attendees and/or clients who are ordering DVDs, please add your name and email again.').'</h3>';
for($n = 1; $n <= $attendee_count; $n++ ) {
woocommerce_form_field( 'attendee_name_'.$n, array(
'type' => 'text',
'class' => array('form-row form-row-first'),
'label' => __('Name'),
'placeholder' => __('name'),
'required' => true,
), $checkout->get_value( 'attendee_name_'.$n ));
woocommerce_form_field( 'attendee_email_'.$n, array(
'type' => 'text',
'class' => array('form-row validate-email'),
'label' => __('Email'),
'placeholder' => __('email'),
'required' => true,
), $checkout->get_value( 'attendee_email_'.$n ));
woocommerce_form_field( 'attendee_phone_'.$n, array(
'type' => 'text',
'class' => array('form-row form-row-last'),
'label' => __('Phone'),
'placeholder' => __(''),
), $checkout->get_value( 'attendee_phone_'.$n ));
}
echo "<style type='text/css'>
#attendee_details .form-row {
float: left;
margin-right: 2%;
width: 31%;
}
#attendee_details .form-row-last {
margin-right: 0;
}
</style>";
}
//echo "Attendees: " . $attendee_count;
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'wt_attendee_fields_process');
function wt_attendee_fields_process() {
global $woocommerce;
$attendee_count = wt_count_attendees();
for($n = 1; $n <= $attendee_count; $n++ ) {
if (!$_POST['attendee_email_'.$n] || !$_POST['attendee_name_'.$n])
$error = true;
break;
}
if($error) {
$woocommerce->add_error( __('Please complete the attendee details.') );
}
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'wt_attendee_update_order_meta');
function wt_attendee_update_order_meta( $order_id ) {
$attendee_count = wt_count_attendees();
for($n = 1; $n <= $attendee_count; $n++ ) {
if ($_POST['attendee_name_'.$n]) update_post_meta( $order_id, $n.' Attendee Name', esc_attr($_POST['attendee_name_'.$n]));
if ($_POST['attendee_email_'.$n]) update_post_meta( $order_id, $n.' Attendee Email', esc_attr($_POST['attendee_email_'.$n]));
if ($_POST['attendee_phone_'.$n]) update_post_meta( $order_id, $n.' Attendee Phone', esc_attr($_POST['attendee_phone_'.$n]));
}
}
function wt_count_attendees() {
global $woocommerce;
$attendee_count = 0;
if (sizeof($woocommerce->cart->get_cart())>0) :
foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
$_product = $values['data'];
if ($_product->exists() && $values['quantity']>0) :
if (get_post_meta($_product->id, '_tribe_wooticket_for_event') > 0)
$attendee_count += $values['quantity'];
endif;
endforeach;
endif;
return $attendee_count;
}
}
?>
关于php - Woocommerce 在结帐页面上获取购物车中特定商品的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824303/