Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在结帐页面上获取购物车中特定项目的数量_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 在结帐页面上获取购物车中特定项目的数量

Php 在结帐页面上获取购物车中特定项目的数量,php,wordpress,woocommerce,Php,Wordpress,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 ) {

我试图在结帐屏幕中显示自定义字段,这些字段有两个条件

  • 如果客户的购物车中有产品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\u form\u字段(),前提是该产品位于客户的购物车中

    现在,我需要做的是根据购物车中每个产品1769或1770的数量显示一定数量的woocommerce表单字段()

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

    在任何给定客户的购物车中,每个产品最多只能添加四个,因此将每个表单字段包装在一个if()中并不是什么大问题,该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\u=$values['quantity']
    到购物车中第一个
    函数conditional\u product\u中的
    foreach()。当我检查
    if(设置($qty\u in\u 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;
    
        }
    }
    
    ?>
    
    if(
    在_数组('woocommerce/woocommerce.php'中,应用_过滤器('active_plugins',get_选项('active_plugins'))&&
    在_数组('wootickets/wootickets.php'中,应用_过滤器('active_plugins',get_选项('active_plugins'))
    ) {
    /**
    *将该字段添加到签出
    **/
    添加行动(“订单注释后添加商业信息”、“与会者详细信息”);
    功能wt_与会者_详细信息($checkout){
    $attendee_count=wt_count_attendes();
    如果($attender\u count>0){
    echo“”;//关闭帐单地址部分以创建新的字段组
    echo“”;//从2个帐单地址的div自动关闭-
    echo“”。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
    对于($n=1;$n“文本”,
    'class'=>array('form-row form-row first'),
    “label'=>”(名称),
    “占位符”=>(名称),
    “必需”=>true,
    ),$checkout->get_value('attendee_name.$n));
    woocommerce_表单_字段('attendee_email_uu')$n,数组(
    '类型'=>'文本',
    'class'=>array('form-row validate email'),
    “标签”=>(电子邮件),
    “占位符”=>(电子邮件),
    “必需”=>true,
    ),$checkout->get_value('attendee_email.$n));
    woocommerce_表单_字段('attendee_phone_u')。$n,数组(
    '类型'=>'文本',
    'class'=>array('form-row form-row last'),
    “标签”=>“‘电话’”,
    “占位符”=>”,
    ),$checkout->get_value('attendee_phone.$n));
    }
    回声“
    #与会者详细信息。表格行{
    浮动:左;
    保证金权利:2%;
    宽度:31%;
    }
    #与会者详细信息。表格最后一行{
    右边距:0;
    }
    ";
    }
    //回声“与会者:”.$attendee\u计数;
    }
    /**
    *处理结帐
    **/
    添加行动(“商业、结帐、过程”、“与会者、字段、过程”);
    函数wt_与会者_字段_流程(){
    全球商业;
    $attendee_count=wt_count_attendes();
    对于($n=1;$n添加错误(uuuu('请填写与会者详细信息'));
    }
    }
    /**
    *使用字段值更新订单元
    **/
    添加动作(“woocommerce\u checkout\u update\u order\u meta”、“wt\u Attender\u update\u order\u meta”);
    功能wt\u与会者\u更新\u订单\u元($order\u id){
    $attendee_count=wt_count_attendes();
    对于($n=1;$n购物车->获取购物车())>0):
    foreach($woocommerce->cart->get_cart()作为$item_id=>$value):
    $_product=$values['data'];
    如果($\u product->exists()&&$values['quantity']>0):
    如果(获取发布元数据($\u产品->id,“\u部落\u活动的门票”)>0)
    $attendee_count+=$value['quantity'];
    endif;
    endforeach;
    endif;
    返回$attender\u count;
    }
    }
    ?>
    
    我知道这是一个老问题,但我想发布一个新答案,使用“事件日历PRO”v3.11和“事件日历:WooCommerce Tickets”v3.11插件,从2015年8月起生效

    此代码基于user1664798的答案,修改后执行以下操作:

    /**
    * 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...
        }
    }
    
    • 更改了错误的命名方式w/新插件版本
    • 显示每个产品的每张票的与会者字段(因此,如果用户购买了事件“A”的2张票和事件“B”的3张票,他们将看到5个字段-前2个字段将标记为事件“A”,最后3个字段将标记为事件“B”)
    • 向WooCommerce电子邮件中添加与会者
    • 将与会者添加到签出确认页面
    • 将与会者添加到“活动日历:WooCommerce票证”插件电子邮件
    添加到functions.php文件:

    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();
    
            foreach($attendee_count as $event_name => $event_info) {
                $event_slug = $event_info['slug'];
                $attendee_quantity = $event_info['quantity'];
                echo '<hr><h3>'.$event_name.__(' Attendee Information').'</h3>';
                for($n = 1; $n <= $attendee_quantity; $n++ ) {
                    woocommerce_form_field( $event_slug.'_attendee_name_'.$n, array(
                        'type'          => 'text',
                        'class'         => array(''),
                        'label'         => __("Attendee #$n Name"),
                        'placeholder'   => __(''),
                        'required'      => true,
                    ), $checkout->get_value( $event_slug.'_attendee_name_'.$n ));
    
                    woocommerce_form_field( $event_slug.'_attendee_email_'.$n, array(
                        'type'          => 'text',
                        'class'         => array('form-row-first'),
                        'label'         => __("Attendee #$n Email"),
                        'placeholder'       => __(''),
                    ), $checkout->get_value( $event_slug.'_attendee_email_'.$n ));
    
                    woocommerce_form_field( $event_slug.'_attendee_phone_'.$n, array(
                        'type'          => 'text',
                        'class'         => array('form-row-last'),
                        'label'         => __("Attendee #$n Phone"),
                        'placeholder'   => __(''),
                    ), $checkout->get_value( $event_slug.'_attendee_phone_'.$n ));
                }
            }
        }
    
        /**
         * Process the checkout
         **/
        add_action('woocommerce_checkout_process', 'wt_attendee_fields_process');
        function wt_attendee_fields_process() {
            global $woocommerce;
            $attendee_count = wt_count_attendees();
    
            foreach($attendee_count as $event_name => $event_info) {
                $event_slug = $event_info['slug'];
                $attendee_quantity = $event_info['quantity'];
                for ($n = 1; $n <= $attendee_quantity; $n++) {
                    if (!$_POST[$event_slug.'_attendee_name_' . $n])
                        $error = true;
                    break;
                }
            }
    
            if($error) {
                wc_add_notice( __( 'Please complete the attendee details.', 'woocommerce' ), 'error' );
            }
        }
    
        /**
         * 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();
            foreach($attendee_count as $event_name => $event_info) {
                $event_slug = $event_info['slug'];
                $attendee_quantity = $event_info['quantity'];
                for ($n = 1; $n <= $attendee_quantity; $n++) {
                    if ($_POST[$event_slug.'_attendee_name_' . $n]) update_post_meta($order_id, "$event_name Attendee #$n Name", esc_attr($_POST[$event_slug.'_attendee_name_' . $n]));
                    if ($_POST[$event_slug.'_attendee_email_' . $n]) update_post_meta($order_id, "$event_name Attendee #$n Email", esc_attr($_POST[$event_slug.'_attendee_email_' . $n]));
                    if ($_POST[$event_slug.'_attendee_phone_' . $n]) update_post_meta($order_id, "$event_name Attendee #$n Phone", esc_attr($_POST[$event_slug.'_attendee_phone_' . $n]));
                }
            }
        }
    
        function wt_count_attendees() {
            global $woocommerce;
            $attendee_count = array();
    
            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[$_product->post->post_title] = array('slug' => $_product->post->post_name, 'quantity' => $values['quantity']);
                    endif;
                endforeach;
            endif;
    
            return $attendee_count;
        }
    
        /**
         * Add attendees to email
         */
        add_action('woocommerce_email_order_meta', 'wt_email_order_meta');
        function wt_email_order_meta($order) {
            $orderMeta = get_post_meta($order->id);
            $hasCustom = false;
    
            array_walk($orderMeta, function($value, $key) use(&$hasCustom) { if($key[0] != "_") $hasCustom = true; });
    
            if($hasCustom) {
                echo '<h2 style="color: #557da1; display: block; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif; font-size: 18px; font-weight: bold; line-height: 130%; margin: 16px 0 8px; text-align: left;">'.__( 'Attendee Details', 'woocommerce' ).'</h2>';
                foreach ($orderMeta as $key => $value) {
                    if ($key[0] == "_" || !substr_count($key, "Attendee #")) continue; // skip internal meta
                    echo '<p style="margin: 0 0 16px;"><strong>' . esc_attr($key) . ':</strong> ' . esc_attr($value[0]) . '</p>';
                }
            }
        }
    
        /**
         * Add attendees to order confirmation page
         */
        add_action('woocommerce_order_details_after_order_table', 'wt_order_details_after_order_table');
        function wt_order_details_after_order_table($order) {
            $orderMeta = get_post_meta($order->id);
            $hasCustom = false;
    
            array_walk($orderMeta, function($value, $key) use(&$hasCustom) { if($key[0] != "_") $hasCustom = true; });
    
            if($hasCustom) {
                echo "<header><h2>".__( 'Attendee Details', 'woocommerce' )."</h2></header>";
                echo '<table class="shop_table order_details table table-simple-body-headers"><tbody>';
                foreach ($orderMeta as $key => $value) {
                    if ($key[0] == "_" || !substr_count($key, "Attendee #")) continue; // skip internal meta
                    echo '<tr>';
                    echo '  <td class="product-name"><strong>'.esc_attr($key).'</strong></td>';
                    echo '  <td>'.esc_attr($value[0]).'</td>';
                    echo '</tr>';
                }
                echo '</tbody></table>';
            }
        }
    
        /**
         * Add attendees to tickets page
         */
        add_action('tribe_tickets_ticket_email_bottom', 'wt_tribe_tickets_ticket_email_bottom');
        function wt_tribe_tickets_ticket_email_bottom($tickets) {
            $ticket = $tickets[0];
            $orderMeta = get_post_meta($ticket['order_id']);
            $hasCustom = false;
    
            array_walk($orderMeta, function($value, $key) use(&$hasCustom) { if($key[0] != "_") $hasCustom = true; });
    
            if($hasCustom) {
                echo '<table class="inner-wrapper" border="0" cellpadding="0" cellspacing="0" width="620" bgcolor="#f7f7f7" style="margin:0 auto !important; width:620px; padding:0;">';
                echo '  <tr>';
                echo '      <td valign="top" class="ticket-content" align="left" width="580" border="0" cellpadding="20" cellspacing="0" style="padding:20px; background:#f7f7f7;">';
                echo '          <table border="0" cellpadding="0" cellspacing="0" width="100%" align="center">';
                echo '              <tr>';
                echo '                  <td valign="top" align="center" width="100%" style="padding: 0 !important; margin:0 !important;">';
                echo '                      <h2 style="color:#0a0a0e; margin:0 0 10px 0 !important; font-family: \'Helvetica Neue\', Helvetica, sans-serif; font-style:normal; font-weight:700; font-size:28px; letter-spacing:normal; text-align:left;line-height: 100%;">';
                echo '                          <span style="color:#0a0a0e !important">Attendee Information</span>';
                echo '                      </h2>';
                echo '                  </td>';
                echo '              </tr>';
                echo '          </table>';
                echo '          <table class="whiteSpace" border="0" cellpadding="0" cellspacing="0" width="100%">';
                echo '              <tr>';
                echo '                  <td valign="top" align="left" width="100%" height="30" style="height:30px; background:#f7f7f7; padding: 0 !important; margin:0 !important;">';
                echo '                      <div style="margin:0; height:30px;"></div>';
                echo '                  </td>';
                echo '              </tr>';
                echo '          </table>';
                echo '          <table class="ticket-details" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">';
                foreach ($orderMeta as $key => $value) {
                    if ($key[0] == "_" || !substr_count($key, "Attendee #")) continue; // skip internal meta
                    echo '              <tr>';
                    echo '                  <td class="ticket-details" valign="top" align="left" style="padding: 0; margin:0 !important;">';
                    echo '                      <h6 style="color:#909090 !important; margin:0 0 10px 0; font-family: \'Helvetica Neue\', Helvetica, sans-serif; text-transform:uppercase; font-size:13px; font-weight:700 !important;">'.esc_attr($key).'</h6>';
                    echo '                  </td>';
                    echo '                  <td class="ticket-details" valign="top" align="left" width="10" style="padding: 0; width:10px; margin:0 !important;">';
                    echo '                      &nbsp;';
                    echo '                  </td>';
                    echo '                  <td class="ticket-details" valign="top" align="left" style="padding: 0; margin:0 !important;">';
                    echo '                      <span style="color:#0a0a0e !important; font-family: \'Helvetica Neue\', Helvetica, sans-serif; font-size:15px;">'.esc_attr($value[0]).'</span>';
                    echo '                  </td>';
                    echo '              </tr>';
                }
                echo '          </table>';
                echo '      </td>';
                echo '  </tr>';
                echo '</table>';
            }
        }
    }
    
    if(在数组('woocommerce/woocommerce.php'中),应用