根据 WooCommerce 中的产品属性添加到购物车验证
Add to cart validation based on product attribute in WooCommerce
我正在尝试在客户将产品添加到购物车时添加一个过滤器,以允许或不允许。
我们只需要比较 WooCommerce 产品的一个属性。
- 如果购物车是空的:可以加入购物车
- 如果购物车有 1 件或多件商品:检查产品的属性 XXX 值
- 如果属性值相同:加入购物车 ok
- 如果属性值不同:添加到购物车被拒绝,将显示消息
到目前为止我已经有了这段代码,但它根本无法正常工作,我不确定为什么?
// Check Products added to cart for same vendor
function so_validate_add_cart_item( $passed, $product_id ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values)
{
$_product = wc_get_product( $values['data']->get_id());
$prod1_vendeur[] = $_product->get_attribute( 'pa_vendeur' );
}
$newproduct = wc_get_product( $product_id );
$prod2_vendeur = $newproduct->get_attribute( 'pa_vendeur' );
if (isset($prod1_vendeur ))
{
if ( $prod1_vendeur[0] != $prod2_vendeur )
{
$passed = false;
wc_add_notice( 'Error message' , 'notice' );
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );
非常感谢任何帮助。
- 虽然您指定
woocommerce_add_to_cart_validation
钩子包含 5 个参数,但您只传递了 2
global $woocommerce;
不是必须的,用WC()
代替
所以你得到:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Setting
$attribute = 'pa_vendeur';
// Real product ID
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// Get product
$product = wc_get_product( $product_id );
// Get the product attribute value
$product_attribute = $product->get_attribute( $attribute );
// Initialize
$flag = false;
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Get the product attribute value
$cart_item_attribute = $cart_item['data']->get_attribute( $attribute );
// NOT equal
if ( $cart_item_attribute != $product_attribute ) {
// Flag becomes true
$flag = true;
// Break loop
break;
}
}
}
}
// True
if ( $flag ) {
// Display an error message
wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
我正在尝试在客户将产品添加到购物车时添加一个过滤器,以允许或不允许。
我们只需要比较 WooCommerce 产品的一个属性。
- 如果购物车是空的:可以加入购物车
- 如果购物车有 1 件或多件商品:检查产品的属性 XXX 值
- 如果属性值相同:加入购物车 ok
- 如果属性值不同:添加到购物车被拒绝,将显示消息
到目前为止我已经有了这段代码,但它根本无法正常工作,我不确定为什么?
// Check Products added to cart for same vendor
function so_validate_add_cart_item( $passed, $product_id ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values)
{
$_product = wc_get_product( $values['data']->get_id());
$prod1_vendeur[] = $_product->get_attribute( 'pa_vendeur' );
}
$newproduct = wc_get_product( $product_id );
$prod2_vendeur = $newproduct->get_attribute( 'pa_vendeur' );
if (isset($prod1_vendeur ))
{
if ( $prod1_vendeur[0] != $prod2_vendeur )
{
$passed = false;
wc_add_notice( 'Error message' , 'notice' );
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );
非常感谢任何帮助。
- 虽然您指定
woocommerce_add_to_cart_validation
钩子包含 5 个参数,但您只传递了 2 global $woocommerce;
不是必须的,用WC()
代替
所以你得到:
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Setting
$attribute = 'pa_vendeur';
// Real product ID
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// Get product
$product = wc_get_product( $product_id );
// Get the product attribute value
$product_attribute = $product->get_attribute( $attribute );
// Initialize
$flag = false;
// WC Cart
if ( WC()->cart ) {
// Get cart
$cart = WC()->cart;
// If cart is NOT empty
if ( ! $cart->is_empty() ) {
// Loop through cart items
foreach( $cart->get_cart() as $cart_item ) {
// Get the product attribute value
$cart_item_attribute = $cart_item['data']->get_attribute( $attribute );
// NOT equal
if ( $cart_item_attribute != $product_attribute ) {
// Flag becomes true
$flag = true;
// Break loop
break;
}
}
}
}
// True
if ( $flag ) {
// Display an error message
wc_add_notice( __( 'My custom error message', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );