2017-09-07 10 views
1

2回目の購入時に価格をオーバーライドできません。商品がすでに購入されている場合は、WooCommerceカートの商品価格をゼロに設定します。

ユースケースは次のようになります。ユーザがすでに商品 "944"を購入している場合、次回の注文の価格は0になります。

意味すると、顧客はその特定の製品の最初の注文のみを支払うことになり、次の注文のために無料となります。

ここに私のコード:

// Enter the ID of the product that shouldn't be purchased again 
    $no_repeats_id = 944; 
    $no_repeats_product = wc_get_product($no_repeats_id); 

    // Get the current product to check if purchasing should be disabled 
    global $product; 

    if ($no_repeats_product->is_type('variation')) { 
     // Bail if we're not looking at the product page for the non-purchasable product 
     if (! $no_repeats_product->parent->id === $product->id) { 
      return; 
     } 

     // Render the purchase restricted message if we are 
     if (wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id)) { 
      sv_render_variation_non_purchasable_message($product, $no_repeats_id); 
     } 

    } elseif ($no_repeats_id === $product->id) { 
     if (wc_customer_bought_product(wp_get_current_user()->user_email, get_current_user_id(), $no_repeats_id)) { 
      // Create your message for the customer here 
      add_action('woocommerce_before_calculate_totals', 'add_custom_price'); 

add_action('woocommerce_before_calculate_totals', 'add_custom_price'); 

function add_custom_price($cart_object) { 
    $custom_price = 10; // This will be your custome price 
    $target_product_id = 944; 
    foreach ($cart_object->cart_contents as $value) { 
     if ($value['product_id'] == $target_product_id) { 
      $value['data']->price = $custom_price; 
     } 
     /* 
     // If your target product is a variation 
     if ($value['variation_id'] == $target_product_id) { 
      $value['data']->price = $custom_price; 
     } 
     */ 
    } 
} 

     } 
    } 
} 
add_action('woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31); 

答えて

1

ここではそれがあまりにも3+ woocommerceに動作させるための方法です:

add_action('woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1); 
function conditionally_change_cart_items_price($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Set Here your targeted product ID 
    $targeted_product_id = 944; 

    // Set Here your custom price (1st purshase) 
    $custom_price = 10; // First purshase for product ID 944 

    // Detecting if customer has already bought The targeted product (944) 
    if(is_user_logged_in()){ 
     $customer  = wp_get_current_user(); 
     $customer_id = $customer->ID; // customer ID 
     $customer_email = $customer->email; // customer email 

     if(wc_customer_bought_product($customer_email, $customer_id, $targeted_product_id)) 
      $custom_price = 0; // Set to 0 for other purchases (product ID 944) 
    } 

    foreach ($cart_object->get_cart() as $cart_item) { 
     // When targeted product is in cart we change the price 
     if ($cart_item['product_id'] == $targeted_product_id) { 
      // Woocommerce 3+ compatibility 
      if (version_compare(WC_VERSION, '3.0', '<')) 
       $cart_item['data']->price = $custom_price; 
      else 
       $cart_item['data']->set_price($custom_price); 
     } 
    } 
} 

コードは、あなたのアクティブな子テーマのfunction.phpファイルになります(またはテーマ)または任意のプラグインファイルでも使用できます。

このコードはWooCommerceでもテストされ、動作します。3 +

+0

これは機能しませんでした。 (wc_customer_bought_product($ customer_email、$ customer_id、$ targeted_product_id)) なしの場合は、(!wc_customer_bought_product($ customer_email、$ customer_id、$ targeted_product_id) –

+0

@MujtabaK **更新**申し訳ありませんテスト中に追加した '!'を削除するのを忘れましたが、このコードは動作します... – LoicTheAztec

+0

合意されたコードはうまく動作します。ありがとう。 –

関連する問題