添加产品后,woocommerce_add_to_cart挂钩清空推车

时间:2015-12-07 08:28:36

标签: javascript php wordpress woocommerce

每次用户将产品添加到他/她的购物车时,我都会在Woocommerce中添加一个弹出窗口,其中显示添加的项目以及在后端的ACF字段中为每个人设置的相关项目产品

functions.php 中的代码如下所示:

add_action('woocommerce_add_to_cart', 'display_more_modal', 10, 6);
function display_more_modal( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
    ?>
        <div class="remodal" data-remodal-id="modal" data-remodal-options="hashTracking: false, closeOnOutsideClick: false">
            <button data-remodal-action="close" class="remodal-close"></button>
                <h3 class="remodal-header">Item added to cart!</h3>

                <?php 
                /* Display all the items currently in your cart */
                global $woocommerce; 
                $currency = get_woocommerce_currency_symbol();
                ?>
                    <div class="cart-wrapper">
                        <?php
                            $item = new WC_Product($product_id);
                            $item_thumb = wp_get_attachment_image_src( get_post_thumbnail_id($product_id));
                        ?>
                            <div class="row cart-item-wrapper">
                                <div class="col-4">
                                    <img class="cart-image" src="<?php echo $item_thumb[0]; ?>" alt="">
                                </div>
                                <div class="col-8">
                                    <a href="<?php echo $cart_product->post->guid; ?>"><h1 class="remodal-cart-item-heading"><?php echo $item->get_title(); ?></h1></a>
                                    <?php 
                                    $categories = wp_get_post_terms( $product_id, 'product_cat' );
                                    foreach($categories as $category) :
                                    ?>
                                        <?php if($category->slug == 'bikes') : ?>
                                            <?php 
                                                $frame_size = wc_attribute_label($variation['attribute_pa_frame-size']);
                                                $build_type = wc_attribute_label($variation['attribute_pa_build-type']);   
                                            ?>
                                            <p class="cart-meta">Frame Size: <?php echo $frame_size; ?></p>
                                            <p class="cart-meta">Build Type: <?php echo $build_type; ?></p>
                                        <?php endif; ?>
                                    <?php endforeach; ?>
                                    <span class="cart-amount"><?php echo $currency . $item->get_price(); ?></span>
                                </div>
                            </div>
                    </div>
                <?php

                ?>
                <div class="btn-wrapper right">
                    <a class="remodal-cart-btn font-18" href="<?php echo $woocommerce->cart->get_cart_url(); ?>">View cart</a>
                    <a class="remodal-cart-btn font-18" href="<?php echo $woocommerce->cart->get_checkout_url(); ?>">Checkout</a>
                </div>

                <?php 
                $related_products = get_field('upsell_products', $product_id);
                if($related_products) :
                ?>

                <h3 class="remodal-header">You may also like</h3>
                <div class="row related-items-wrapper">
                    <?php
                    /* Display related items that the user may be interested in. */
                    foreach($related_products as $post) :
                        setup_postdata($post);
                        $related_product = new WC_Product($post['upsell_product']->ID);
                        $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($post['upsell_product']->ID));
                    ?>
                        <div class="col-3">
                            <img src="<?php echo $featured_image[0]; ?>" alt="">
                            <h3 class="upsell-title"><?php echo $post['upsell_product']->post_title; ?></h3>
                            <div class="btn-wrapper">
                                <a class="remodal-cart-btn font-12" href="<?php echo $post['upsell_product']->guid; ?>">View item</a>
                            </div>
                        </div>
                    <?php
                    endforeach; 
                    wp_reset_postdata();
                    ?>
                </div>
                <?php endif; ?>
                <div class="btn-wrapper center">
                    <a class="remodal-cart-btn font-18" href="<?php echo get_site_url(); ?>/shop">Continue shopping</a> 
                </div>
        </div>
    <?php
  }

我遇到的问题是,只要用户点击弹出窗口中的任何链接,Woocommerce就会清空购物车。因此,如果您点击查看购物车,它会带您进入空购物车模板页面。如果用户继续浏览其他页面,您会在右上方看到它仍然表示购物车中有0个商品。

到目前为止我注意到了几个指示:

  1. 当我以管理员身份登录时,此问题不会发生。仅限访客用户。

  2. 当我删除代码然后重新添加代码时,问题就会消失,但在添加隐身或使用其他浏览器/计算机的产品时仍会出现问题。

  3. 您可以尝试将产品添加到购物车here

    我做错了什么以及什么是最佳做法,因为行动中的这个巨大的块看起来相当混乱?

0 个答案:

没有答案