how to add products to wordpress woocommerce with PHP

Recently we had to transfer 3,000 product groups and over 20,000 individual product sku’s to a wordpress woocommerce installation. We quickly noticed that there was very little documentation regarding how exactly to set this all up pragmatically.  This post goes over some important aspects to note when performing the conversion

Woocommerce Product Fields

Woocommerce has several standard, meta, attribute, and post data. Specific ones are needed to correctly integrate with woocommerce and have your products show up. The code below demonstrates the most important aspects to remember when uploading your data

[code]

wp_set_object_terms( $post_id, $product->model, ‘product_cat’ );
wp_set_object_terms($post_id, ‘simple’, ‘product_type’);
update_post_meta($post_id, ‘_sku’, $product->part_num);
update_post_meta( $post_id, ‘_regular_price’, str_replace("$","",$product->price) );
update_post_meta( $post_id, ‘_price’, str_replace("$","",$product->price) );
update_post_meta( $post_id, ‘_visibility’, ‘search’ );
update_post_meta( $post_id, ‘_stock_status’, ‘instock’);

[/code]

Upload Process

Luckily, like many well made plugins today, woocommerce is fully integrated into wordpress. So their posts are known as custom posts. This means creating a base woocommerce product listing is no more complicated than creating a custom post for any wordpress plugin. That means you can perform code like this:

[code]

$post = array(
‘post_author’ => $user_id,
‘post_content’ => $product->description,
‘post_status’ => "publish",
‘post_title’ => $product->part_num,
‘post_parent’ => $product->parent_id,
‘post_type’ => "product"
);

//Create post
$post_id = wp_insert_post( $post, $wp_error );
if($post_id){
$attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
add_post_meta($post_id, ‘_thumbnail_id’, $attach_id);

[/code]

Woocommerce Product Groups

This section has some intuitive parts and not so intuitive. Product Groupsare simply posts setup to be “groups”. Products are posts setup to be products AKA “simple”. This is done with the following code which was referenced above:

[code]

wp_set_object_terms($post_id, ‘simple’, ‘product_type’);

[/code]

OR

[code]

wp_set_object_terms($post_id, ‘group’, ‘product_type’);

[/code]

Now, to set a product as part of a group, you simply use the group’s post ID as the post_parent for the products in the post array, during the upload process. However at this point you are only partially there.

Going Transient

We have dubbed this section going transient since it involves an entire process to complete the group/product relationships. During this process, you will need to use the wodpress function set_transient to tell woocommerce, which products are groups. We were saying the same thing. What the Forward thinking thing to do. Yeah PG 🙂 . Actually, to this day, this makes little sense. However, the truth is, woocommerce triggers a function to do this for you, when creating posts via the GUI. So if you expect the products to show up on the group page, you will need to complete this section:

[code]

$children = get_posts( ‘post_parent=’ . $product->post_parent. ‘&post_type=’ . "product". ‘&orderby=menu_order&order=ASC&fields=ids&post_status=any&numberposts=-1’ );
$transient_name = ‘wc_product_children_ids_’ . $product->post_parent;

if(!empty($children)){
set_transient( $transient_name, $children );

[/code]

Conclusion

If you would like to setup an ecommerce solution using the wordpress CMS, then woocommerce is an option. It can handle normal product relationships and attributes. There are some tricky aspects as noted above but its definitely possible. This process resulted in over 20,000 products which were transferred to this platform and are now 100% controllable. If you need help doing the same, reach out to our BePro Software team for development help with woocommerce. You can view rates for development here

You can leave a response, or trackback from your own site.

Leave a Reply


Notice: Undefined variable: user_ID in /home/superdupa/public_html/wp-content/themes/freshlook/comments.php on line 54

You must be logged in to post a comment.