Skip to content

Instantly share code, notes, and snippets.

@Giannisduke
Created June 2, 2019 18:30
Show Gist options
  • Save Giannisduke/3bf849667fc44f06639a2b18239884d0 to your computer and use it in GitHub Desktop.
Save Giannisduke/3bf849667fc44f06639a2b18239884d0 to your computer and use it in GitHub Desktop.
Import script for Woocommerce and variable products.
<?php require __DIR__ . '/../htdocs/wp-load.php';
// Decode products.json into an array of products.
$products = json_decode( file_get_contents( __DIR__ . '/products.json' ), true );
// Now we want to loop through and create the top-level product.
foreach ( $products as $product ) {
/**
* A product is just a Wordpress post with a different 'post_type',
* therefore we can use Wordpress' built-in functionality of
* wp_insert_post for the creation of our products, the bare minimum a post
* needs to get created is 'post_title' and 'post_type'
*
* @link(https://developer.wordpress.org/reference/functions/wp_insert_post)
*/
$args = array(
'post_title' => $product['title'],
'post_content' => $product['description'],
'post_status' => 'publish',
'post_type' => 'product'
);
$product_id = wp_insert_post( $args );
/*
* Set the product to a variable product, if you want a simple product,
* then set the value to 'simple'
*/
wp_set_object_terms( $product_id, 'variable', 'product_type' );
/*
* We need to update the meta data for the top-level product before we
* create any variations. It's pretty self-explanatory, play about with
* these to understand what meta data changes.
*/
update_post_meta( $product_id, '_sku', $product['sku'] );
update_post_meta( $product_id, '_visibility', 'visible' );
update_post_meta( $product_id, '_stock_status', 'instock' );
update_post_meta( $product_id, '_sold_individually', '' );
update_post_meta( $product_id, '_manage_stock', 'no' );
update_post_meta( $product_id, '_stock', '' );
update_post_meta( $product_id, '_downloadable', 'no' );
update_post_meta( $product_id, '_virtual', 'no' );
update_post_meta( $product_id, '_price', $product['price'] );
update_post_meta( $product_id, '_regular_price', $product['price'] );
update_post_meta( $product_id, '_sale_price', '' );
update_post_meta( $product_id, '_sale_price_dates_from', '' );
update_post_meta( $product_id, '_sale_price_dates_to', '' );
update_post_meta( $product_id, 'total_sales', '0' );
update_post_meta( $product_id, '_purchase_note', '' );
update_post_meta( $product_id, '_featured', 'no' );
update_post_meta( $product_id, '_backorders', 'no' );
/**
* $attributes_collection will build up into an array simliar to this:
* It may contain duplication.
* ["Size" => ["XL", "L", "M", "S"], "Colour" => ["Blue"]]
*
* @var array $attributes_collection
*/
$attributes_collection = array();
/**
* A multi-dimensional array containing the Wordpress data needed to insert
* custom attributes for a product.
*
* See further down for an example of the array.
*
* @var array $product_attributes
*/
$product_attributes = array();
/**
* Building up the $attributes_collection array.
*/
foreach ( $product['variations'] as $variation ) {
// Loop through the variations' attributes.
foreach ( $variation['attributes'] as $key => $value ) {
if ( ! array_key_exists( $key, $attributes_collection ) ) {
$attributes_collection[$key] = array();
}
$attributes_collection[$key][] = $value;
}
}
/*
* Loop through the array we just built, and create a Wordpress ready
* multi-dimensional array.
*/
foreach ( $attributes_collection as $attribute_name => $attributes ) {
// Remove any duplication from the attributes.
$attributes = array_unique( $attributes );
// Create a wordpress ready name, this will convert to lowercase and,
// strip out spaces for _ and convert to lowercase. It's often used for
// slugs.
$sanitized_attribute_name = sanitize_title( $attribute_name );
/*
* The array key should be a sanitized version of the NAME of your attribute.
* Use sanitize_title
*
* 'name' = Unsanitized name for the attribute, e.g. Colour, Size
* 'value' = Imploded array of attributes, similiar to that of which
* you would populate the Woocommerce backend with e.g. XL|L|M|S
* 'position' = Order of the attribute.
* 'is_visible' = Whether it should be visible on the front-end.
* 'is_variation' = Whether it could be used for product variations
* 'is_taxonomy' = Is the attribute based off a taxonomy.
*
* 0 = FALSE, 1 = TRUE
*/
$product_attributes[$sanitized_attribute_name] = array(
'name' => $attribute_name,
'value' => implode( '|', $attributes ),
'position' => 0,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0
);
}
/*
* Update _product_attributes post meta to contain a serialized array of our
* product attributes.
*/
update_post_meta( $product_id, '_product_attributes', $product_attributes );
foreach ( $product['variations'] as $variation ) {
// Create a different title based off the product attributes,
// You could also set this within your array get get it from a different
// data source.
$variation_title = $product['title'];
foreach ( $variation['attributes'] as $attribute ) {
$variation_title .= ' - ' . $attribute;
}
/*
* Like we did when creating the top-level product, only this time with
* a post_type of 'product_variation'
*/
$args = array(
'post_title' => $variation_title,
'post_status' => 'publish',
'post_type' => 'product_variation',
'post_parent' => $product_id
);
$variation_id = wp_insert_post( $args );
/**
* Loop through the attributes for the product and insert post meta to
* update the attribute for the product.
*/
foreach ( $variation['attributes'] as $attribute_name => $attribute ) {
$sanitized_attribute_name = sanitize_title( $attribute_name );
// When creating post_meta for the attributes, prefix the
// sanitized attribute name with 'attribute_'
$attribute_key = 'attribute_' . $sanitized_attribute_name;
// Update the post meta from the attribute.
update_post_meta( $variation_id, $attribute_key, $attribute );
}
/*
* Update the SKU from the data source, as well as the prices.
*/
update_post_meta( $variation_id, '_sku', $variation['sku'] );
update_post_meta( $variation_id, '_price', $product['price'] );
update_post_meta( $variation_id, '_regular_price', $product['price'] );
}
}
[{
"title": "T-Shirt",
"description": "A t-shirt fit for all occasions.",
"price": 20.00,
"sku": "TSHIRT",
"variations": [{
"attributes": {
"Colour": "Blue",
"Size": "XL"
},
"sku": "TSHIRTXLB"
}, {
"attributes": {
"Colour": "Blue",
"Size": "L"
},
"sku": "TSHIRTLB"
}, {
"attributes": {
"Colour": "Blue",
"Size": "M"
},
"sku": "TSHIRTMB"
}, {
"attributes": {
"Colour": "Blue",
"Size": "S"
},
"sku": "TSHIRTSB"
}]
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment