Php 手动导入属性

Php 手动导入属性,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我在尝试手动将某些属性导入woocommerce产品时遇到问题。我不知道为什么,脚本正确导入所有整数项,如8、9、10等,但不导入字符串项,如XS、S、M等 以下是代码: $TAGLIA = 'M'; //term size wp_insert_term( $TAGLIA, 'pa_taglia' ); //pa_taglia is a taxonomy $post_id = 2; //parent post ID wp_set_object_terms($post_id, $TAGLIA,

我在尝试手动将某些属性导入woocommerce产品时遇到问题。我不知道为什么,脚本正确导入所有整数项,如8、9、10等,但不导入字符串项,如XS、S、M等

以下是代码:

$TAGLIA = 'M'; //term size
wp_insert_term( $TAGLIA, 'pa_taglia' ); //pa_taglia is a taxonomy

$post_id = 2; //parent post ID
wp_set_object_terms($post_id, $TAGLIA, 'pa_taglia' , true);

global $wpdb;
$row = $wpdb->get_row('select * from ' . $wpdb->prefix . 'terms where name = "' . $TAGLIA . '"');
if($row)
{
    $term_id = $row->term_id;
    $wpdb->insert( 
            $wpdb->prefix . 'term_relationships', 
                array( 
                    'object_id' => $post_id, 
                    'term_taxonomy_id' => $term_id,
                    'term_order' => 0
                ), 
                array( 
                    '%d', 
                    '%d',
                    '%d' 
                ) 
            );
}


$my_post = array(
            'post_title'    => 'Variation of post id #' . $post_id,
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_parent'     => $post_id,
            'post_type'   => 'product_variation'
         );

$relate_post_id = wp_insert_post( $my_post );
$regular_price = get_post_meta($post_id, '_regular_price', true);
$sale_price    = get_post_meta($post_id, '_sale_price', true);
$price         = get_post_meta($post_id, '_price', true);
add_post_meta($relate_post_id, 'attribute_pa_taglia', $TAGLIA, true);
add_post_meta($relate_post_id, '_price', $price, true);
add_post_meta($relate_post_id, '_regular_price', $regular_price, true);
add_post_meta($relate_post_id, '_sale_price', $sale_price, true);
add_post_meta($relate_post_id, '_stock', $QUANTITA, true);
add_post_meta($relate_post_id, '_stock_status', 'instock', true);
add_post_meta($relate_post_id, '_manage_stock', 'yes', true);

我找到了一个解决方案:wordpress似乎管理slug而不是term name,因此,当我插入诸如S、XS、M之类的术语时,代码工作,而当我插入诸如8,5之类的术语时,代码不工作,因为slug变成了8-5

在具体的例子中,我给出了一个大写术语,wordpress保存了一个小写的slug