Session 添加到购物车方法后购物车为空

Session 添加到购物车方法后购物车为空,session,cookies,woocommerce,cart,Session,Cookies,Woocommerce,Cart,我正在为WC变量产品开发一项功能。这将是不同“级别”的机票,每一级别的价格不同。用户应该选择他想要的票证数量,在创建表单之后,他可以为每个票证(行)添加名称、地址和票证级别。所有这些数据都应该在发送到购物车并保存到订单之后 编辑2 大家好,又来了 我花了更多的时间在这方面,并进一步 我几乎做到了我想要的: 用户可以选择票数 用户可以为每张票证填写附加信息(姓名、地址、简历、票证级别) 所有这些行都将添加到购物车 所有这些数据都显示在购物车和收银台中 所有这些数据将发送至订单/电子邮件 现在我发现

我正在为WC变量产品开发一项功能。这将是不同“级别”的机票,每一级别的价格不同。用户应该选择他想要的票证数量,在创建表单之后,他可以为每个票证(行)添加名称、地址和票证级别。所有这些数据都应该在发送到购物车并保存到订单之后

编辑2

大家好,又来了

我花了更多的时间在这方面,并进一步

我几乎做到了我想要的:

  • 用户可以选择票数
  • 用户可以为每张票证填写附加信息(姓名、地址、简历、票证级别)
  • 所有这些行都将添加到购物车
  • 所有这些数据都显示在购物车和收银台中
  • 所有这些数据将发送至订单/电子邮件
  • 现在我发现了问题(当然,可能还有更多的问题)。但是,我一直都是以登录用户的身份在同一个浏览器窗口中进行此操作。 之后,我意识到,当我使用inkognito模式,或另一个没有为该站点存储会话或Cookie的浏览器时,在将项目添加到购物车后,购物车是空的

    当我试图第一次添加一些其他项目,是使用常规的WC添加到购物车按钮,它的工作,然后我可以添加我的票没有问题

    但当我尝试先添加门票时,购物车是空的。在我的主浏览器中,作为登录用户,我没有问题

    我对此做了一些研究,但我只是在尝试。我真的不明白这个问题,所以我又在浪费时间了

    如果有人能帮助我或指导我解决问题,我会很高兴

    谢谢你抽出时间

    下面是我的插件的代码:

    if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    
        if ( ! class_exists( 'UberMike' ) ) {
    
            class UberMike{
                private static $farba_vlastnost = 'attribute_pa_level-bezca';
                private static $attributes_values = array();
    
                public function __construct() {
                    self::$farba_vlastnost = self::$farba_vlastnost;
                    self::$attributes_values = self::$attributes_values;
                    // called only after woocommerce has finished loading
                    add_action( 'woocommerce_init', array( &$this, 'woocommerce_loaded' ) );
                    // called after all plugins have loaded
                    add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) );
                    //enqueue scripts and css style
                    add_action("wp_loaded", array( &$this, "enqueue_scripts") );
                    //add custom actions
                    //remove WC action completely
                    add_action('woocommerce_single_product_summary', function () {
                        remove_action('woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30);
                    }, -1000);
                    //add custom add to cart
                    add_action( 'woocommerce_variable_add_to_cart', array( $this, 'add_to_cart'), 30 );
                    //create collapsible section input number and button
                    add_action( 'woocommerce_variable_add_to_cart', array( $this, 'createCollapsibleSection') );
                    //hide collapsible section in header
                    add_action('wp_footer', array( $this, 'collapsibleSectionHeader') );
                    //boot session
                    // add_action('wp_head', array( $this, 'hook_cookie') );
                    add_action('wp_head', array( $this, 'boot_session') );
                    //display meta data to each item in cart anch checkout
                    add_filter( 'woocommerce_get_item_data', array( $this, 'ubermike_render_meta_on_cart_and_checkout'), 20, 2 );
                    //display grouped custom fields in checkout and order notes
                    add_action( 'woocommerce_checkout_create_order', array( $this, 'my_custom_checkout_field') );
                    add_action( 'woocommerce_after_order_notes', array( $this, 'my_custom_checkout_field') );
                    //display fields in order and email
                    add_action('woocommerce_checkout_create_order_line_item', array( $this, 'save_custom_order_item_meta_data'), 10, 4 );
                }
                /**
             * Take care of anything that needs woocommerce to be loaded.
             * For instance, if you need access to the $woocommerce global
             */
                public function woocommerce_loaded() {
                    // ...
                }
                /**
                 * Take care of anything that needs all plugins to be loaded
                 */
                public function plugins_loaded() {
                    // ...
                }
                //enqueue JS scripts and CSS style
                public function enqueue_scripts() {
                    wp_register_style( 'ubermike_css', plugins_url('assets/ubermike.css', __FILE__) );
                    wp_enqueue_style('ubermike_css');
    
                    wp_register_script( 'ubermike_js', plugins_url('assets/ubermike.js',__FILE__ ), array( 'jquery' ));
                    wp_enqueue_script('ubermike_js');
                }
    
                //add to cart replacement
                public function add_to_cart($allsets){
                    global $product, $post, $woocommerce;
    
                    //check product category
                    $is_ticket = false;
                    $terms = get_the_terms( $post->ID, 'product_cat' );
                    foreach ($terms as $t){
                        if ($t->slug=='ticket'){
                            $is_ticket = true;
                            break;
                        }
                    }
    
                    //if the category is other than ticket, basic WC single variable product will be displayed on frontend
                    if (!method_exists($product,'get_variation_attributes') || !$is_ticket) {
                        wp_enqueue_script( 'wc-add-to-cart-variation' );
    
                        wc_get_template( 'single-product/add-to-cart/variable.php', array(
                            'available_variations'  => $product->get_available_variations(),
                            'attributes'            => $product->get_variation_attributes(),
                            'selected_attributes'   => $product->get_default_attributes()
                            ) );
                        return;
                    }
    
                    //get product variations
                    $variations = $product->get_available_variations();
                    //storing variables of product and each variation of this product
                    //product ID and quantity (default 1 piece for row)
                    $custom_product_id = $product->get_id();
                    $custom_product_quantity = 1;
                    //storing variations data to variables with index
                    $index_n = 1;
                    ?>
                    <div id="hidden-variables-container">
                    <?php
                    foreach($variations as $var) {
                        $product_variation = new WC_Product_Variation($var['variation_id']);
                        //here i used ->id before
                        // $somevar = $this->_fillAttributeValues($product, $product_variation->get_id());
                        $this->_fillAttributeValues($product, $product_variation->id);
                        //variation attribute name json encode
                        $attr_text[$index_n] = json_encode($var['attributes']);
                        //variation name to display in frontend
                        $variation_name_to_display[$index_n] = self::_getAttributeValue($var['attributes']);
                        //variation attribute name json encode htmlspecialchars
                        $variation_name_full[$index_n] = htmlspecialchars($attr_text[$index_n]);
                        //variation id
                        $variation_id[$index_n] = $var['variation_id'];
    
                        ?>
                            <!-- Variables to frontend for JS use -->
                            <div class="hidden-variable <?php echo $index_n; ?>">
                            <span class="um_variation_name_frontend <?php echo $index_n; ?>"><?php echo $variation_name_to_display[$index_n]; ?></span>
                            <span class="um_variation_attribute_name <?php echo $index_n; ?>"><?php echo  $variation_name_full[$index_n]; ?></span>
                            <span class="um_variation_id <?php echo $index_n; ?>"><?php echo  $variation_id[$index_n];?></span>
                        </div>
                        <?php
                        $index_n++;
                    }
                    ?>
                    </div>
                    <?php
                }
    
                //get values of attributes
                private function _getAttributeValue($get_attributes){
                    $text = '';
                    foreach ($get_attributes as $name=>$attribute){
                        if ($name==self::$farba_vlastnost){
                            if (isset(self::$attributes_values['pa_level-bezca'][$attribute])) $text = self::$attributes_values['pa_level-bezca'][$attribute];
                            break;
                        }
                    }
                    if (empty($text)){
                        $text = [];
                        foreach ($get_attributes as $name=>$attribute){
                            if (isset(self::$attributes_values[substr($name, strlen('attribute_'))][$attribute])) $text[] = self::$attributes_values[substr($name, strlen('attribute_'))][$attribute];
                        }
                        $text = join(', ', $text);
                    }
                    return $text;
                }
    
                //fill attribute values to display name in frontend
                private function _fillAttributeValues($product, $product_variation_id){
                    $attributes = $product->get_attributes();
                    foreach ($attributes as $attribute){
                        if ($attribute['is_taxonomy']){
                            $post_terms = wp_get_post_terms( $product_variation_id, $attribute[ 'name' ] );
                            foreach ( $post_terms as $term ) {
                                self::$attributes_values[$term->taxonomy][$term->slug] = esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) );
                            }
                        }
                    }
                }
    
            function createCollapsibleSection(){
                global $product, $post, $woocommerce;
                //check if product term is ticket
                $is_ticket = false;
                $terms = get_the_terms( $post->ID, 'product_cat' );
                foreach ($terms as $t){
                    if ($t->slug=='ticket'){
                        $is_textil = true;
                    } else{
                    }
                }
                ?>
    
                <!-- Input type number for JS created fields -->
                <div class="quantity">
                    <label class="tickets-quantity" for="number-of-tickets">Vyberte počet lístkov</label>
                    <input type="number" id="number-of-tickets" class="" step="1" min="1" max="" name="number-of-tickets" value="1" title="Počet" size="4" inputmode="numeric">
                </div>
                <!-- This span opens hidden modal with input rows -->
                <span class="open-modal"> Vyplniť údaje  </span>
                    <?php
                }
    
                function collapsibleSectionHeader(){
                    global $product, $post, $woocommerce;
                    ?>
                                <!-- Form with input fields and select for variation -->
                                <div id="clone-wrapper-bigbranding" class="collapsible-wrapper-bigbranding hidenx">
                                    <div class="uber-modal">
                                    <span class="close-modal">Zatvoriť</span>
                                    <form id="form-motherboard" action="#" method="post">
                                        <input type="hidden" name="product_id" value="<?php $product->id; ?>">
                                        <input type="hidden" name="ubermike" value="1">
                                    <input class="do-kosika" type="submit" name="submit" value="Pridať do košíka" />
                                    <?php
                                            if (isset($_POST['ubermike'])){
                                                $product_id = absint($_POST['product_id']);
                                                // This loops through rows and adds products to cart
                                                $cnt = count($_POST['ticketLevel']);
                                                $quantity_total = 0;
    
                                                    for($i=0;$i<$cnt;$i++){
                                                            $cats = explode("|", $_POST['ticketLevel'][$i]);
                                                            $selected_val = $cats[0]; // cat_id
                                                            $selected_level = $cats[1]; // cat_name
                                                            $customer_name = $_POST['name'][$i];
                                                            $customer_address = $_POST['address'][$i];
                                                            $customer_bio = $_POST['bio'][$i];
                                                            $custom_data = array(); // Initializing
    
                                                            $custom_data['custom_data']['name'] = array(
                                                                    'label' => 'Meno',
                                                                    'value' => $customer_name
                                                            );
                                                            $custom_data['custom_data']['address'] = array(
                                                                    'label' => 'Adresa',
                                                                    'value' => $customer_address
                                                            );
                                                            $custom_data['custom_data']['bio'] = array(
                                                                    'label' => 'Bio',
                                                                    'value' => $customer_bio
                                                            );
                                                            $custom_data['custom_data']['level'] = array(
                                                                    'label' => 'Level',
                                                                    'value' => $selected_level
                                                            );
    
                                                            $quantity = 1;
    
                                                            WC()->cart->add_to_cart( $product_id, $quantity, $selected_val, array(), $custom_data );
    
                                                            do_action( 'woocommerce_set_cart_cookies', TRUE );
    
                                                            $quantity_total += $quantity;
    
                                                    }
    
                                                    if ($quantity_total) wc_add_to_cart_message( array( $product_id => $quantity_total ), true );
                                            }
                                     ?>
                                    </form>
                                        </div>
                                    </div>
                                    <?
                }
    
                function ubermike_render_meta_on_cart_and_checkout( $cart_data, $cart_item ){
                        $custom_items = array();
    
                        if( !empty( $cart_data ) )
                                $custom_items = $cart_data;
    
                        if( isset( $cart_item['custom_data'] ) ) {
                                foreach( $cart_item['custom_data'] as $key => $custom_data ){
                                        if( $key != 'key' ){
                                                $custom_items[] = array(
                                                        'name' => $custom_data['label'],
                                                        'value' => $custom_data['value'],
                                                );
                                        }
                                }
                        }
                        return $custom_items;
                }
    
                function my_custom_checkout_field( $checkout ) {
                        global $woocommerce;
    
                        echo '<div id="my_custom_checkout_field"><h2>' . __('Údaje objednávky') . '</h2>';
                        $indexing = 1;
                        foreach ( $woocommerce->cart->get_cart() as $cart_item ) {
                                if( isset($cart_item['custom_data']) ) {
    
                                    echo '<div class="uber-ticket-wrapper">';
                                    echo '<div class="uber-ticket">Ticket ' . $indexing . ' </div>';
                                        foreach( $cart_item['custom_data'] as $key => $custom_data ){
                                                if( $key != 'key' ){
                                                                echo $custom_data['label'];
                                                                echo("<br>");
                                                                echo $custom_data['value'];
                                                                echo("<br>");
                                                }
                                        }
                                        echo "</div>";
                                        $indexing++;
                                }
                        }
                        echo '</div>';
                }
    
                // Save cart item custom data as order item meta data and display it everywhere in Orders and email notifications
                function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
                    if (isset($values['custom_data'])) {
                        foreach( $values['custom_data'] as $key => $custom_data ){
                                if( $key != 'key' ){
                                    $item->update_meta_data( $custom_data['label'], $custom_data['value'] );
                                }
                        }
                    }
                }
    
                function boot_session() {
                    session_start();
                }
            }
            //instantiate plugin class and add it to the set of globals
            $GLOBALS['ubermike'] = new UberMike();
        }
    }
    
    if(在_数组('woocommerce/woocommerce.php',应用_过滤器('active_plugins',获取_选项('active_plugins'))){
    如果(!class_存在('UberMike')){
    超级教室麦克{
    私有静态$farba_vlastnost='attribute_pa_level-bezca';
    私有静态$attributes_values=array();
    公共函数构造(){
    self::$farba_vlastnot=self:$farba_vlastnot;
    self::$attributes\u values=self::$attributes\u values;
    //仅在woocommerce完成加载后调用
    添加_操作('woocommerce_init',数组(&$this,'woocommerce_loaded');
    //在加载所有插件后调用
    添加_操作('plugins_-loaded',array(&$this,'plugins_-loaded'));
    //排队脚本和css样式
    添加_操作(“wp_加载”,数组(&$this,“排队_脚本”);
    //添加自定义操作
    //完全取消WC操作
    添加操作('woocommerce\u single\u product\u summary',函数(){
    删除操作(“woocommerce变量添加到购物车”,“woocommerce变量添加到购物车”,30);
    }, -1000);
    //添加自定义添加到购物车
    add_action('woocommerce_variable_add_to_cart',数组($this,'add_to_cart'),30);
    //创建可折叠节输入编号和按钮
    add_操作('woocommerce_variable_add_to_cart',数组($this,'createcollapsablesection'));
    //在标题中隐藏可折叠部分
    添加动作('wp_footer',数组($this,'collapsablesectionheader');
    //启动会话
    //添加动作('wp_head',数组($this,'hook_cookie');
    添加_操作('wp_head',数组($this,'boot_session'));
    //显示购物车ANC结帐中每个项目的元数据
    添加过滤器(“woocommerce\u get\u item\u data”,数组($this,'ubermike\u render\u meta\u on\u cart\u and\u checkout'),20,2);
    //在结帐和订单注释中显示分组的自定义字段
    添加_操作('woocommerce_checkout_create_order',数组('my_custom_checkout_field');
    添加操作('woocommerce'u在'u order'u notes'之后,'数组('this,'my'u custom'u checkout'u field');
    //按顺序和电子邮件显示字段
    添加动作('WOOMerce\u checkout\u create\u order\u line\u item',数组('save\u custom\u order\u item\u meta\u data'),10,4);
    }
    /**
    *处理任何需要加载的内容。
    *例如,如果您需要访问$woocommerce global
    */
    已加载的公用函数(U){
    // ...
    }
    /**
    *处理任何需要加载所有插件的事情
    */
    公共函数插件\u加载(){
    // ...
    }
    //让JS脚本和CSS样式排队
    公共函数排队_脚本(){
    wp_register_样式('ubermike_css',plugins_url('assets/ubermike.css',_uu文件_u));
    wp_enqueue_风格(“ubermike_css”);
    wp_register_脚本('ubermike_js',plugins_url('assets/ubermike.js','uuuuu FILE_uuu',数组('jquery');
    wp_enqueue_脚本('ubermike_js');
    }
    //添加到购物车更换
    公共函数添加到购物车($allset){
    全球$product、$post、$WOOMERCESS;
    //检查产品类别
    $is_票证=假;
    $terms=get_the_terms($post->ID,'product_cat');
    foreach(条款为$t){
    如果($t->slug=='ticket'){
    $is_ticket=真;
    打破
    }
    }
    //如果类别不是票证,则前端将显示基本WC单变量产品
    如果(!method_存在($product,'get_variation_attributes')| |!$is_ticket){
    wp_排队_脚本('wc add to cart variation');
    wc_get_模板('single product/add to cart/variable.php',数组(
    “可用的\u变体
    
    add_action( 'woocommerce_init', 'force_non_logged_user_wc_session' );
    function force_non_logged_user_wc_session(){ 
        if( is_user_logged_in() || is_admin() )
           return;
    
        if ( ! WC()->session->has_session() ) 
           WC()->session->set_customer_session_cookie( true ); 
    }