Created
March 18, 2021 10:28
-
-
Save Tusko/d647efecf9388dca0d74441c8427db39 to your computer and use it in GitHub Desktop.
Woocommerce import from json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": "new24", | |
"category_id": "new-category", | |
"category_name": "Моя нова категорія", | |
"category_published": true, | |
"name": "Новий товар lab24", | |
"private": false, | |
"price": 199, | |
"pricelist": [ | |
{ | |
"id": "new24-new-import", | |
"lab_id": "newlabid", | |
"name": "\u0422\u0435\u0441\u0442 \u0456\u043c\u043f\u043e\u0440\u0442 I1z", | |
"days": 2, | |
"price": 99, | |
"enabled": true | |
},{ | |
"id": "new24-diagen", | |
"lab_id": "diagen", | |
"name": "\u0422\u0435\u0441\u0442 \u0456\u043c\u043f\u043e\u0440\u0442 I1z", | |
"days": 6, | |
"price": 101, | |
"enabled": true | |
}] | |
}, | |
{ | |
"id": "zagalKrov1", | |
"category_id": "nyrky", | |
"category_name": "\u0422\u0435\u0441\u0442\u043e\u0432\u0438\u0439 \u0456\u043c\u043f\u043e\u0440\u0442", | |
"category_published": true, | |
"name": "Загальний аналіз Lab24.io", | |
"private": false, | |
"price": 122, | |
"pricelist": [{ | |
"id": "bilorubin-general-dila-1", | |
"lab_id": "dila", | |
"name": "\u0422\u0435\u0441\u0442 \u0456\u043c\u043f\u043e\u0440\u0442 I1z", | |
"days": 6, | |
"price": 109, | |
"enabled": true | |
}, | |
{ | |
"id": "zagal26212", | |
"lab_id": "importSlugID", | |
"name": "\u0422\u0435\u0441\u0442 \u0456\u043c\u043f\u043e\u0440\u0442 I1z", | |
"days": 6, | |
"price": 111, | |
"enabled": true | |
}] | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('rest_api_init', function() { | |
register_rest_route('wpaImport/v3', 'import', array( | |
'methods' => 'POST', | |
'callback' => 'wc_wpaImport', | |
'permission_callback' => '__return_true', | |
)); | |
}); | |
function wc_wpaImport($request) { | |
global $wpdb; | |
$importData = json_decode($request->get_body()); | |
foreach($importData as $incoming) { | |
// manipulate with product | |
$post_id = wc_get_product_id_by_sku($incoming->id); | |
if($post_id == 0) { | |
$post_id = wp_insert_post(array( | |
'post_author' => 1, | |
'post_title' => mb_convert_case($incoming->name, MB_CASE_TITLE, "UTF-8"), | |
'post_status' => $incoming->private ? 'private' : 'publish', | |
'post_type' => 'product', | |
)); | |
$_product = new WC_Product_Variable($post_id); | |
} else { | |
$_product = wc_get_product($post_id); | |
} | |
$_product->set_sku($incoming->id); | |
$_product->set_name(mb_convert_case($incoming->name, MB_CASE_TITLE, "UTF-8")); | |
$_product->set_status($incoming->private ? 'private' : 'publish'); | |
//manipulate with category | |
$cat = get_term_by('slug', $incoming->category_id, 'product_cat'); | |
if(false == $cat) { | |
$cat = wp_insert_term($incoming->category_name, 'product_cat', array( | |
'parent' => 0, | |
'slug' => $incoming->category_id | |
)); | |
} else { | |
wp_update_term($cat->term_id, 'product_cat', [ | |
'name' => $incoming->category_name | |
]); | |
} | |
$cid = $cat->term_id ? $cat->term_id : $cat['term_id']; | |
update_field('category_published', $incoming->category_published, 'product_cat_' . $cid); | |
wp_set_post_terms($_product->get_id(), $cid, 'product_cat', false); | |
if($incoming->pricelist) { | |
foreach($incoming->pricelist as $price) { | |
$variation_data = array( | |
'attributes' => array( | |
'labs' => $price->lab_id | |
), | |
'sku' => $price->id, | |
'description' => $price->name, | |
'days' => $price->days, | |
'regular_price' => $incoming->price, | |
'enabled' => $price->enabled, | |
'sale_price' => $price->price | |
); | |
create_product_variation($_product, $variation_data); | |
// var_dump(wc_get_product($vv)); | |
} | |
} | |
$_product->save(); | |
} | |
exit; | |
} | |
/** | |
* Create a product variation for a defined variable product ID. | |
* | |
* @param int $product_id | Post ID of the product parent variable product. | |
* @param array $variation_data | The data to insert in the product. | |
* | |
* @since 3.0.0 | |
*/ | |
function create_product_variation($product, $variation_data) { | |
$variation_post = array( | |
'post_title' => $product->get_name(), | |
'post_name' => 'product-' . $product->get_id() . '-variation', | |
'post_status' => 'publish', | |
'post_parent' => $product->get_id(), | |
'post_type' => 'product_variation', | |
'guid' => $product->get_permalink() | |
); | |
// Creating the product variation | |
$findVariationID = wc_get_product_id_by_sku($variation_data['sku']); | |
if($findVariationID != 0) { | |
$variation_id = $findVariationID; | |
} else { | |
$variation_id = wp_insert_post($variation_post); | |
} | |
var_dump($variation_id); | |
// Get an instance of the WC_Product_Variation object | |
$variation = new WC_Product_Variation($variation_id); | |
$variation->set_parent_id($product->get_id()); | |
// Iterating through the variations attributes | |
$product_attributes_data = array(); // Setup array to hold our product attributes data | |
foreach($variation_data['attributes'] as $attribute => $cat_slug) { | |
$taxonomy = 'pa_' . $attribute; // The attribute taxonomy | |
$cat_slug = strtolower($cat_slug); | |
// If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil) | |
if( ! taxonomy_exists($taxonomy)) { | |
register_taxonomy( | |
$taxonomy, | |
'product_variation', | |
array( | |
'hierarchical' => false, | |
'label' => ucfirst($taxonomy), | |
'query_var' => true, | |
'rewrite' => array('slug' => sanitize_title($taxonomy)), // The base slug | |
), | |
); | |
} | |
// Check if the Term name exist and if not we create it. | |
if( ! term_exists($cat_slug, $taxonomy)) { | |
wp_insert_term($cat_slug, $taxonomy); | |
} // Create the term | |
// Get the post Terms names from the parent variable product. | |
$slugs = wp_get_post_terms($product->get_id(), $taxonomy, array( | |
'fields' => 'id=>slug', | |
)); | |
$attribute_tax = get_term_by('slug', $cat_slug, $taxonomy); | |
// Check if the post term exist and if not we set it in the parent variable product. | |
if( ! in_array($cat_slug, $slugs)) { | |
wp_set_post_terms($product->get_id(), [$attribute_tax->term_id], $taxonomy, true); | |
} | |
// Set/save the attribute data in the product variation | |
update_post_meta($variation_id, 'attribute_' . $taxonomy, $cat_slug); | |
$product_attributes_data[ $taxonomy ] = [ | |
'name' => $taxonomy, | |
'value' => '', | |
'is_visible' => '1', | |
'is_variation' => '1', | |
'is_taxonomy' => '1' | |
]; | |
} | |
// set attributes to product | |
update_post_meta($product->get_id(), '_product_attributes', $product_attributes_data); | |
// set product name by lab | |
$variation->set_description($variation_data['description']); | |
// add custom fields | |
update_post_meta($variation_id, 'days', (int) $variation_data['days']); | |
update_post_meta($variation_id, '_virtual', true); | |
## Set/save all other data | |
// SKU | |
if( ! empty($variation_data['sku'])) { | |
$variation->set_sku($variation_data['sku']); | |
} | |
// Prices | |
if(empty($variation_data['sale_price'])) { | |
$variation->set_price($variation_data['regular_price']); | |
} else { | |
$variation->set_price($variation_data['sale_price']); | |
$variation->set_sale_price($variation_data['sale_price']); | |
} | |
$variation->set_regular_price($variation_data['regular_price']); | |
// Stock | |
if( ! empty($variation_data['stock_qty'])) { | |
$variation->set_stock_quantity($variation_data['stock_qty']); | |
$variation->set_manage_stock(true); | |
$variation->set_stock_status(''); | |
} else { | |
$variation->set_manage_stock(false); | |
} | |
$variation->set_weight(''); // weight (reseting) | |
return $variation->save(); // Save the data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment