1 回答

TA貢獻1836條經驗 獲得超3個贊
請嘗試以下操作,當所有定義的產品 ID 都在購物車中時(在您的情況下是兩個),這將提供折扣:
add_action( 'woocommerce_cart_calculate_fees', 'x_products_discount' );
function x_products_discount( $cart ) {
// Settings below
$product_ids = array(34, 35); // <== Your defined product Ids
$percentage = 10; // <== discount in percentage (10% here)
$found_ids = array();
// Loop through cart items
foreach (WC()->cart->get_cart() as $cart_item ) {
// Loop through defined product Ids
foreach( $product_ids as $product_id ) {
if( in_array( $product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found_ids[$product_id] = $product_id;
break;
}
}
}
// Discount part
if( count( $found_ids ) === count( $product_ids ) ) {
$cart->add_fee( __( 'Discount', 'woocommerce' ), -( $cart->subtotal * $percentage / 100 ) );
}
}
代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。
- 1 回答
- 0 關注
- 174 瀏覽
添加回答
舉報