![](/img/trans.png)
[英]Allow cart item quantity update for only one product category in Woocommerce
[英]Woocommerce: How to allow only one product type in a cart?
我有兩種產品類型,一種是簡單產品$cart_item['default-engraving']
,另一種是信用產品$cart_item['default-engraving'] && $cart_item['iconic-engraving']
。 我試圖找到一個解決方案,如果我將簡單的產品添加到購物車,則不應添加信用產品。 或者,如果我將信用產品添加到購物車,則不應添加簡單產品。 但是,如果我願意,我可以根據需要添加相同的類型,例如簡單的產品類型。
更新:無法在添加到購物車事件時檢測自定義購物車項目數據。
檢查購物車項目將允許您防止購物車項目同時具有$cart_item['default-engraving']
和$cart_item['iconic-engraving']
:
add_action( 'woocommerce_check_cart_items', 'check_cart_items_custom_data' );
function check_cart_items_custom_data() {
// Initializing: set the current product type in an array
$types = [];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
if( isset( $item['default-engraving'] ) )
$types[] = 'default';
if( isset( $item['iconic-engraving'] ) )
$types[] = 'iconic';
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice and avoid checkout
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
}
}
代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。
原始答案:(它不適用於您的情況,因為在添加到購物車事件時無法檢測到該情況)
以下是將產品類型定位為僅允許購物車中的一種的方法:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_type_allowed', 10, 3 );
function only_one_product_type_allowed( $passed, $product_id, $quantity ) {
// Initializing: set the current product type in an array
$types = [ wc_get_product( $product_id )->get_type() ];
// Loop through cart items
foreach (WC()->cart->get_cart() as $item ){
// Set each product type in the array
$types[] = wc_get_product( $item['product_id'] )->get_type();
}
$types = array_unique( $types );
// Check the number of product types allowing only one
if( count( $types ) > 1 ){
// Displaying a custom notice
wc_add_notice( __('Only items from one product type are allowed in cart'), 'error' );
return false; // Avoid add to cart
}
return $passed;
}
代碼位於活動子主題(或活動主題)的 functions.php 文件中。 測試和工作。
你可以試試這個
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_category_allowed', 20, 3 );
function only_one_product_category_allowed( $passed, $product_id, $quantity) {
// Getting the product categories term slugs in an array for the current product
$term_slugs = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'slugs') );
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the product category of the current product don't match with a cart item
$_product = wc_get_product( $cart_item['product_id']; );
if( $_product->is_type( 'simple' ) ) {
// code to not allow other product type
} else {
// do stuff for everything else
}
}
}
請檢查下面的代碼。
<?php
function ji_woocommerce_add_to_cart_validation( $valid, $product_id, $quantity )
{
$_product = wc_get_product( $product_id);
foreach (WC()->cart->get_cart() as $item ){
$checkTypes[] = wc_get_product( $item['product_id'] )->get_type();
}
$checkTypes = array_unique( $types );
if(count($checkTypes) > 0)
{
if(!in_array($_product->get_type(),$checkTypes))
{
wc_add_notice( __('Here you can add your error message','textdomain'), 'error' );
return false;
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'ji_woocommerce_add_to_cart_validation', 10, 3 );
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.