duanque3125 2016-10-04 06:47
浏览 73

Woocommerce - 显示购物车,即使它是空的

My WordPresss related task is simple, but I can't find the solution. I have 2 products in my woocommerce shop, and I would like to show both of them on the cart-page in the woocommerce table, let the customer set the quantity of them. If the customer don't want to buy something, just leave it on 0.

The problem is the cart table is not shown if it is empty and I can only see the items that I put in there.

  • 写回答

1条回答 默认 最新

  • douhang8991 2016-10-04 07:10
    关注

    you can add product like that and add condition according you i set for admin and product id. change product id with your product id

    /*
     * goes in theme functions.php or a custom plugin
     **/
    // add item to cart on visit
    add_action( 'template_redirect', 'add_product_to_cart' );
    function add_product_to_cart() {
        if ( ! is_admin() ) {
            $product_id = 64;
            $found = false;
            //check if product already in cart
            if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
                foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                    $_product = $values['data'];
                    if ( $_product->id == $product_id )
                        $found = true;
                }
                // if product not found, add it
                if ( ! $found )
                    WC()->cart->add_to_cart( $product_id );
            } else {
                // if no products in cart, add it
                WC()->cart->add_to_cart( $product_id );
            }
        }
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?