WordPress教程

Woocommerce 结帐页面上获取购物车中特定商品的数量

阿里云

我试图在结帐屏幕中显示自定义字段,这些字段以 2 件事为条件。

>如果产品#1769 和/或产品#1770 在客户的购物车中。
>当前购物车中的#1769 和/或#1770 的数量将决定要显示的自定义字段的数量。

也想出现在这里?联系我们
创客主机

现在我已经通过以下方式处理了#1:

  1. /**
  2. * Check to see what is in the cart
  3. *
  4. * @param $product_id
  5. *
  6. * @return bool
  7. */
  8. function conditional_product_in_cart( $product_id ) {
  9.     //Check to see if user has product in cart
  10.     global $woocommerce;
  11.  
  12.     $check_in_cart = false;
  13.  
  14.     foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
  15.         $_product = $values['data'];
  16.  
  17.         if ( $_product->id === $product_id ) {
  18.             $check_in_cart = true;
  19.         }       
  20.     }
  21.     return $check_in_cart;
  22. }
  23. function checkout_register_names( $checkout ) {
  24.  
  25. $check_in_cart = conditional_product_in_cart(1769); 
  26.  
  27.     // Product id 1769 is in cart so show custom fields
  28.     if ($check_in_cart === true ) {
  29.     // Display custom fields for 1769...
  30.     woocommerce_form_field( 'golf_field_one', array(
  31.     'type'          => 'text',
  32.     'class'         => array('my-field-class form-row-wide'),
  33.     'label'         => __('Golfer #1'),
  34.     'placeholder'       => __('Name'),
  35.     ), $checkout->get_value( 'golf_field_one' ));
  36.     woocommerce_form_field( 'golf_field_two', array(
  37.     'type'          => 'text',
  38.     'class'         => array('my-field-class form-row-wide'),
  39.     'label'         => __('Golfer #2'),
  40.     'placeholder'       => __('Name'),
  41.     ), $checkout->get_value( 'golf_field_two' ));
  42.     //etc...
  43.     }
  44. $check_in_cart = conditional_product_in_cart(1770); 
  45.  
  46.     // Product id 1770 is in cart so show custom fields
  47.     if ($check_in_cart === true ) {
  48.     // Display custom fields for 1770...
  49.     woocommerce_form_field( 'dinner_field_one', array(
  50.     'type'          => 'text',
  51.     'class'         => array('my-field-class form-row-wide'),
  52.     'label'         => __('Dinner Name #1'),
  53.     'placeholder'       => __('Name'),
  54.     ), $checkout->get_value( 'dinner_field_one' ));
  55.     woocommerce_form_field( 'dinner_field_two', array(
  56.     'type'          => 'text',
  57.     'class'         => array('my-field-class form-row-wide'),
  58.     'label'         => __('Dinner Name #2'),
  59.     'placeholder'       => __('Name'),
  60.     ), $checkout->get_value( 'dinner_field_two' ));
  61.     //etc...
  62.     }
  63. }

以上代码将有条件地显示我在每个产品下设置的所有 woocommerce_form_field(),仅当该产品位于客户的购物车中时。现在我需要做的是根据购物车中每种产品#1769 或#1770 的数量显示一定数量的 woocommerce_form_field()。

因此如果有两(2)个产品#1769,,则应显示两个字段。如果有两(2)个产品#1769 和一个(1)产品#1770,则应显示三个总字段(两个用于产品#1769,一个用于产品#1770)。

在任何给定客户的购物车中,每个产品最多只会添加四个,因此将每个表单字段包装在 if()中并检查以下内容并不是一件大事:

  1. if([quantity of product 1769] >= 1) {
  2.     show first woocommerce_form_field()
  3. }
  4. if([quantity of product 1769 >= 2) {
  5.     show second woocommerce_form_field()
  6. } //etc... 
  7. // Repeat for product 1770...

我尝试添加$qty_in_cart = $values [‘quantity’];在第一个函数 conditional_product_in_cart 中的 foreach(),但似乎不想给我任何东西。当我检查是否(isset($qty_in_cart))时,它没有设置。

我觉得我很亲密,但是我无法弄清楚我错过了什么,任何帮助将不胜感激。我正在执行同样的任务,我在 WooTickets 和 WooCommerce 上使用的代码片段可能对您有所帮助:

  1. if (    
  2.         in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && 
  3.         in_array( 'wootickets/wootickets.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
  4.     ) {
  5.     /**
  6.      * Add the field to the checkout
  7.      **/
  8.     add_action('woocommerce_after_order_notes', 'wt_attendee_details');
  9.     function wt_attendee_details( $checkout ) {
  10.  
  11.         $attendee_count = wt_count_attendees();
  12.  
  13.         if($attendee_count > 0) {
  14.             echo "</div></div>"; //close the Billing Address section to create new group of fields
  15.             echo "<div id='attendee_details'><div>"; //automatically be closed from 2 Billing Address's div - </div></div>
  16.             echo '<h3>'.__('All Event Attendees and/or clients who are ordering DVDs, please add your name and email again.').'</h3>';
  17.             for($n = 1; $n <= $attendee_count; $n++ ) {
  18.                 woocommerce_form_field( 'attendee_name_'.$n, array(
  19.                     'type'          => 'text',
  20.                     'class'         => array('form-row form-row-first'),
  21.                     'label'         => __('Name'),
  22.                     'placeholder'   => __('name'),
  23.                     'required'      => true,
  24.                 ), $checkout->get_value( 'attendee_name_'.$n ));
  25.                 woocommerce_form_field( 'attendee_email_'.$n, array(
  26.                     'type'          => 'text',
  27.                     'class'         => array('form-row validate-email'),
  28.                     'label'         => __('Email'),
  29.                     'placeholder'       => __('email'),
  30.                     'required'      => true,
  31.                 ), $checkout->get_value( 'attendee_email_'.$n ));
  32.                 woocommerce_form_field( 'attendee_phone_'.$n, array(
  33.                     'type'          => 'text',
  34.                     'class'         => array('form-row form-row-last'),
  35.                     'label'         => __('Phone'),
  36.                     'placeholder'   => __(''),
  37.                 ), $checkout->get_value( 'attendee_phone_'.$n ));
  38.             }
  39.             echo "<style type='text/css'>
  40.                         #attendee_details .form-row {
  41.                             float: left;
  42.                             margin-right: 2%;
  43.                             width: 31%;
  44.                         }
  45.                         #attendee_details .form-row-last {
  46.                             margin-right: 0;
  47.                         }
  48.                     </style>";
  49.         }
  50.         //echo "Attendees: " . $attendee_count;
  51.     }
  52.  
  53.     /**
  54.      * Process the checkout
  55.      **/
  56.     add_action('woocommerce_checkout_process', 'wt_attendee_fields_process');
  57.  
  58.     function wt_attendee_fields_process() {
  59.         global $woocommerce;
  60.         $attendee_count = wt_count_attendees();
  61.  
  62.         for($n = 1; $n <= $attendee_count; $n++ ) {
  63.             if (!$_POST['attendee_email_'.$n] || !$_POST['attendee_name_'.$n])
  64.                 $error = true;
  65.                 break;
  66.         }
  67.         if($error) {
  68.             $woocommerce->add_error( __('Please complete the attendee details.') );
  69.         }
  70.  
  71.     }
  72.  
  73.     /**
  74.      * Update the order meta with field value
  75.      **/
  76.     add_action('woocommerce_checkout_update_order_meta', 'wt_attendee_update_order_meta');
  77.  
  78.     function wt_attendee_update_order_meta( $order_id ) {
  79.  
  80.         $attendee_count = wt_count_attendees();
  81.         for($n = 1; $n <= $attendee_count; $n++ ) {
  82.             if ($_POST['attendee_name_'.$n]) update_post_meta( $order_id, $n.' Attendee Name', esc_attr($_POST['attendee_name_'.$n]));
  83.             if ($_POST['attendee_email_'.$n]) update_post_meta( $order_id, $n.' Attendee Email', esc_attr($_POST['attendee_email_'.$n]));
  84.             if ($_POST['attendee_phone_'.$n]) update_post_meta( $order_id, $n.' Attendee Phone', esc_attr($_POST['attendee_phone_'.$n]));
  85.         }
  86.  
  87.     }
  88.  
  89.     function wt_count_attendees() {
  90.  
  91.         global $woocommerce;
  92.         $attendee_count = 0;
  93.  
  94.         if (sizeof($woocommerce->cart->get_cart())>0) :
  95.             foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
  96.                 $_product = $values['data'];
  97.                 if ($_product->exists() && $values['quantity']>0) :
  98.                     if (get_post_meta($_product->id, '_tribe_wooticket_for_event') > 0)
  99.                         $attendee_count += $values['quantity'];
  100.                 endif;
  101.             endforeach;
  102.         endif;
  103.  
  104.         return $attendee_count;
  105.  
  106.     }
  107. }

Woocommerce 结帐页面上获取购物车中特定商品的数量

已有 192 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!