WC REST API | Attributes not uploading

2019-08-05 13:48发布

I have a Attribute called "Color" and it has two attributes "Red" and "Green".
When i run this using WC REST API

Everything is working from the below code, i am stuck with the attributes.

print_r( $client->products->create( array( 
    'title' => 'Nile - Over Counter Basin',
    'sku' => '91081_Nile', 
    'type' => 'simple', 
    'regular_price' => '7260',
    'sale_price' => '5445',
    'description' => 'Nile - Over Counter BasinOver Counter BasinHindware Italian CollectionContemporary design with smooth flowing line Space for toiletries', 
    'dimensions'=>array( 'length' =>'67.5' ,'width' =>'39.5','height'=>'12.5'), 
    'categories'=>array( ' SANITARYWARE' =>'592',' WASHBASIN' =>'650',' Table Top Wash Basin' =>'508'),
    'images' =>Array ('91081_Nile'=>Array('src'=>'http://www.somethingsomething.com/images/products/91081/2.jpg','title'=>'91081_Nile','position'=>'0') ),
    'short_description'=>'Contemporary design with smooth flowing line Space for toiletries <table id="ProductDescriptiontable"><tr><td>Brand</td><td>:</td><td class="thirdcolumn">Hindware</td></tr><tr><td>Product Name</td><td>:</td><td class="thirdcolumn">Nile - Over Counter Basin</td></tr><tr><td>Product Description</td><td>:</td><td class="thirdcolumn">Table Top Wash Basin</td></tr></tr><tr><td>Product Color</td><td>:</td><td class="thirdcolumn">StarwhiteIvory</td></tr></table>',
'attributes' => Array ('name'=>'Color','slug'=>'color','position'=>'0','visible'=>'true','options'=>'Red'),
    'enable_html_short_description' => true,  // This is the line you need to add 
)  ) ) ;

Anand: After adding the attributes in multiple array, the attributes are displayed in correct section, but they are not considered as attributes, .. please see the image, they are seen as a plain text and not as an attributes.
My Code is:

'attributes'=>array(array('name'=>'Color','Slug'=>'color','position'=>'0','visible'=>true,'options'=>'Starwhite'),array('name'=>'Model',
'Slug'=>'model','position'=>'0','visible'=>true,'options'=>'Pedestal Wash Basin'),array('name'=>'Brands','Slug'=>'brands','position'=>'0','visible'=>true,'options'=>'Hindware'),array('name'=>'Washbasin Size','Slug'=>'washbasin-size','position'=>'0','visible'=>true,'options'=>'56 x 46 x 38.5 cm'),array('name'=>'Washbasin Type','Slug'=>'washbasin-type','position'=>'0','visible'=>true,'options'=>'Washbasin With Pedestal'))

enter image description here

1条回答
爷、活的狠高调
2楼-- · 2019-08-05 14:25

You need to pass attributes as an array of arrays, change

'attributes' => Array ('name'=>'Color','slug'=>'color','position'=>'0','visible'=>'true','options'=>'Red'),

to

'attributes' => array( array( 'name'=>'Color','slug'=>'color','position'=>'0','visible'=>'true','options'=>'Red' ) ),

P.S: I am presuming there that the taxonomy and term already exist, and that the taxonomy's type is set to text.

EDIT

When the taxonomy's type is set to "text" pass options as plain text

'options' => 'term'

When the taxonomy's type is set to "select" pass options as an array

'options' => array( 'red', 'white' )

To pass multiple attributes, send them as an array of arrays, for eg:

'attributes'=>array(
    array( 'name'=>'Color', 'slug'=>'color', 'position'=>'0', 'visib‌​le'=>true, 'options'=> array('Starwhite') ),
    array( 'name'=>'Washbasin Type', 'slug'=>'washbasin-type', 'position'=>'0', 'visible'=>true, 'options'=> array(‌​'Washbasin With Pedestal') ),
);
查看更多
登录 后发表回答