У меня есть код в WooCommerce, по которому вы покупаете товары с идентификатором:
1561, 1919, 1568, 1562, 1563, 1564, 1565, 1566, 1567
Итак, второй товар — NIS 10.
Товар, на который распространяется скидка, — это только продукт с идентификатором 1561.
Код работает хорошо, но как только я хочу провести несколько рекламных акций, он не работает. Это не работает.
Например:
Если вы покупаете два продукта, я хочу, чтобы покупатель мог получить продукт 1561 дважды за 10 шекелей.
Если вы купите пять продуктов, то я хочу, чтобы покупатель мог получить в пять раз больше продукта 1561 за 10 шекелей
И так далее...
add_action('woocommerce_before_calculate_totals', 'change_second_item_price', 10, 1);
function change_second_item_price($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$discounted_product_id = 1561;
$new_regular_price = 10; // NIS 10
// Initialize count for the target products
$target_product_count = 0;
// Loop through cart items
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id'];
// Check if the product is one of the target products
if (in_array($product_id, array(1561, 1919, 1568, 1562, 1563, 1564, 1565, 1566, 1567))) {
$target_product_count += $cart_item['quantity'];
// Check if it's the discounted product and change the regular price
if ($product_id == $discounted_product_id && $target_product_count >= 2) {
$cart_item['data']->set_regular_price($new_regular_price);
$cart_item['data']->set_price($new_regular_price); // Also set the current price to the regular price
break; // Stop the loop after changing the regular price for the second item of the discounted product
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/777 ... ids-item-c
Измените цену конкретного товара в корзине WooCommerce на основе количества товаров с определенными идентификаторами про ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Расчет цены товара в корзине на основе выбранного настраиваемого поля «дни» в Woocommerce.
Anonymous » » в форуме Php - 0 Ответы
- 93 Просмотры
-
Последнее сообщение Anonymous
-