<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$woocommerce = new Client(
'http://localhost/wordpress',
'ck_d',
'cs_4cb',
[
'wp_api' => true,
'version' => 'wc/v3'
]
);
//creating product variable type
$data = [
'name' => 'Ship Your Idea',
'type' => 'variable',
'description' => 'Pellentesque habitant morbi tristique senectus et netus et
malesuada fames ac turpis egestas. Vestibulum tortor quam,
feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet
quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
'short_description' => 'Pellentesque habitant morbi tristique senectus et netus e
t malesuada fames ac turpis egestas.',
'categories' => [
[
'id' => 9
],
[
'id' => 14
]
],
'images' => [
[
'src' => 'http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_4_front.jpg'
]
],
'attributes' => [
[
'name' => 'color',
'position' => 0,
'visible' => true,
'variation' => true,
'options' => [
'Black',
'Green'
]
],
[ 'name' => 'size',
'position' => 0,
'visible' => true,
'variation' => true,
'options' =>
['S','M']
]
],
'default_attributes' => [
[
'name' => 'color',
'option' => 'Black'
],
[
'name' => 'size',
'option' => 'M'
]
];
$obj = $woocommerce->post('products',$data);
$product_id = $obj->id;
//product created here now creating variation
$data =
['regular_price' => '9.00',
'status'=>'publish',
'image' => [
'src' =>'https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-760w.JPG'
],
'attributes' =>
[
[
'name' => 'color',
'option' => 'Green'
],
[
'name' => 'Size',
'option' => 'S'
]
]
];
print_r($woocommerce->post('products/'.$product_id.'/variations',$variation));
//product variation created here
0 Comments