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 关注
- 139 浏览
添加回答
举报