2017-12-20 35 views
1

WooCommerce에서 WooCommerce 구독 플러그인을 사용하고 있습니다.WooCommerce 비 반복 구독료

  1. 사용자가
  2. 사용자가 구독 제품이있는 활성 구독이 있습니다의 하나는 다음과 같은 조건이 충족 될 때 나는 정상 제품에 10 % 할인을 수행하는 함수를 작성하는 것을 시도하고있다 그의 카트
function vip_discount() { 

    $woocommerce = WC(); 
    $items = $woocommerce->cart->get_cart(); 
    $vip_product_id = get_subscription_product_id(); 
    $is_vip_in_cart = is_in_cart($vip_product_id); 
    $vip_product_price = 0; 

    foreach ($items as $item) { 
     if($item['variation_id'] === get_subscription_variation_id('monthly') || $item['variation_id'] === get_subscription_variation_id('quarterly') || $item['variation_id'] === get_subscription_variation_id('annually')) { 
      $vip_product_price = $item['line_total']; 
     } 
    } 

    if (wcs_user_has_subscription('', '', 'active') || $is_vip_in_cart) { 
     // Make sure that the calculation is a negative number at the end ALWAYS! 
     $discount = -(10/100) * ($woocommerce->cart->get_displayed_subtotal() - $vip_product_price); 
     print_r($discount); 
     $woocommerce->cart->add_fee('VIP Discount', $discount); 
    } 
} 
add_action('woocommerce_cart_calculate_fees', 'vip_discount'); 

에 문제는이 훅은 어떤 이유로 두 번 실행 것입니다. 또한 올바른 요금을 적용하지 않습니다. 부정적으로 적용된 수수료에서 반복 항목 합계를 뺀 것이지만 대신 구독료 (반복되는) 제품 가격 자체로 끝납니다.

추가 정보 또는 도움을 주시면 감사하겠습니다.

답변

0

처음으로 $woocommerce을 사용하는 경우 먼저 global $woocommerce;이 필요합니다. 같은 것을 실제 방법으로 사용하는 것이 더 WC()을 사용하는 것이 낫습니다. woocommerce_cart_calculate_fees 액션 후크와

은 후크 기능에서 누락 된 인수 $cart합니다 (WC_Cart 객체)있다.

함수 get_subscription_product_id()이 종료되지 않으므로 사용자 지정 함수 일 수 있습니다 ... 다른 것으로 대체했습니다.

표시된 총계 대신 cart_contents_total을 사용해야합니다. 총계 계산 전에이 후크가 실행되므로이 ​​부분을 사용해야합니다.

이 대신 유사한 코드를 재 방문하십시오 :

add_action('woocommerce_cart_calculate_fees', 'vip_discount', 10, 1); 
function vip_discount($cart) { 
    if (is_admin() && ! defined('DOING_AJAX')) return; // Exit 

    // Here the rate percentage of 10% to be applied 
    $rate = .10; 

    // Initializing variables 
    $vip_price = $discount = 0; 
    $subscription_in_cart = false; 

    // Loop through the cart items 
    foreach ($cart->get_cart() as $cart_item) { 
     if($cart_item['variation_id'] === get_subscription_variation_id('monthly') || $cart_item['variation_id'] === get_subscription_variation_id('quarterly') || $cart_item['variation_id'] === get_subscription_variation_id('annually')) { 
      $vip_price += $cart_item['line_total']; 
     } 

     // Get an instance of the parent product object (if not parent the product object) 
     $product = wc_get_product($cart_item['product_id']); 

     // Check for simple or variable "subscription" products 
     if($product->is_type('subscription') || $product->is_type('variable-subscription')){ 
      $subscription_in_cart = true; 
     } 
    } 

    // If customer has an active subscription or a product subscription in cart 
    if (wcs_user_has_subscription('', '', 'active') || $subscription_in_cart) { 

     // The discount calculation 
     $discount = ($cart->cart_contents_total - $vip_product_price) * $rate; 

     if($discount > 0){ 
      // Add a negative fee (a discount) 
      $cart->add_fee(__("VIP Discount"), -$discount); // not taxable here 
     } 
    } 
} 

이 코드는 어떤 플러그인 파일도 function.php의 활성 자식 테마 (또는 테마)의 파일이나 간다.

이렇게하면됩니다.