脚本宝典收集整理的这篇文章主要介绍了php – Woocommerce在结帐页面上获取购物车中特定商品的数量,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
>如果产品#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','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))时,它没有设置.
我觉得我很亲密,但是我无法弄清楚我错过了什么.任何帮助,将不胜感激.
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; } } ?>
以上是脚本宝典为你收集整理的php – Woocommerce在结帐页面上获取购物车中特定商品的数量全部内容,希望文章能够帮你解决php – Woocommerce在结帐页面上获取购物车中特定商品的数量所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。