2016-08-29 4 views
1

WooCommerceで注文ステータスが「完了」になると、新しいカテゴリを商品に適用したいと考えています。商品が(カテゴリA)にあり、注文ステータス「完了」に適用(カテゴリB)したいとします。注文ステータスが完了したら商品カテゴリを変更してください

これを行う方法はありますか?

私は、チュートリアルのカップルを見つけたが、それらを結合する方法がわからない:

https://wordpress.org/support/topic/automatically-add-posts-to-a-category-conditionally

https://wordpress.org/support/topic/woocommerce-on-order-complete-insert-quantity-data-into-custom-database-table

どのように私はこれを達成することができますか?

ありがとうございます!

答えて

2

更新(WooCommerceとの互換性3+)

あなたが製品のwoocommerceカテゴリを変更すると、あなたは、カテゴリIDのいずれかを受け入れるwp_set_object_terms()ネイティブWordPressの機能を使用する必要がありますまたはスラッグ'product_cat'分類法パラメータおよびなし'category'です。

woocommerce_order_status_completedフックは、古典的状態に順序変更が完了したときにコールバック関数を発射するために使用されます。

これはコードです:

add_action('woocommerce_order_status_completed', 'add_category_to_order_items_on_competed_status' 10, 1); 

function add_category_to_order_items_on_competed_status($order_id) { 

    // set your category ID or slug 
    $your_category = 'my-category-slug'; // or $your_category = 123; 

    $order = wc_get_order($order_id); 

    foreach ($order->get_items() as $item_id => $product_item) { 
     // compatibility with WC +3 
     if (version_compare(WC_VERSION, '3.0', '<')) 
      $product_id = $product_item['product_id']; 
     else 
      $product_id = $product_item->get_product_id(); 

     wp_set_object_terms($product_id, $your_category, 'product_cat'); 
    } 
} 

それとも、ステータスが「完了」の順序をフィルタします条件関数でもwoocommerce_order_status_changedフックを使用することができます。

add_action('woocommerce_order_status_changed', 'add_category_to_order_items_on_competed_status' 10, 1); 

function add_category_to_order_items_on_competed_status($order_id) { 

    // set your category ID or slug 
    $your_category = 'my-category-slug'; // or $your_category = 123; 

    $order = wc_get_order($order_id); 
    $order_status = $order->post->post_status; 

    if ($order->has_status('completed')) { 
     foreach ($order->get_items() as $item_id => $product_item) { 
      // compatibility with WC +3 
      if (version_compare(WC_VERSION, '3.0', '<')) 
       $product_id = $product_item['product_id']; 
      else 
       $product_id = $product_item->get_product_id(); 

      wp_set_object_terms($product_id, $your_category, 'product_cat'); 
     } 
    } 
} 

このコードが進みますあなたのアクティブな子テーマまたはテーマのfunction.phpファイル。

このコードはテスト済みで、完全に機能します。

+0

HI、functions.phoにコードを貼り付けると、コードの1行目と2行目にエラーが発生します。私は単語(関数)が2行目に欠けていると思いますが、なぜコードの最初の行にエラーが出るのか分かりません。add_action( 'woocommerce_order_status_completed'、 'add_category_to_order_items_on_competed_status' 10、1); – user3814097

+0

はい、これは動作します!ありがとう:) – user3814097

+0

また、私はグループ化された製品を使用しています、グループリストの最初の製品が完了してカテゴリを変更したかどうか確認できますか? – user3814097

0

ご注文完了後に何かしたい場合は、woocommerce_order_status_completedアクションを使用し、カテゴリーを追加するにはwp_set_object_termsを使用する必要があります。したがって、あなたの場合、この関数はうまくいくはずです。

function add_cat_product($order_id) { 
    $post_categories = array(); //Array of category IDs. 
    $append = FALSE; // If true, categories will be appended to the post. If false, categories will replace existing categories. 
    $order = new WC_Order($order_id); 
    $items = $order->get_items(); 
    foreach ($items as $item) { 
     //$product_name = $item['name']; 
     $product_id = $item['product_id']; 
     $term_taxonomy_ids = wp_set_object_terms($product_id, $post_categories, 'product_cat', $append); 
     if (is_wp_error($term_taxonomy_ids)) { 
      // There was an error somewhere and the terms couldn't be set. 
     } else { 
      // Success! The post's categories were set. 
     } 
    } 
} 
add_action('woocommerce_order_status_completed', 'add_cat_product'); 
+0

こんにちはRaunak - 答えをありがとうが、上記のコードで[カテゴリB]はどこで定義するのですか? – user3814097

+0

'$ post_categories'変数にカテゴリIDを追加する必要があります。たとえば、カテゴリB idが12の場合、 '$ post_categories = array(12);'と書く必要があります。 –

+0

この行にプロダクトIDを定義する必要もありますか?$ product_id = $ item ['product_id']; ? – user3814097

関連する問題