Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach ($preparedProducts['create'] as $key => $product) {
- $idProduct = $this->createProduct($product, $brand);
- }
- private function createProduct($product, $brand)
- {
- global $wpdb;
- $item = [
- 'post_title' => $product['name'],
- 'post_status' => 'publish',
- 'post_type' => 'product'
- ];
- $postId = wp_insert_post($item);
- $meta_keys = [];
- $meta_keys['_visibility'] = 'visible';
- $meta_keys['_stock_status'] = 'instock';
- $meta_keys['wholesale_customer_wholesale_price'] = $product['wholesale_price'];
- $meta_keys['wholesale_customer_have_wholesale_price'] = $product['have_wholesale_price'];
- $meta_keys['_price'] = $product['price'];
- $meta_keys['_regular_price'] = $product['price'];
- $meta_keys['_weight'] = $product['weight'];
- $meta_keys['_sku'] = $product['sku'];
- $meta_keys['_stock'] = $product['stock_quantity'];
- $meta_keys['_manage_stock'] = 'yes';
- $meta_keys['_id_ds'] = $product['id_ds'];
- $meta_keys['_ean'] = $product['ean'];
- //$meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($product['attributes']));;
- /* SET Attributes */
- $meta_keys['_product_attributes'] = maybe_serialize(wp_unslash($this->createAttributes($postId, $product['attributes'])));
- /* SET Attributes */
- $custom_fields = [];
- $place_holders = [];
- $query_string = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
- foreach($meta_keys as $key => $value) {
- array_push($custom_fields, $postId, $key, $value);
- $place_holders[] = "('%d', '%s', '%s')";
- }
- $query_string .= implode(', ', $place_holders);
- $wpdb->query($wpdb->prepare("$query_string ", $custom_fields));
- $this->generateFeaturedImage($product['image'], $postId);
- wp_set_object_terms( $postId, 'simple', 'product_type' );
- wp_set_object_terms($postId, get_term_by('name', $brand->name,'product_cat')->term_id, 'product_cat');
- //update_post_meta($postId, '_product_attributes', $product['attributes']);
- return $postId;
- }
- private function generateFeaturedImage($imageUrl, $postId, $isCategory = false)
- {
- $imageUrl = $this->escapefileUrl($imageUrl);
- $uploadDir = wp_upload_dir();
- $filename = str_replace("%20", "-", basename($imageUrl));
- if(wp_mkdir_p($uploadDir['path'])) {
- $file = $uploadDir['path'] . '/' . $filename;
- } else {
- $file = $uploadDir['basedir'] . '/' . $filename;
- }
- file_put_contents($file, file_get_contents($imageUrl));
- $fileType = wp_check_filetype($filename, null );
- $attachment = array(
- 'post_mime_type' => $fileType['type'],
- 'post_title' => sanitize_file_name($filename),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $attachId = wp_insert_attachment( $attachment, $file, $postId );
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $attachData = wp_generate_attachment_metadata( $attachId, $file );
- wp_update_attachment_metadata( $attachId, $attachData );
- if ($isCategory == false ) {
- set_post_thumbnail( $postId, $attachId );
- }
- else {
- update_term_meta($postId, 'thumbnail_id', $attachId);
- }
- }
Add Comment
Please, Sign In to add comment