Guest User

Untitled

a guest
Dec 12th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /**
  2. * Create a product variation for a defined variable product ID.
  3. *
  4. * @since 3.0.0
  5. * @param int $product_id post ID of the product parent variable product.
  6. * @param array $variation_data post ID of the product parent variable product.
  7. */
  8.  
  9. function create_product_variation( $product_id, $variation_data ){
  10. // Get the Variable product object (parent)
  11. $product = wc_get_product($product_id);
  12.  
  13. $variation_post = array(
  14. 'post_title' => $product->get_title(),
  15. 'post_name' => 'product-'.$product_id.'-variation',
  16. 'post_status' => 'publish',
  17. 'post_parent' => $product_id,
  18. 'post_type' => 'product_variation',
  19. 'guid' => $product->get_permalink()
  20. );
  21.  
  22. // Creating the product variation
  23. $variation_id = wp_insert_post( $variation_post );
  24.  
  25. // Get an instance of the WC_Product_Variation object
  26. $variation = new WC_Product_Variation( $variation_id );
  27.  
  28. // Iterating through the variations attributes
  29. foreach ($variation_data['attributes'] as $attribute => $term_name )
  30. {
  31. $taxonomy = 'pa_'.$attribute; // The attribute taxonomy
  32. $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug
  33.  
  34. // Check if the Term name exist and if not we create it.
  35. if( ! term_exists( $term_name, $taxonomy ) )
  36. wp_insert_term( $term_name, $taxonomy ); // Create the term
  37.  
  38. // Get the post Terms names from the parent variable product.
  39. $post_term_names = wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );
  40.  
  41. // Check if the post term exist and if not we set it in the parent variable product.
  42. if( ! in_array( $term_name, $post_term_names ) )
  43. wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
  44.  
  45. // Set/save the attribute data in the product variation
  46. update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
  47. }
  48.  
  49. ## Set/save all other data
  50.  
  51. // SKU
  52. if( ! empty( $variation_data['sku'] ) )
  53. $variation->set_price( $variation_data['sku'] );
  54.  
  55. // Prices
  56. if( empty( $variation_data['sale_price'] ) ){
  57. $variation->set_price( $variation_data['regular_price'] );
  58. } else {
  59. $variation->set_price( $variation_data['sale_price'] );
  60. $variation->set_sale_price( $variation_data['sale_price'] );
  61. }
  62. $variation->set_regular_price( $variation_data['regular_price'] );
  63.  
  64. // Stock
  65. if( ! empty($variation_data['stock_qty']) ){
  66. $variation->set_stock_quantity( $variation_data['stock_qty'] );
  67. $variation->set_manage_stock(true);
  68. $variation->set_stock_status('');
  69. } else {
  70. $variation->set_manage_stock(false);
  71. }
  72.  
  73. $variation->set_weight(''); // weight (reseting)
  74.  
  75. $variation->save(); // Save the data
  76. }
  77.  
  78. $parent_id = 746; // Or get the variable product id dynamically
  79.  
  80. // The variation data
  81. $variation_data = array(
  82. 'attributes' => array(
  83. 'size' => 'M',
  84. 'color' => 'Green',
  85. ),
  86. 'sku' => '',
  87. 'regular_price' => '22.00',
  88. 'sale_price' => '',
  89. 'stock_qty' => 10,
  90. );
  91.  
  92. // The function to be run
  93. create_product_variation( $parent_id, $variation_data );
Add Comment
Please, Sign In to add comment