我的WordPresss相关任务很简单,但我找不到解决方案。我在我的woocommerce商店有2个产品,我想在woocommerce表的购物车页面上显示它们,让客户设定它们的数量。如果客户不想购买东西,只需将其保留为0。
问题是如果购物车表是空的,那么它就不会显示,我只能看到我放在那里的物品。
答案 0 :(得分:0)
你可以添加这样的产品,并根据我为管理员和产品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 );
}
}
}