#wordpress #woocommerce
#wordpress #woocommerce
Вопрос:
В моем магазине разные категории товаров. У меня есть категория «nettoyage», которую я хотел бы разрешить клиентам заказывать, ТОЛЬКО если количество товаров «nettoyage» в корзине> 3.
Прямо сейчас я установил количество по умолчанию равным 3 и не разрешаю пользователю переходить ниже на страницы продуктов. Но я бы хотел, чтобы они могли добавлять по 1 каждому, например, и разрешать им заказывать до тех пор, пока количество товаров «nettoyage» в корзине> 3. Как я могу реорганизовать этот код, чтобы сделать это?
Прямо сейчас у меня есть следующий код в моем functions.php :
add_filter('woocommerce_quantity_input_args', 'bloomer_woocommerce_quantity_changes', 10, 2);
function bloomer_woocommerce_quantity_changes($args, $product)
{
if (!is_cart()) {
if (is_singular('product') amp;amp; (has_term('nettoyage', 'product_cat'))) {
$args['input_value'] = 3; // Start from this value (default = 1)
$args['max_value'] = 10; // Max quantity (default = -1)
$args['min_value'] = 3; // Min quantity (default = 0)
$args['step'] = 1; // Increment/decrement by this value (default = 1)
}
}
return $args;
}
add_filter('woocommerce_quantity_input_args', 'min_qty_filter_callback', 20, 2);
function min_qty_filter_callback($args, $product)
{
$categories = array('Noten'); // The targeted product category(ies)
$min_qty = 3; // The minimum product quantity
$product_id = $product->is_type('simple') ? $product->get_parent_id() : $product->get_id();
if (has_term($categories, 'product_cat', $product_id)) {
$args['min_value'] = $min_qty;
}
return $args;
}
// On shop and archives pages
add_filter('woocommerce_loop_add_to_cart_args', 'min_qty_loop_add_to_cart_args', 10, 2);
function min_qty_loop_add_to_cart_args($args, $product)
{
$categories = array('nettoyage'); // The targeted product category
$min_qty = 3; // The minimum product quantity
$product_id = $product->get_id();
if (has_term($categories, 'product_cat', $product_id)) {
$args['quantity'] = $min_qty;
}
return $args;
}
add_action('woocommerce_check_cart_items', 'wc_min_item_required_qty');
function wc_min_item_required_qty()
{
$categories = array('nettoyage'); // The targeted product category
$min_item_qty = 3; // Minimum Qty required (for each item)
$display_error = false; // Initializing
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item_key) {
$item_quantity = $cart_item['quantity']; // Cart item quantity
$product_id = $cart_item['product_id']; // The product ID
// For cart items remaining to "Noten" producct category
if (has_term($categories, 'product_cat', $product_id) amp;amp; $item_quantity < $min_item_qty) {
wc_clear_notices(); // Clear all other notices
// Add an error notice (and avoid checkout).
wc_add_notice(sprintf("Le service livraison nettoyage n'est valable qu'à partir de %s paires!", $min_item_qty, $item_quantity), 'error');
break; // Stop the loop
}
}
}
Ответ №1:
Наконец-то найдено решение. Для тех, кто заинтересован, вот пользовательская функция :
add_action('woocommerce_check_cart_items', 'custom_set_min_total');
function custom_set_min_total()
{
if (is_cart() || is_checkout()) {
global $woocommerce, $product;
$i = 0;
foreach ($woocommerce->cart->cart_contents as $product) :
$minimum_cart_product_total = 3;
if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
$total_quantity = $product['quantity'];
endif;
endforeach;
foreach ($woocommerce->cart->cart_contents as $product) :
if (has_term('nettoyage', 'product_cat', $product['product_id'])) :
if ($total_quantity < $minimum_cart_product_total amp;amp; $i == 0) {
wc_add_notice(
sprintf(
'<strong>A Minimum of %s products is required from the nettoyage category before checking out.</strong>'
. '<br />Current number of items in the cart: %s.',
$minimum_cart_product_total,
$total_quantity
),
'error'
);
}
$i ;
endif;
endforeach;
}
}