Import products into WooCommerce: add images, variations, categories, upsells, crosssells
Work for WooCommmerce 2.0
- Fetch image from remote server, first image will be added as featured image, other added into product gallery
- Add 2 variations: Blue and Red with different stock level and price
require( 'class-woocommerce-product-importer.php' );
$wc_importer = new WooCommerce_Product_Importer();
$args = array(
'name' => 'Mobile WX6',
'status' => 'publish',
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sed ullamcorper velit. Vestibulum sodales sapien vel consectetur hendrerit. Ut lacinia felis velit, vitae tincidunt mauris ultrices et.',
'excerpt' => 'Wonderful product',
'tags' => array('mobile', 'wireless'),
'images' => array('https://remote.com/images/mobilewe.jpg', 'https://remote.com/images/mobilewe_back.jpg'),
'categories' => array('Mobile Device'),
'fetch_image_type' => 'remote',
'type' => 'simple',
'metas' => array(
'_sku' => 'WX620013',
'_regular_price' => '120.95',
'_sale_price' => '99.99',
'_product_attributes' => array(
array(
'name' => 'Color',
'values' => array(
'Blue' => array(
'_sku' => 'WX620013B',
'_manage_stock' => 'yes',
'_regular_price' => '120.95',
'_sale_price' => '99.99',
'_stock' => '8'
),
'Red' => array(
'_sku' => 'WX620013R',
'_manage_stock' => 'yes',
'_regular_price' => '120.95',
'_sale_price' => '89.99',
'_stock' => '34'
)
)
)
)
)
);
$wc_importer->add_product( $args );
require( 'class-woocommerce-product-importer.php' );
$wc_importer = new WooCommerce_Product_Importer();
// Add main category
$args = array(
'name' => 'Bike',
'description' => 'This is the description of the category'
);
$category_id = $wc_importer->add_category( $args );
// Add a subcategory
$args = array(
'name' => 'Road Bike',
'parent' => $category_id,
'description' => 'This is the description of the sub-category'
);
$sub_category_id = $wc_importer->add_category( $args );
require( 'class-woocommerce-product-importer.php' );
$wc_importer = new WooCommerce_Product_Importer();
$images = array(
'https://remote.com/bike_front.jpg', // First image will be defined as featured image
'https://remote.com/bike_back.jpg', // Other images are added as element in the product gallery
'https://remote.com/bike_zoomed.jpg'
);
$product_id = 87;
$wc_importer->add_image_to_product( $product_id $args );
-
v1.2.0 Function add_image_to_product() is now using wp_remote_get() instead of file_get_contents()
-
v1.1.0 New function delete_product() added