嘿,伙计们:)我正在尝试对购物车实施自定义折扣规则。基本上有WooCommerce,该网站出售t恤衫。目前有一个促销活动,如果你买了3件t恤,你只需付2件,另一件价格最低的t恤是免费的。我使用钩子“woocommerce\\u cart\\u calculate\\u fees”创建了一个自定义函数,到目前为止,它仍在运行。这是我的代码:
function iom_add_custom_discount( $wc_cart ){
$discount = 0;
$product_ids = array();
$item_prices = array();
$in_cart = true;
foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
$cart_product = $cart_item[\'data\'];
if ( has_term( \'detski-bodita\', \'product_cat\', $cart_product->get_id() ) ) {
$in_cart = true;
}else {
$product_ids[] = $cart_product->get_id();
$item_prices[$cart_product->get_id()] = $cart_product->get_price();
}
}
if( $in_cart ) {
$count_ids = count($product_ids);
asort( $item_prices ); //Sort the prices from lowest to highest
$count = 0;
if( $count_ids == 3 ) {
foreach( $item_prices as $id => $price ) {
if( $count >= 1 ) {
break;
}
//$product = wc_get_product( $id );
//$price = $product->get_price();
$discount -= ($price * 100) / 100;
$count++;
}
}
}
if( $discount != 0 ){
$wc_cart->add_fee( \'Discount\', $discount, true );
# Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
}
}
这是购物车页面的屏幕截图-->;https://pasteboard.co/JkES3RD.png将显示并应用折扣。我无法理解的棘手部分是,如何使折扣产品的价格删除,并在表中显示价格0.00,以及如何编辑函数,以便在迷你购物车中仍显示3种产品,但显示2种产品的折扣价格?提前非常感谢!:)编辑:而且,它似乎只有在我的购物车中有3种不同的产品时才起作用。如果我有一个数量为2的产品和一个数量为1的产品,那么它不起作用。。如何调整函数使其工作?