我的woocommerce产品实际上是巡航探险(两种产品=两种探险类型)。对于每种产品,变化包括巡航发生的周数(=日期)。 所以我有20个不同星期的利古里亚海上探险队和其他20周的希腊远征队。幸运的是,我只有2个这样的产品可以处理(非常简单的情况)
顾客通常选择一周的探险。但是,如果客户决定申请>,我需要在第二周(或第三周)申请10%的折扣。 1周。因此,第一周支付全价,但第二周和(如果有)第三周将打折10%。
我已经推出了一个功能,可以在两周的情况下应用折扣。
function cart_discount() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
global $woocommerce; $cat_count = 0;
$cart = WC()->cart->get_cart();
foreach($cart as $cart_item_key => $values) {
$product_id = $values['product_id']; // product ID (= cruise type)
$variation_id = $values['variation_id']; // variation (=week)
$cart_lines_total = $values["line_total"]; //variation total price
$cart_lines_quantity = $values["quantity"];// variation quantity
//$product = 1394 = Expedition Ligurian eng
//$product = 1389 = Expedition Greece eng
//$product = 13888 = Expedition Ligurian ita
//$product = 13910 = Expedition Greece ita
//I hereby add a condition as we do have the same cruises with students prices which are not eligible to this discount (and are stored with different product_id)
if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
{
//put in a new array only the terms I need for the calculation later
$cart_array []= array( $product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
}
}
// discount percent is 10%
$percent = -0.10;
if ($cart_array[0][0] == $cart_array[1][0]) //if in the cart the same product is present two times
{
$discount = $percent * $cart_array[1][2] * $cart_array[1][3];
$discount_text = __( 'Quantity discount', 'woocommerce' );
WC()->cart->add_fee( $discount_text, $discount, false );
}
}
add_action( 'woocommerce_cart_calculate_fees','cart_discount' );
如前所述,此代码有许多限制,因为它没有考虑到某些情况,例如:
1)只有在购物车中只有同一产品的两种变体才会处理:如果客户决定购买x周,我应该能够检查同一产品是否存在> 2种变化;
2)它没有考虑到两种产品有两种变化的可能性(即,一个人在利古里亚海购买两周,在希腊购买两周)
如果有人可以帮我改进我写的代码,我会非常高兴!!
答案 0 :(得分:0)
为了确保我做对了,如果客户购买同一产品的1种以上变体,您希望申请折扣。正确的吗?
答案 1 :(得分:0)
这是我写的最终代码(它有效,我已经测试过了)
function cart_discount() {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
global $woocommerce;
$cart = WC()->cart->get_cart();
$has_coupons = count(WC()->cart->applied_coupons)>0?true:false;
if(!$has_coupons) //verifies that other discounts are not added
{
foreach($cart as $cart_item_key => $values) {
$product_id = $values['product_id']; // product ID
$variation_id = $values['variation_id']; // product quantity
$cart_lines_total = $values["line_total"];
$cart_lines_quantity = $values["quantity"];
//products for which this kind of discount must be applicable:
//$product_id = 1394 = Spedizioni CSR eng
//$product_id = 1389 = Spedizioni IDP eng
//$product_id = 13888 = Spedizioni CSR ita
//$product_id = 13910 = Spedizioni IDP ita
if($product_id == '1394' || $product_id == '1389' || $product_id == '13888' || $product_id == '13910')
{
$cart_array []= array( $product_id , $variation_id, $cart_lines_quantity, $cart_lines_total);
}
}
$conteggio = count($cart_array); //conta il numero di prodotti nel carrello
// percent is 10%
$percent = -0.10;
if ($conteggio < 3 && $cart_array[0][0] == $cart_array[1][0])
{
$discount = $percent * $cart_array[1][3];
$discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
WC()->cart->add_fee( $discount_text, $discount, false );
}
if ($conteggio < 4 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0])
{
$discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]);
$discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
WC()->cart->add_fee( $discount_text, $discount, false );
}
if ($conteggio < 5 && $cart_array[0][0] == $cart_array[1][0] && $cart_array[0][0] == $cart_array[2][0] && $cart_array[0][0] == $cart_array[3][0])
{
$discount = ($percent * $cart_array[1][3]) + ($percent * $cart_array[2][3]) + ($percent * $cart_array[3][3]);
$discount_text = __( '10% discount on subsequent week(s)', 'woocommerce' );
WC()->cart->add_fee( $discount_text, $discount, false );
}
} else return;
}
add_action( 'woocommerce_cart_calculate_fees','cart_discount' );
如你所见,我必须指定对这种特殊折扣感兴趣的产品的product_id,这对我来说没问题,但如果我们可以将它设置为类别会更好(我没有&#39;有时间发展这种情况) 其次,我不喜欢的部分是以下
if ($conteggio < 3 && $cart_array[0][0] == $cart_array[1][0])
以及以下所有条件:我正在寻找可以通过$cart_array
的函数,并找到我在每个if()条件下手动设置的关系。
由于