php – Woocommerce在结帐页面上获取购物车中特定商品的数量
我试图在结帐屏幕中显示自定义字段,这些字段以2件事为条件.
>如果产品#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','label' => __('Golfer #2'),$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','label' => __('Dinner Name #1'),$checkout->get_value( 'dinner_field_one' )); woocommerce_form_field( 'dinner_field_two','label' => __('Dinner Name #2'),$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’];在第一个函数conditional_product_in_cart中的foreach(),但似乎不想给我任何东西.当我检查是否(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',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,'class' => array('form-row validate-email'),'label' => __('Email'),'placeholder' => __('email'),$checkout->get_value( 'attendee_email_'.$n )); woocommerce_form_field( 'attendee_phone_'.$n,'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; } } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |