2017-11-27 6 views
2

私はWoocommerceバージョン3以降で可変製品(「親」製品)を作成しました。 Wordpressのプラグインから、新しい属性値を持つ製品バリエーション(「子供」製品)をプログラムで作成したいと思います。新しい属性値を使用してWooCommerce製品バリエーションをプログラムで登録します

バリエーション属性は、すでに勘定設定で設定されています。
したがって、1つのバリエーションが作成されるたびに、新しい属性の値もプログラムで作成し、親変数製品に設定する必要があります。

どうすればいいですか?出来ますか?

+1

は、あなたの研究を共有するすべての人を助けます。あなたが試したこと、なぜ があなたのニーズを満たしていなかったのかを教えてください。これは、あなたが時間をかけて自分自身を助けようと試みたことを示しています。答えが明白であることを忘れないでください。 そしてもっと重要なことは、 も参照してください:[ask] – j08691

+1

これらのいずれかを閉じる必要があります - https://stackoverflow.com/questions/47518333/how-to-create-a-variable-product-programmatically-with-two-new-variante-attribut – Adam

答えて

14

定義済みの可変プロダクトIDプロダクトバリエーションを追加(作成)するカスタム機能があります。変数の親製品は、必要な属性を設定する必要があります。

あなたのようにいくつかの情報を提供する必要があります。

  • 属性の配列を/
  • SKU、価格と在庫の値...。

このデータは、フォーマットされた多次元配列(最後の例を参照)に格納する必要があります。

属性値(用語名)が既に存在するかどうかをチェックし、そうでない場合は - 製品属性 のために作成します - それを親変数製品に設定します。

カスタム関数のコード:

/** 
* Create a product variation for a defined variable product ID. 
* 
* @since 3.0.0 
* @param int $product_id | Post ID of the product parent variable product. 
* @param array $variation_data | The data to insert in the product. 
*/ 

function create_product_variation($product_id, $variation_data){ 
    // Get the Variable product object (parent) 
    $product = wc_get_product($product_id); 

    $variation_post = array(
     'post_title' => $product->get_title(), 
     'post_name' => 'product-'.$product_id.'-variation', 
     'post_status' => 'publish', 
     'post_parent' => $product_id, 
     'post_type' => 'product_variation', 
     'guid'  => $product->get_permalink() 
    ); 

    // Creating the product variation 
    $variation_id = wp_insert_post($variation_post); 

    // Get an instance of the WC_Product_Variation object 
    $variation = new WC_Product_Variation($variation_id); 

    // Iterating through the variations attributes 
    foreach ($variation_data['attributes'] as $attribute => $term_name) 
    { 
     $taxonomy = 'pa_'.$attribute; // The attribute taxonomy 

     // Check if the Term name exist and if not we create it. 
     if(! term_exists($term_name, $taxonomy)) 
      wp_insert_term($term_name, $taxonomy); // Create the term 

     $term_slug = get_term_by('name', $term_name, $taxonomy)->slug; // Get the term slug 

     // Get the post Terms names from the parent variable product. 
     $post_term_names = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'names')); 

     // Check if the post term exist and if not we set it in the parent variable product. 
     if(! in_array($term_name, $post_term_names)) 
      wp_set_post_terms($product_id, $term_name, $taxonomy, true); 

     // Set/save the attribute data in the product variation 
     update_post_meta($variation_id, 'attribute_'.$taxonomy, $term_slug); 
    } 

    ## Set/save all other data 

    // SKU 
    if(! empty($variation_data['sku'])) 
     $variation->set_sku($variation_data['sku']); 

    // Prices 
    if(empty($variation_data['sale_price'])){ 
     $variation->set_price($variation_data['regular_price']); 
    } else { 
     $variation->set_price($variation_data['sale_price']); 
     $variation->set_sale_price($variation_data['sale_price']); 
    } 
    $variation->set_regular_price($variation_data['regular_price']); 

    // Stock 
    if(! empty($variation_data['stock_qty'])){ 
     $variation->set_stock_quantity($variation_data['stock_qty']); 
     $variation->set_manage_stock(true); 
     $variation->set_stock_status(''); 
    } else { 
     $variation->set_manage_stock(false); 
    } 

    $variation->set_weight(''); // weight (reseting) 

    $variation->save(); // Save the data 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルに入るか、また、どのプラグイン・ファイルインチ

使用(2つの属性を有する例):

$parent_id = 746; // Or get the variable product id dynamically 

// The variation data 
$variation_data = array(
    'attributes' => array(
     'size' => 'M', 
     'color' => 'Green', 
    ), 
    'sku'   => '', 
    'regular_price' => '22.00', 
    'sale_price' => '', 
    'stock_qty'  => 10, 
); 

// The function to be run 
create_product_variation($parent_id, $variation_data); 
テストさ

と作品。

パート2:

enter image description here

をそして、それはフロントエンドで完璧に動作します:Create programmatically a variable product and two new attributes in Woocommerce

あなたはバックエンドでこれを取得します。

+0

在庫は0になる可能性があります。したがって、おそらくif ::($ variation_data ['stock_qty']){} ($ variation_data ['stock_qty'])){ – Cedric

+0

私はいくつかのデータウェイトを追加しました(もし私が間違っていなければ、バリエーションは親/変数や他のバリエーションとは異なるウェイトを持つかもしれません)、description、slug。 – Cedric

+0

if($ variation_data ['weight']))) $ variation-> set_weight($ variation_data ['weight']); if($ variation_data ['long_description']))&& strlen($ variation_data ['long_description']) $ variation-> set_description($ variation_data ['long_description']); if($ variation_data ['slug']))&& strlen($ variation_data ['slug']) $ variation-> set_slug($ variation_data ['slug']); – Cedric

1

LoicTheAztecの答えを展開すると、属性の組み合わせが彼のコードに次のように変更されているかどうかを確認できます。

function create_update_product_variation($product_id, $variation_data){ 

    if(isset($variation_data['variation_id'])) { 

     $variation_id = $variation_data['variation_id']; 

    } else { 

     // if the variation doesn't exist then create it 

     // Get the Variable product object (parent) 
     $product = wc_get_product($product_id); 

     $variation_post = array(
      'post_title' => $product->get_title(), 
      'post_name' => 'product-'.$product_id.'-variation', 
      'post_status' => 'publish', 
      'post_parent' => $product_id, 
      'post_type' => 'product_variation', 
      'guid'  => $product->get_permalink() 
    ); 

     // Creating the product variation 
     $variation_id = wp_insert_post($variation_post); 

    } 

    // ... 

} 

使用例

// The variation data 
$variation_data = array(
    'attributes' => array(
     'size' => 'M', 
     'color' => 'Green', 
    ), 
    'sku'   => '', 
    'regular_price' => '22.00', 
    'sale_price' => '1', 
    'stock_qty'  => 1, 
); 

// check if variation exists 
$meta_query = array(); 
foreach ($variation_data['attributes'] as $key => $value) { 
    $meta_query[] = array(
    'key' => 'attribute_pa_' . $key, 
    'value' => $value 
); 
} 

$variation_post = get_posts(array(
    'post_type' => 'product_variation', 
    'numberposts' => 1, 
    'post_parent' => $parent_id, 
    'meta_query' => $meta_query 
)); 

if($variation_post) { 
    $variation_data['variation_id'] = $variation_post[0]->ID; 
} 

create_update_product_variation($product_id, $variation_data); 
関連する問題