add_action( 'woocommerce_check_cart_items', 'prevent_checkout_product_category_based' );
function prevent_checkout_product_category_based() {
// HERE set the product category slug
$category = 'ucenennye-tovary';
$found = $other = false; // Initializing variables
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// checking for the specific product category
$term_slugs = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array('fields' => 'slugs') );
if( in_array($category, $term_slugs) ) {
$found = true; // Targeted product category found
}
elseif( ! in_array($category, $term_slugs) && sizeof($term_slugs) > 0 ){
$other = true; // Other product categories found
}
}
// If the targeted product category is mixed with other product categories
if ( $found && $other ) {
// Display an error notice and avoid checkout
wc_add_notice( __( "Товары категории Х оформляются отдельным заказом!" ), 'error' );
}
}
Попробуйте следующий сниппет.
Код:add_action( 'woocommerce_check_cart_items', 'prevent_checkout_product_category_based' ); function prevent_checkout_product_category_based() { // HERE set the product category slug $category = 'ucenennye-tovary'; $found = $other = false; // Initializing variables // Loop through cart items foreach ( WC()->cart->get_cart() as $cart_item ) { // checking for the specific product category $term_slugs = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array('fields' => 'slugs') ); if( in_array($category, $term_slugs) ) { $found = true; // Targeted product category found } elseif( ! in_array($category, $term_slugs) && sizeof($term_slugs) > 0 ){ $other = true; // Other product categories found } } // If the targeted product category is mixed with other product categories if ( $found && $other ) { // Display an error notice and avoid checkout wc_add_notice( __( "Товары категории Х оформляются отдельным заказом!" ), 'error' ); } }
Верно, чтобы в одном заказе были либо только "Тарелки" либо только "Кастрюли" и т.д.Как бы да, но я так понял что чуток нужно по другому
Допустим есть категории
Тарелки / Кастрюли / чашки
Клиент добавляет рандомно товар из категорий
но оформить заказ он может только когда товары в корзине соответствуют только одной из всех категорий
в Вашем примере вы указали только определенную категорию
ucenennye-tovary
Спасибо большое за ответ, выше верно подметили, можно как то решить этот вопрос?Попробуйте следующий сниппет.
Код:add_action( 'woocommerce_check_cart_items', 'prevent_checkout_product_category_based' ); function prevent_checkout_product_category_based() { // HERE set the product category slug $category = 'ucenennye-tovary'; $found = $other = false; // Initializing variables // Loop through cart items foreach ( WC()->cart->get_cart() as $cart_item ) { // checking for the specific product category $term_slugs = wp_get_post_terms( $cart_item['product_id'], 'product_cat', array('fields' => 'slugs') ); if( in_array($category, $term_slugs) ) { $found = true; // Targeted product category found } elseif( ! in_array($category, $term_slugs) && sizeof($term_slugs) > 0 ){ $other = true; // Other product categories found } } // If the targeted product category is mixed with other product categories if ( $found && $other ) { // Display an error notice and avoid checkout wc_add_notice( __( "Товары категории Х оформляются отдельным заказом!" ), 'error' ); } }