web-dev-qa-db-ja.com

プラグインからのカスタムWoocommerce属性分類法の作成

Woocommerceを使ってWordpressに製品をインポートするためのプラグインを作りました。商品属性以外はうまくいきます - それらを正しくインポートする方法が見つかりません。

問題は、ダッシュボードで定義されていないカスタム分類法を製品に追加したことです。驚くべきことに私はこれのための方法を見つけることができません。私はwp_insert_term()register_taxonomy()を試しましたが、これらはデータベースのwp_woocommerce_attribute_taxonomiesテーブルに何も追加せず、製品の属性にもダッシュボードの製品属性ページにも見つかりません。私はデータベースのwp_termsテーブルでそれらを見つけることができただけで、私が学んだことからそれはWoocommerceの製品属性が属する場所ではないということを学びました。

update_post_meta()も機能しないようです(メタとして属性を追加することはできますが、Woocommerceの製品属性として必要です)。

商品をインポートする前に、属性が存在しない場合は作成する必要があります。見落としたことをするための機能はありますか?

私がこれまでに持っているものを投稿するべきかどうかわからないが、ここに問題に関連するコードがあります:

// This is an example of what I get from my AJAX input
$product_attributes = array(
    "attr_id_01" => array(
        "name" => "Material",
        "value" => "Metal",
        "is_visible" => 1,
        "is_taxonomy" => 1
    ),
    "attr_id_02" => array(
        "name" => "Type",
        "value" => "Has a handle",
        "is_visible" => 1,
        "is_taxonomy" => 1
    )
);
foreach ($product_attributes_copy as $key => $value) {
    // sanitize_title filter is provided by CyrToLat plugin,
    // it basically makes the string url-friendly, 
    // it's used because names and values could contain Cyrillic, uppercase and spaces
    $filtered_name = apply_filters('sanitize_title', $value['name']);
    $filtered_value = apply_filters('sanitize_title', $value['value']);

    $taxonomy = 'pa_' . $filtered_name;
    $parent_term = term_exists( $filtered_value, $taxonomy );
    $parent_term_id = $parent_term['term_id'];

    if ( ! taxonomy_exists($taxonomy) ) {
        register_taxonomy($taxonomy, 'product', array('label' => $value['name']) );
    }

    // No errors from the following
    $insert_result = wp_insert_term(
        $filtered_value,
        $taxonomy,
        array(
            'description'=> '',
            'slug' => $filtered_value,
            'parent'=> $parent_term_id
        )
    );
}
3
Dmitriy Demir

何らかの理由で、Woocommerceはあなたにこれをさせたくないようです。なぜなら、手動で行わなければならないことは、多くの場合スケーラビリティにとって問題であるからです(可能な値が多い属性がある場合は、言うまでもなく、それらが提供するインターフェースは負荷をかけます 実際 ゆっくり)ちょっと掘り下げた後、これが管理ページが使用するプライベート関数です。

function process_add_attribute($attribute)
{
    global $wpdb;
//      check_admin_referer( 'woocommerce-add-new_attribute' );

    if (empty($attribute['attribute_type'])) { $attribute['attribute_type'] = 'text';}
    if (empty($attribute['attribute_orderby'])) { $attribute['attribute_orderby'] = 'menu_order';}
    if (empty($attribute['attribute_public'])) { $attribute['attribute_public'] = 0;}

    if ( empty( $attribute['attribute_name'] ) || empty( $attribute['attribute_label'] ) ) {
            return new WP_Error( 'error', __( 'Please, provide an attribute name and slug.', 'woocommerce' ) );
    } elseif ( ( $valid_attribute_name = valid_attribute_name( $attribute['attribute_name'] ) ) && is_wp_error( $valid_attribute_name ) ) {
            return $valid_attribute_name;
    } elseif ( taxonomy_exists( wc_attribute_taxonomy_name( $attribute['attribute_name'] ) ) ) {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" is already in use. Change it, please.', 'woocommerce' ), sanitize_title( $attribute['attribute_name'] ) ) );
    }

    $wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute );

    do_action( 'woocommerce_attribute_added', $wpdb->insert_id, $attribute );

    flush_rewrite_rules();
    delete_transient( 'wc_attribute_taxonomies' );

    return true;
}

function valid_attribute_name( $attribute_name ) {
    if ( strlen( $attribute_name ) >= 28 ) {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    } elseif ( wc_check_if_attribute_name_is_reserved( $attribute_name ) ) {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    }

    return true;
}

次のように呼ばれます。

$insert = proccess_add_attribute(array('attribute_name' => 'my-new-slug', 'attribute_label' => 'my-new-attribute', 'attribute_type' => 'text', 'attribute_orderby' => 'menu_order', 'attribute_public' => false));
if (is_wp_error($insert)) { do_something_for_error($insert); }

属性に関連するフィールドは、名前(標準のwpスラッグ)、ラベル(人間が読める形式の属性名)、タイプ( 'select'と 'text'のどちらかを選択できますが、体系的に作成している場合は、おそらく、テキストをデフォルトにするように修正した)、public(インターフェースで「アーカイブを有効にする」というラベルを付け、インターフェースをデフォルトのインターフェースにする)、およびorderby(選択肢は 'menu_order'( 'Custom'と呼ばれる) (インタフェース内の)順序付け、 'name'(自己説明)、 'name_num'(数値名)、および 'id'(term id))

7
Ardidaj