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

问题描述:

我的WordPress相关任务很简单,但我找不到解决方案。我在我的woocommerce商店有两种产品,我想在woocommerce表的购物车页面上显示他们两个,让客户设置它们的数量。如果客户不想购买东西,只需将它放在0上即可。Woocommerce - 即使它是空的即可显示购物车

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

你可以添加这样的产品,并添加条件根据我为管理员和产品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); 
     } 
    } 
}