Woocommerce手动属性导入

时间:2015-11-03 15:51:08

标签: 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, '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);

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案:似乎wordpress管理slug而不是术语名称,因此,当我插入像S,XS,M这样的术语时代码工作,当我插入术语时,代码工作不起作用8,5因为slug变为8-5。

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