Created
May 27, 2020 09:38
-
-
Save xlawok/94da456ed4787c5e137de83969f85d3c to your computer and use it in GitHub Desktop.
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
function matterhorn_insert_product($product_data){ | |
$post_id = wp_insert_post( array( | |
'post_author' => 1, | |
'post_title' => $product_data['mh_name'], | |
'post_content' => $product_data['mh_desc'], | |
'post_status' => 'publish', | |
'post_type' => "product", | |
) ); | |
wp_set_object_terms( $post_id, 'variable', 'product_type' ); | |
// ///////////////////////////////////////////////// | |
$product = wc_get_product($post_id); | |
update_post_meta( $post_id, 'pk_mh_sale_price',$product_data['mh_sale'] ); | |
update_post_meta( $post_id, 'pk_mh_price',$product_data['mh_price'] ); | |
create_product_attributes($post_id,$product_data['mh_sizes']); | |
foreach ($product_data['mh_sizes'] as $attribute => $qty ) | |
{ | |
$variation_data = array( | |
'attributes' => array( | |
'size' => $attribute, | |
), | |
'sku' => '', | |
'regular_price' => '22.00', | |
'sale_price' => '', | |
'stock_qty' => $qty, | |
); | |
create_product_variation($post_id, $variation_data, $attribute); | |
} | |
} | |
function create_product_variation($product_id, $variation_data, $attr){ | |
$product = wc_get_product($product_id); | |
$variation_post = array( | |
'post_title' => $product->get_name()." - ".$attr, | |
'post_content' => '', | |
'post_status' => 'publish', | |
'post_parent' => $product_id, | |
'post_type' => 'product_variation', | |
'guid' => $product->get_permalink() | |
); | |
// Creating the product variation | |
$variation_id = wp_insert_post( $variation_post ); | |
// Get an instance of the WC_Product_Variation object | |
$variation = new WC_Product_Variation( $variation_id ); | |
foreach ($variation_data['attributes'] as $attribute => $term_name ) | |
{ | |
$taxonomy = 'pa_'.$attribute; // The attribute taxonomy | |
// 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( $attribute ), | |
'query_var' => true, | |
'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug | |
), | |
); | |
} | |
// Check if the Term name exist and if not we create it. | |
if( ! term_exists( $term_name, $taxonomy ) ) | |
wp_insert_term( $term_name, $taxonomy ); // Create the term | |
$term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug | |
// Get the post Terms names from the parent variable product. | |
$post_term_names = wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') ); | |
// Check if the post term exist and if not we set it in the parent variable product. | |
if( ! in_array( $term_name, $post_term_names ) ) | |
wp_set_post_terms( $product_id, $term_name, $taxonomy, true ); | |
// Set/save the attribute data in the product variation | |
update_post_meta( $variation_id, 'attribute_'.sanitize_title($attribute), $term_name ); | |
WC_Product_Variable::sync( $product_id ); | |
} | |
## 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) | |
$variation->save(); // Save the data | |
echo 'ok'; | |
} | |
function create_product_attributes($post_id,$attr) | |
{ | |
$my_sizes=""; | |
foreach ($attr as $attribute => $qty ) | |
{ | |
$my_sizes.=$attribute."|"; | |
} | |
$product_attributes = array | |
( | |
"size" => substr($my_sizes, 0, -1), | |
// size=> "XL|L|M" | |
); | |
foreach($product_attributes as $attribute_name => $values) | |
{ | |
wp_set_object_terms($post_id, $values, $attribute_name); | |
$attributes[sanitize_title($attribute_name)] = array | |
( | |
'name' => wc_clean( $attribute_name ), | |
'value' => $values, | |
'is_visible' => 1, | |
'is_variation' => 1, | |
'is_taxonomy' => 0 | |
); | |
// update it back | |
update_post_meta($post_id, '_product_attributes', $attributes ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment