【发布时间】:2015-01-17 19:02:10
【问题描述】:
我无法想象如何验证购物车内是否有一些产品。我只需要允许一个产品进行结帐。
这是 class-wc-cart.php 中使用的代码,用于防止在购物车中已经有相同的产品时添加产品,我确信应该非常相似,但我缺少一些 WP 变量定义任何类型的产品。 我也试过this code,但它在functions.php中不起作用(不,我没有使用子主题)。
if ( $product_data->is_sold_individually() ) {
$in_cart_quantity = $cart_item_key ? $this->cart_contents[ $cart_item_key ]['quantity'] : 0;
// If it's greater than 0, it's already in the cart
if ( $in_cart_quantity > 0 ) {
wc_add_notice( sprintf(
'<a href="%s" class="button wc-forward">%s</a> %s',
$this->get_cart_url(),
__( 'View Cart', 'woocommerce' ),
sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_title() )
), 'error' );
return false;
}
}
谢谢。
【问题讨论】:
-
您想将购物车限制为仅 1 件total 还是仅限制特定数量的特定商品? (如果是后者,只需将商品设置为单独出售)
-
虽然技术上是重复的,但我不喜欢这个答案。我提供了一个我偶尔使用的 sn-p,因为我认为验证过滤器是添加您请求的功能的更好地方。
标签: php wordpress woocommerce e-commerce shopping-cart