在 WooCommerce 中成本较低的产品的购物车折扣,除非该产品已经在销售
Cart discount for product that cost less in WooCommerce unless that product is already on sale
我想为购物车中最便宜的商品添加 30% 的折扣,除非它已经有折扣。
基于 答案代码,这是我的代码尝试:
add_action('woocommerce_cart_calculate_fees', 'discount_on_cheapest_cart_item', 20, 1 );
function discount_on_cheapest_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if ( $cart->get_cart_contents_count() < 2 ) return;
// Initialising
$percentage = 50; // 10 %
$discount = 0;
$item_prices = array();
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
$product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
sort($product_prices_excl_tax);
if( ! $cart_item['data']->is_on_sale() ){
$discount = reset($product_prices_excl_tax) * $percentage / 100;
$cart->add_fee( "Discount on cheapest (".$percentage."%)", -$discount );
}
}
有没有办法让它工作,如果价格最低的产品不打折,那么申请 30%,如果打折,就不要。但这仅适用于最低价,如果有其他产品在销售,我们将跳过它。
在您的代码尝试中使用 if( ! $cart_item['data']->is_on_sale() )
不会有任何影响,因为它在 foreach 循环之外使用。
此答案将应用基于价格最低的产品计算的折扣,仅当该产品尚未打折时才如此。
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if ( $cart->get_cart_contents_count() < 2 ) return;
// Setting
$percentage = 30; // 30 %
// Initialize
$discount = 0;
$product_prices_on_sale = array();
$product_prices_excl_tax = array();
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
// When cart contains a on sale product
if ( $cart_item['data']->is_on_sale() ) {
// On sale, push to on sale array
$product_prices_on_sale[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
// Push to excl tax array
$product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
// Sort an array in ascending order
sort( $product_prices_on_sale );
sort( $product_prices_excl_tax );
// Set the internal pointer of an array to its first element
$p_o_s = reset( $product_prices_on_sale );
$p_e_t = reset( $product_prices_excl_tax );
// NOT equal
if ( $p_o_s != $p_e_t ) {
// Calculate discount
$discount = $p_e_t * $percentage / 100;
// Apply discount (negative fee)
$cart->add_fee( 'Discount on cheapest (' . $percentage . '%)', -$discount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
我想为购物车中最便宜的商品添加 30% 的折扣,除非它已经有折扣。
基于
add_action('woocommerce_cart_calculate_fees', 'discount_on_cheapest_cart_item', 20, 1 );
function discount_on_cheapest_cart_item( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if ( $cart->get_cart_contents_count() < 2 ) return;
// Initialising
$percentage = 50; // 10 %
$discount = 0;
$item_prices = array();
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
$product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
sort($product_prices_excl_tax);
if( ! $cart_item['data']->is_on_sale() ){
$discount = reset($product_prices_excl_tax) * $percentage / 100;
$cart->add_fee( "Discount on cheapest (".$percentage."%)", -$discount );
}
}
有没有办法让它工作,如果价格最低的产品不打折,那么申请 30%,如果打折,就不要。但这仅适用于最低价,如果有其他产品在销售,我们将跳过它。
在您的代码尝试中使用 if( ! $cart_item['data']->is_on_sale() )
不会有任何影响,因为它在 foreach 循环之外使用。
此答案将应用基于价格最低的产品计算的折扣,仅当该产品尚未打折时才如此。
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Only for 2 items or more
if ( $cart->get_cart_contents_count() < 2 ) return;
// Setting
$percentage = 30; // 30 %
// Initialize
$discount = 0;
$product_prices_on_sale = array();
$product_prices_excl_tax = array();
// Loop though each cart items and set prices in an array
foreach ( $cart->get_cart() as $cart_item ) {
// When cart contains a on sale product
if ( $cart_item['data']->is_on_sale() ) {
// On sale, push to on sale array
$product_prices_on_sale[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
// Push to excl tax array
$product_prices_excl_tax[] = wc_get_price_excluding_tax( $cart_item['data'] );
}
// Sort an array in ascending order
sort( $product_prices_on_sale );
sort( $product_prices_excl_tax );
// Set the internal pointer of an array to its first element
$p_o_s = reset( $product_prices_on_sale );
$p_e_t = reset( $product_prices_excl_tax );
// NOT equal
if ( $p_o_s != $p_e_t ) {
// Calculate discount
$discount = $p_e_t * $percentage / 100;
// Apply discount (negative fee)
$cart->add_fee( 'Discount on cheapest (' . $percentage . '%)', -$discount );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );