2016-10-03 11 views
2

私はウェブページ上にかなり大きなデータベース製品を持っています。このウェブページはPHPで書かれており、今はデータベースをWordpress(WooCommerce)サイトに移行する必要があります。私はすでに製品を追加する方法を考え出しました。その後、WooCommerceに新しいカスタム商品属性をプログラムで追加するには?

include('../wp/wp-load.php'); 
require_once('../wp/wp-admin/includes/media.php'); 
require_once('../wp/wp-admin/includes/file.php'); 
require_once('../wp/wp-admin/includes/image.php'); 

まず、私はPHPの配列に古いデータベースから$アイテムをすべての製品を取得し、:私はこのヘッダにPHPファイル(外ワードプレス)を作ったので、私はWordPressのすべての機能を使用することができますforeachループを使用してこの配列をループします。私はこのコードを持っているループの内側:

["Some custom attribute"] => (string)"Something" 
["Another custom attribute"] => (string)"Something else" 

今、私は追加する必要があります。

$user_id = get_current_user(); 
$post_id = wp_insert_post(array(
    'post_author' => $user_id, 
    'post_title' => $item['termek'], 
    'post_content' => $item['reszletes_leiras'], 
    'post_status' => 'publish', 
    'post_type' => "product", 
)); 
wp_set_object_terms($post_id, 'simple', 'product_type'); 
update_post_meta($post_id, '_visibility', 'visible'); 
update_post_meta($post_id, '_stock_status', 'instock'); 
update_post_meta($post_id, 'total_sales', '0'); 
update_post_meta($post_id, '_downloadable', 'no'); 
update_post_meta($post_id, '_virtual', 'no'); 
update_post_meta($post_id, '_regular_price', $item['brutto_ar']); 
update_post_meta($post_id, '_sale_price', $item['brutto_ar']); 
update_post_meta($post_id, '_purchase_note', ''); 
update_post_meta($post_id, '_featured', 'no'); 
update_post_meta($post_id, '_weight', ''); 
update_post_meta($post_id, '_length', ''); 
update_post_meta($post_id, '_width', ''); 
update_post_meta($post_id, '_height', ''); 
update_post_meta($post_id, '_sku', $item['id']); 
update_post_meta($post_id, '_product_attributes', array()); 
update_post_meta($post_id, '_sale_price_dates_from', ''); 
update_post_meta($post_id, '_sale_price_dates_to', ''); 
update_post_meta($post_id, '_price', ''); 
update_post_meta($post_id, '_sold_individually', ''); 
update_post_meta($post_id, '_manage_stock', 'no'); 
update_post_meta($post_id, '_backorders', 'no'); 
update_post_meta($post_id, '_stock', ''); 

私の問題は、私はまた、このカイルに見えた、$項目[「parameterek」]の配列を持っているということですこれらのフィールドをカスタム属性として製品に追加します。配列キーは名前であり、値は...もちろん値です。私は、このようにそれを実行しようとしましたが、このコードは何もしないようだ:

foreach(array_keys($item['parameterek']) as $key) { 
    register_attribute($key); 
} 
wproduct_set_attributes($post_id, $item['parameterek']); 

function register_attribute($name){ 
    $paname = 'pa_' . htmlspecialchars(stripslashes($name)); 
    $permalinks = get_option('woocommerce_permalinks'); 
    $taxonomy_data = array(
         'hierarchical'   => false, 
         'update_count_callback' => '_update_post_term_count', 
         'labels'    => array(
           'name'    => $name, 
           'singular_name'  => $name, 
           'search_items'  => sprintf(__('Search %s', 'woocommerce'), $label), 
           'all_items'   => sprintf(__('All %s', 'woocommerce'), $label), 
           'parent_item'  => sprintf(__('Parent %s', 'woocommerce'), $label), 
           'parent_item_colon' => sprintf(__('Parent %s:', 'woocommerce'), $label), 
           'edit_item'   => sprintf(__('Edit %s', 'woocommerce'), $label), 
           'update_item'  => sprintf(__('Update %s', 'woocommerce'), $label), 
           'add_new_item'  => sprintf(__('Add New %s', 'woocommerce'), $label), 
           'new_item_name'  => sprintf(__('New %s', 'woocommerce'), $label) 
          ), 
         'show_ui'   => false, 
         'query_var'   => true, 
         'rewrite'   => array(
          'slug'   => empty($permalinks['attribute_base']) ? '' : trailingslashit($permalinks['attribute_base']) . sanitize_title($name), 
          'with_front' => false, 
          'hierarchical' => true 
         ), 
         'sort'    => false, 
         'public'   => true, 
         'show_in_nav_menus' => false, 
         'capabilities'  => array(
          'manage_terms' => 'manage_product_terms', 
          'edit_terms' => 'edit_product_terms', 
          'delete_terms' => 'delete_product_terms', 
          'assign_terms' => 'assign_product_terms', 
         ) 
        ); 
    register_taxonomy($paname, array('product'), $taxonomy_data); 
} 

function wcproduct_set_attributes($post_id, $attributes) { 
    $i = 0; 
    // Loop through the attributes array 
    foreach ($attributes as $name => $value) { 
     $product_attributes[$i] = array (
      'name' => 'pa_' . htmlspecialchars(stripslashes($name)), // set attribute name 
      'value' => $value, // set attribute value 
      'position' => 1, 
      'is_visible' => 1, 
      'is_variation' => 0, 
      'is_taxonomy' => 1 
     ); 

     $i++; 
    } 
    update_post_meta($post_id, '_product_attributes', $product_attributes); 
} 

だから私の質問は次のとおりです。PHPを使用して配列からカスタム製品の属性を追加する最も簡単な方法は何ですか?

答えて

5

私は最近、あなたと同じ問題を抱えています。 まず、各属性のオプションについて考える必要があります。 0

is_variationに設定されている場合

is_visibleは1に設定されている場合、製品ページ内の属性が表示されるよう、および編集の製品ページでのみ表示されますと、カスタム属性の同じ製品のバリエーションを作成するために使用されます。 1に設定すると、「post_parentは」post_idの

is_taxonomy

'post_parent' => $original_post_idなどが使用されているオリジナル製品として設定され、それは属性を通じて、元の製品のポストに関連する 'post_type' => "product_variation"との記事を探しますので、バリエーションは、存在していることWoocommerceに指示します前記カスタム属性に基づいて前記製品を分類する。たとえば、衣服が販売される商品であると考えると、性別に応じて商品を分類するためのgenderというカスタム属性を作成します。

それはあなたの質問を読んでいるようだとして、あなただけの単純なカスタムは、商品ページに表示文字列値を持つ属性が必要です。

["Some custom attribute"] => (string)"Something" 
["Another custom attribute"] => (string)"Something else" 

ので、あなたのループを実装する方法は、このようなものになるだろう:

[loop] 

$user_id = get_current_user(); 
$post_id = wp_insert_post(array(
    'post_author' => $user_id, 
    'post_title' => $item['termek'], 
    'post_content' => $item['reszletes_leiras'], 
    'post_status' => 'publish', 
    'post_type' => "product", 
)); 
wp_set_object_terms($post_id, 'simple', 'product_type'); 
update_post_meta($post_id, '_visibility', 'visible'); 
update_post_meta($post_id, '_stock_status', 'instock'); 
update_post_meta($post_id, 'total_sales', '0'); 
update_post_meta($post_id, '_downloadable', 'no'); 
update_post_meta($post_id, '_virtual', 'no'); 
update_post_meta($post_id, '_regular_price', $item['brutto_ar']); 
update_post_meta($post_id, '_sale_price', $item['brutto_ar']); 
update_post_meta($post_id, '_purchase_note', ''); 
update_post_meta($post_id, '_featured', 'no'); 
update_post_meta($post_id, '_weight', ''); 
update_post_meta($post_id, '_length', ''); 
update_post_meta($post_id, '_width', ''); 
update_post_meta($post_id, '_height', ''); 
update_post_meta($post_id, '_sku', $item['id']); 
update_post_meta($post_id, '_product_attributes', array()); 
update_post_meta($post_id, '_sale_price_dates_from', ''); 
update_post_meta($post_id, '_sale_price_dates_to', ''); 
update_post_meta($post_id, '_price', ''); 
update_post_meta($post_id, '_sold_individually', ''); 
update_post_meta($post_id, '_manage_stock', 'no'); 
update_post_meta($post_id, '_backorders', 'no'); 
update_post_meta($post_id, '_stock', ''); 
$i = 0; 
$product_attributes = array(); 
foreach($item['parameterek'] as $key => $value){ 
    $product_attributes[sanitize_title($key)] = array (
      'name' => wc_clean($key), // set attribute name 
      'value' => $value, // set attribute value 
      'position' => $i, 
      'is_visible' => 1, 
      'is_variation' => 0, 
      'is_taxonomy' => 0 
     ); 
    $i++; 
} 
update_post_meta($post_id, '_product_attributes', $product_attributes); 
[/loop] 

あなたの製品の1つにバリエーションがある場合は、まず既存の製品を作成します。その後、前記カスタム属性をバリエーションとして保存する。

$attr[sanitize_title($custom_attribute_name)] = array(
    'name'=> wc_clean($custom_attribute_name), 
    'value'=> $string_custom_attr_values, 
    'position' => $position, 
    'is_visible' => 1, 
    'is_variation' => 1, 
    'is_taxonomy' => 0 
); 

ここで、$ string_custom_attr_valuesは、|で区切られたバリエーションの値の文字列です。 。たとえば、バリエーションのサイズのカスタム属性$string_custom_attr_values = 'S|M|L|XL'

そして、あなたは$itemsが言った、製品のバリエーションを作成するためにあなたが既に持っている1、大きさの変化の前の例以下のようなものの内側に新しいループが必要になります。私が持っている

$string_custom_attr_values = 'S|M|L|XL'; 
$arr_custom_attr_values = explode("|", $string_custom_attr_values); 
$total_variations = count($arr_custom_attr_values); 
$variation_post_id = $post_id; 
for($i = 1; $i <= $total_variations; $i++) { 
    $variation_post_id += $i; 
    $variation_post = array(
     'post_title' => 'Variation #' . $variation_post_id . ' of ' . $item['termek'], 
     'post_name'  => 'product-' . $variation_post_id . '-variation', 
     'post_status' => 'publish', 
     'post_parent' => $post_id, 
     'post_type'  => 'product_variation', 
     'guid'   => home_url() . '/product_variation/product-' . $variation_post_id . '-variation', 
     'menu_order' => $i 
); 

    // Insert the variation post into the database 
    $variation_post_id = wp_insert_post($variation_post); 
    update_post_meta($variation_post_id, 'attribute_'.$custom_attribute_name, $arr_custom_attr_values[$i-1]); 

    /*Rest of the post_meta like in the base product*/ 

}//end for 

・ホープ十分にはっきりしていた。

+0

あなたの答えをありがとう、それは動作しますが、私はそれに少し問題があります。 wc_cleanは、名前にアクセント付きのchararcterがある場合、空の文字列を返します。コードからwc_cleanを削除すると、何も含まれません。助言がありますか? (これは製品ページで次のようになります。 –

+0

wc_cleanはsanitize_text_fieldを使用します。ここで関数を確認することができます: http://woocommerce.wp-a2z.org/oik_api/sanitize_text_field/ wc_cleanが正しく機能するのを防ぐストリングまたは文字エンコード関数を適用していないかどうかを確認するためにデバッグします。 – Dez

+1

最後に、wc_clean(htmlentities($ key、0、 "ISO8859-1"))を使って動作させることができます。今すぐ完璧に動作し、あなたの速い応答のために再び感謝します! :)(結果:https://s10.postimg.org/v9ft0g14p/image.png) –

関連する問題