私はウェブページ上にかなり大きなデータベース製品を持っています。このウェブページは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を使用して配列からカスタム製品の属性を追加する最も簡単な方法は何ですか?
あなたの答えをありがとう、それは動作しますが、私はそれに少し問題があります。 wc_cleanは、名前にアクセント付きのchararcterがある場合、空の文字列を返します。コードからwc_cleanを削除すると、何も含まれません。助言がありますか? (これは製品ページで次のようになります。 –
wc_cleanはsanitize_text_fieldを使用します。ここで関数を確認することができます: http://woocommerce.wp-a2z.org/oik_api/sanitize_text_field/ wc_cleanが正しく機能するのを防ぐストリングまたは文字エンコード関数を適用していないかどうかを確認するためにデバッグします。 – Dez
最後に、wc_clean(htmlentities($ key、0、 "ISO8859-1"))を使って動作させることができます。今すぐ完璧に動作し、あなたの速い応答のために再び感謝します! :)(結果:https://s10.postimg.org/v9ft0g14p/image.png) –