1 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
請(qǐng)嘗試以下操作,當(dāng)所有定義的產(chǎn)品 ID 都在購(gòu)物車中時(shí)(在您的情況下是兩個(gè)),這將提供折扣:
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 ) );
}
}
代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過測(cè)試并有效。
- 1 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報(bào)