【问题标题】:Woocommerce - Show cart even if it's emptyWoocommerce - 显示购物车,即使它是空的
【发布时间】:2016-10-04 06:47:09
【问题描述】:

我的 WordPress 相关任务很简单,但我找不到解决方案。我的 woocommerce 商店中有 2 种产品,我想在 woocommerce 表的购物车页面上显示它们,让客户设置它们的数量。如果客户不想买东西,就把它留在 0 上。

问题是购物车表如果为空则不显示,我只能看到我放在那里的物品。

【问题讨论】:

    标签: php wordpress woocommerce shopping-cart


    【解决方案1】:

    您可以添加这样的产品,并根据我为管理员和产品 ID 设置的条件添加条件。用您的产品 ID 更改产品 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 );
            }
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 2015-02-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      相关资源
      最近更新 更多