Example 1: woocommerce import product programmatically
function myCustomProduct($product_array)
{
if (!empty($product_array)):
foreach ($product_array as $product):
$product_id = wc_get_product_id_by_sku($product['sku']);
//no product exist with the given SKU so create one
if (!$product_id):
$post = [
'post_author' => '',
'post_content' => $product['content'],
'post_status' => "publish",
'post_title' => wp_strip_all_tags($product['title']),
'post_name' => $product['title'],
'post_parent' => '',
'post_type' => "product",
];
//Create Post
$product_id = wp_insert_post($post, $wp_error);
//set Product Category
wp_set_object_terms($product_id, $product['product_cat'], 'product_cat');
//set product type
wp_set_object_terms($product_id, 'simple', 'product_type');
update_post_meta($product_id, '_sku', $product['sku']);
update_post_meta($product_id, 'total_sales', '0');
//product found
else:
$post = [
'ID' => $product_id,
'post_title' => $product['title'],
'post_content' => $product['content'],
];
$post_id = wp_update_post($post, true);
// if (is_wp_error($post_id))
// {
// $errors = $post_id->get_error_messages();
// foreach ($errors as $error)
// {
// echo $error;
// }
// }
endif;
update_post_meta($product_id, '_visibility', 'visible');
update_post_meta($product_id, '_stock_status', 'instock');
update_post_meta($product_id, '_product_attributes', array());
update_post_meta($product_id, '_manage_stock', "yes");
update_post_meta($product_id, '_backorders', "no");
update_post_meta($product_id, '_stock', $product['qty']);
update_post_meta($product_id, '_price', $product['price']);
//update_post_meta($product_id, '_downloadable', 'yes');
//update_post_meta($product_id, '_virtual', 'yes');
//update_post_meta($product_id, '_regular_price', "1");
//update_post_meta($product_id, '_sale_price', "1");
//update_post_meta($product_id, '_purchase_note', "");
//update_post_meta($product_id, '_featured', "no");
//update_post_meta($product_id, '_weight', "");
//update_post_meta($product_id, '_length', "");
//update_post_meta($product_id, '_width', "");
//update_post_meta($product_id, '_height', "");
//update_post_meta($product_id, '_sale_price_dates_from', "");
//update_post_meta($product_id, '_sale_price_dates_to', "");
//update_post_meta($product_id, '_price', "1");
//update_post_meta($product_id, '_sold_individually', "");
endforeach;
endif;
}
Example 2: export product data from woocommerce mysql
-- If you need to export products from WooCommerce using MySQL then
-- try out the code below:
SELECT
p.ID,
p.post_title,
`post_content`,
`post_excerpt`,
t.name AS product_category,
t.term_id AS product_id,
t.slug AS product_slug,
tt.term_taxonomy_id AS tt_term_taxonomia,
tr.term_taxonomy_id AS tr_term_taxonomia,
MAX(CASE WHEN pm1.meta_key = '_stock' then pm1.meta_value ELSE NULL END) as stock,
MAX(CASE WHEN pm1.meta_key = '_price' then pm1.meta_value ELSE NULL END) as price,
MAX(CASE WHEN pm1.meta_key = '_regular_price' then pm1.meta_value ELSE NULL END) as regular_price,
MAX(CASE WHEN pm1.meta_key = '_sale_price' then pm1.meta_value ELSE NULL END) as sale_price,
MAX(CASE WHEN pm1.meta_key = '_sku' then pm1.meta_value ELSE NULL END) as sku
FROM wp_posts p
LEFT JOIN wp_postmeta pm1 ON pm1.post_id = p.ID
LEFT JOIN wp_term_relationships AS tr ON tr.object_id = p.ID
JOIN wp_term_taxonomy AS tt ON tt.taxonomy = 'product_cat' AND tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN wp_terms AS t ON t.term_id = tt.term_id
WHERE p.post_type in('product', 'product_variation') AND p.post_status = 'publish' AND p.post_content <> ''
GROUP BY p.ID,p.post_title
ORDER BY `p`.`ID` ASC
-- Now you can export the results to a .csv file :)
-- Happy coding, my homies <3