Guest User

Untitled

a guest
Feb 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. foreach ($preparedProducts['create'] as $key => $product) {
  2. $idProduct = $this->createProduct($product, $brand);
  3. }
  4.  
  5. private function createProduct($product, $brand)
  6. {
  7. global $wpdb;
  8. $item = [
  9. 'post_title' => $product['name'],
  10. 'post_status' => 'publish',
  11. 'post_type' => 'product'
  12. ];
  13. $postId = wp_insert_post($item);
  14.  
  15. $meta_keys = [];
  16. $meta_keys['_visibility'] = 'visible';
  17. $meta_keys['_stock_status'] = 'instock';
  18. $meta_keys['wholesale_customer_wholesale_price'] = $product['wholesale_price'];
  19. $meta_keys['wholesale_customer_have_wholesale_price'] = $product['have_wholesale_price'];
  20. $meta_keys['_price'] = $product['price'];
  21. $meta_keys['_regular_price'] = $product['price'];
  22. $meta_keys['_weight'] = $product['weight'];
  23. $meta_keys['_sku'] = $product['sku'];
  24. $meta_keys['_stock'] = $product['stock_quantity'];
  25. $meta_keys['_manage_stock'] = 'yes';
  26. $meta_keys['_id_ds'] = $product['id_ds'];
  27. $meta_keys['_ean'] = $product['ean'];
  28. //$meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($product['attributes']));;
  29.  
  30. /* SET Attributes */
  31. $meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($this->createAttributes($postId, $product['attributes'])));
  32. /* SET Attributes */
  33.  
  34. $custom_fields = [];
  35. $place_holders = [];
  36. $query_string = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
  37. foreach($meta_keys as $key => $value) {
  38. array_push($custom_fields, $postId, $key, $value);
  39. $place_holders[] = "('%d', '%s', '%s')";
  40. }
  41. $query_string .= implode(', ', $place_holders);
  42. $wpdb->query($wpdb->prepare("$query_string ", $custom_fields));
  43.  
  44. $this->generateFeaturedImage($product['image'], $postId);
  45.  
  46. wp_set_object_terms( $postId, 'simple', 'product_type' );
  47. wp_set_object_terms($postId, get_term_by('name', $brand->name,'product_cat')->term_id, 'product_cat');
  48.  
  49. //update_post_meta($postId, '_product_attributes', $product['attributes']);
  50. return $postId;
  51. }
  52.  
  53. private function generateFeaturedImage($imageUrl, $postId, $isCategory = false)
  54. {
  55. $imageUrl = $this->escapefileUrl($imageUrl);
  56. $uploadDir = wp_upload_dir();
  57. $filename = str_replace("%20", "-", basename($imageUrl));
  58.  
  59. if(wp_mkdir_p($uploadDir['path'])) {
  60. $file = $uploadDir['path'] . '/' . $filename;
  61. } else {
  62. $file = $uploadDir['basedir'] . '/' . $filename;
  63. }
  64. file_put_contents($file, file_get_contents($imageUrl));
  65.  
  66. $fileType = wp_check_filetype($filename, null );
  67. $attachment = array(
  68. 'post_mime_type' => $fileType['type'],
  69. 'post_title' => sanitize_file_name($filename),
  70. 'post_content' => '',
  71. 'post_status' => 'inherit'
  72. );
  73. $attachId = wp_insert_attachment( $attachment, $file, $postId );
  74. require_once(ABSPATH . 'wp-admin/includes/image.php');
  75. $attachData = wp_generate_attachment_metadata( $attachId, $file );
  76. wp_update_attachment_metadata( $attachId, $attachData );
  77.  
  78. if ($isCategory == false ) {
  79. set_post_thumbnail( $postId, $attachId );
  80. }
  81. else {
  82. update_term_meta($postId, 'thumbnail_id', $attachId);
  83. }
  84. }
Add Comment
Please, Sign In to add comment