2017-11-26 25 views
1

私はWooCommerceメンバーシップを使用しており、特定の商品を購入した場合、私はこれをいくつか別々に動かすことができますが、一緒に来るのが難しいです。WooCommerce、別の特定の商品がカートに入っている場合は、商品価格を変更してください

また、購入しなければならないアイテムの販売価格もあります。そのため、アイテムがセール・ウィンドウにあるかどうかを日付で確認しています。その部分は機能しています。

商品がカートに入っているかどうかを確認するために引数を追加すると、この全体が壊れているようです。 2つの機能を別々に実行すると、どちらも機能します。

function mps_registration_price($cart_object) { 
    global $post, $product; 

    // Bail if memberships is not active 
    if (! function_exists('wc_memberships')) { 
     return; 
    } 

    $user_id = get_current_user_id(); 
    date_default_timezone_set("America/Chicago"); 
    $today = time(); 

    // Check if user was never a member 
    if (!wc_memberships_get_user_memberships($user_id)) { 
     if(mps_check_for_product()) { 

     foreach ($cart_object->get_cart() as $cart_item) { 

      // get the product id (or the variation id) 
      $id = $cart_item['data']->get_id(); 

      if($id == 17) { 
        $salePrice = get_post_meta($id, '_sale_price', true); 
        $saleFrom = get_post_meta($id, '_sale_price_dates_from', true); 
        $saleTo = get_post_meta($id, '_sale_price_dates_to', true); 

        if(!empty($salePrice) && $today >= $saleFrom && $today <= $saleTo) { 
         $new_price = $salePrice; 
        } else { 
         $new_price = get_post_meta($id, '_regular_price', true); 
        } 
        // Updated cart item price 
        $cart_item['data']->set_price($new_price); 
       } 
      } 
     } else { 

      foreach ($cart_object->get_cart() as $cart_item) { 

       // get the product id (or the variation id) 
       $id = $cart_item['data']->get_id(); 

       if($id == 17) { 
        $salePrice = get_post_meta($id, '_sale_price', true); 
        $saleFrom = get_post_meta($id, '_sale_price_dates_from', true); 
        $saleTo = get_post_meta($id, '_sale_price_dates_to', true); 

        if(!empty($salePrice) && $today >= $saleFrom && $today <= $saleTo) { 
         $new_price = $salePrice + 50; 
        } else { 
         $new_price = get_post_meta($id, '_regular_price', true); 
        } 
        // Updated cart item price 
        $cart_item['data']->set_price($new_price); 
       } 
      } 
     } 
    } 

} 

add_filter('woocommerce_before_calculate_totals', 'mps_registration_price', 10, 1); 

function mps_check_for_product() { 

    $product_id = 11; 
    $product_cart_id = WC()->cart->generate_cart_id($product_id); 
    $in_cart = WC()->cart->find_product_in_cart($product_cart_id); 

    if ($in_cart) { 
     return true; 
    } else { 
     return false; 
    } 
} 

add_action('woocommerce_before_cart', 'mps_check_for_product'); 

答えて

1

お客様がまだメンバーでない場合は、特定の製品(ID 17)を追加するだけで追加費用が発生します。

2つの分離した関数を必要とせず、2番目の関数が間違った方法でそれを実行しています。あなたが最初に夢中にされた機能でそれを呼び出すならば、それは夢中になる必要はありません。

私はまた... WC_Product方法の代わりに、get_post_meta()を使用して、完全に

をあなたのコードを再検討しているWC_Product方法is_on_sale()は、販売上の製品価格の日付へ/からのように、すべてを管理します。だからあなたはあなたの機能の中でそれを得る必要はありません。あなたのコードで

製品11がカートにあるか、あなたの2カートループ内のカートでないときに違いはありません...だからあなたには、いくつかの変更が加えを作成する必要がすることができる必要があります。

だからあなたのコードは、現在、非常にコンパクトです:

add_filter('woocommerce_before_calculate_totals', 'mps_registration_price', 10, 1); 
function mps_registration_price($cart_object) { 

    // Bail if memberships is not active 
    if (! function_exists('wc_memberships')) return; // Exit 

    // Only for non members 
    $user_id = get_current_user_id(); 
    if (wc_memberships_get_user_memberships($user_id)) return; // Exit 

    // First loop to check if product 11 is in cart 
    foreach ($cart_object->get_cart() as $cart_item){ 
     $is_in_cart = $cart_item['product_id'] == 11 ? true : false; 
    } 

    // Second loop change prices 
    foreach ($cart_object->get_cart() as $cart_item) { 

     // Get an instance of the WC_Product object (or the variation product object) 
     $product = $cart_item['data']; 

     // Method is_on_sale() manage everything (dates…) 
     // Here we target product ID 17 
     if($product->is_on_sale() && $product->get_id() == 17) { 

      // When product 11 is not cart 
      if(! $is_in_cart){ 
       $product->set_price($product->get_sale_price() + 50); 
      } 
     } 
    } 
} 

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

テスト済みで動作します。

+1

非常にニース!ありがとうございました! – RiotAct

+0

商品14が商品17より前のカートに入っているかのように、これを逆にすることはできません。論理は同じであると仮定しますが、価格は変更されません。 – RiotAct

関連する問題