我在 WooCommerce 中有一个代码,一旦您使用 ID 购买产品: 1561、1919、1568、1562、1563、1564、1565、1566、1567 所以,第二个产品是 NIS 10。
优惠商品仅限ID 1561的商品 代码运行得很好,但是一旦我想要有多个促销,它就不起作用了。
例如: 如果您购买两种产品,那么我希望客户能够以 10 新谢克尔的价格获得两次产品 1561 如果您购买五种产品,那么我希望客户能够以 NIS 10 收到五倍产品 1561 等等...
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
}
}
}
}
我认为你需要先计算最大折扣产品数量,然后为适当的产品设置价格:
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'];
}
}
// Loop through cart items again, to find the discounted product
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id'];
// Check if it's the discounted product and change the regular price
if ($product_id == $discounted_product_id && $target_product_count >= $cart_item['quantity']) {
$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
}
}
}
请记住,这允许客户始终以 10 新谢克尔购买 1561。我认为这是您的意图,但我不确定。
在以下代码中,将更改特定购物车商品(最小数量为 2)的价格,启用、基于某些定义的产品数量(购物车商品数量)并进行计算。
您需要在第一个函数中定义:
// Utility function: Get the discounted product price
function get_discounted_product_price( $cart_items ) {
$targeted_products_ids = array(1919, 1568, 1562, 1563, 1564, 1565, 1566, 1567);
$product_id_to_discount = 1561;
$current_price = 20; // Here define the product active price
$discounted_price = 10; // Here define the discounted price
$targeted_count = $discounted_count = $discounted_key = false; // Initialize variables
// Loop through cart items: Count items
foreach ( $cart_items as $item_key => $item ) {
// count targeted products
if ( in_array($item['product_id'], $targeted_products_ids) ) {
$targeted_count += $item['quantity'];
}
// count targeted products
elseif ( $item['product_id'] == $product_id_to_discount ) {
$discounted_count += $item['quantity'];
$discounted_key = $item_key;
}
}
// Calculate new discounted price
if ( $targeted_count > 1 && $discounted_count > 1 ) {
$difference_count = $targeted_count - $discounted_count;
if ( $difference_count >= 0 ) {
$new_price = $discounted_price;
} else {
$difference_count = -$difference_count;
$new_price = (($discounted_price * $targeted_count) + ($current_price * $difference_count)) / $discounted_count;
}
return array('key' => $discounted_key, 'price' => floatval($new_price));
}
return false;
}
// change specific cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_cart_item_price', 100, 1 );
function change_cart_item_price( $cart ) {
if ( is_admin() && ! defined('DOING_AJAX') )
return;
$cart_items = $cart->get_cart();
$data = get_discounted_product_price( $cart_items );
if ( ! $data )
return;
$cart_items[$data['key']]['data']->set_price($data['price']);
}
// For minicart: change cart item displayed price html
add_filter( 'woocommerce_cart_item_price', 'filter_cart_item_price_html', 10, 3 );
function filter_cart_item_price_html( $price_html, $cart_item, $cart_item_key ) {
$data = get_discounted_product_price( WC()->cart->get_cart() );
if ( isset($data['key']) && $data['key'] === $cart_item_key ) {
$args = array( 'price' => $data['price'] );
if ( WC()->cart->display_prices_including_tax() ) {
$product_price = wc_get_price_including_tax( $cart_item['data'], $args );
} else {
$product_price = wc_get_price_excluding_tax( $cart_item['data'], $args );
}
return wc_price( $product_price );
}
return $price_html;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并工作。