init(); } public function init() { } public static function bookstore_exhibition_setup_widgets(){ $bookstore_exhibition_product_image_gallery = array(); $bookstore_exhibition_product_ids = array(); $bookstore_exhibition_product_category= array( 'Most Popular Books' => array( 'Product Name 01', 'Product Name 02', 'Product Name 03', 'Product Name 04', ), ); $bookstore_exhibition_k = 1; foreach ( $bookstore_exhibition_product_category as $bookstore_exhibition_product_cats => $bookstore_exhibition_products_name ) { // Insert porduct cats Start $content = 'This is sample product category'; $bookstore_exhibition_parent_category = wp_insert_term( $bookstore_exhibition_product_cats, // the term 'product_cat', // the taxonomy array( 'description'=> $content, 'slug' => str_replace( ' ', '-', $bookstore_exhibition_product_cats) ) ); // -------------- create subcategory END ----------------- $bookstore_exhibition_n=1; // create Product START foreach ( $bookstore_exhibition_products_name as $key => $bookstore_exhibition_product_title ) { $content = '

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

'; // Create post object $bookstore_exhibition_my_post = array( 'post_title' => wp_strip_all_tags( $bookstore_exhibition_product_title ), 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'product', 'post_category' => [$bookstore_exhibition_parent_category['term_id']] ); // Insert the post into the database $bookstore_exhibition_uqpost_id = wp_insert_post($bookstore_exhibition_my_post); wp_set_object_terms( $bookstore_exhibition_uqpost_id, str_replace( ' ', '-', $bookstore_exhibition_product_cats), 'product_cat', true ); $bookstore_exhibition_product_price = array('$20.46','$20.46','$20.46','$20.46'); $bookstore_exhibition_product_regular_price = array('$20.46','$20.46','$20.46','$20.46'); // $bookstore_exhibition_product_sale_price = array('49','49','49','49'); update_post_meta( $bookstore_exhibition_uqpost_id, '_regular_price', $bookstore_exhibition_product_regular_price[$bookstore_exhibition_n-1] ); update_post_meta( $bookstore_exhibition_uqpost_id, '_price', $bookstore_exhibition_product_price[$bookstore_exhibition_n-1] ); // update_post_meta( $bookstore_exhibition_uqpost_id, '_sale_price', $bookstore_exhibition_product_sale_price[$bookstore_exhibition_n-1] ); array_push( $bookstore_exhibition_product_ids, $bookstore_exhibition_uqpost_id ); // Now replace meta w/ new updated value array $bookstore_exhibition_image_url = get_template_directory_uri().'/images/product/'.$bookstore_exhibition_product_cats.'/' . str_replace(' ', '_', strtolower($bookstore_exhibition_product_title)).'.png'; $bookstore_exhibition_image_name = $bookstore_exhibition_product_title.'.png'; $bookstore_exhibition_upload_dir = wp_upload_dir(); // Set upload folder $bookstore_exhibition_image_data = file_get_contents(esc_url($bookstore_exhibition_image_url)); // Get image data $unique_file_name = wp_unique_filename($bookstore_exhibition_upload_dir['path'], $bookstore_exhibition_image_name); // Generate unique name $bookstore_exhibition_filename = basename($unique_file_name); // Create image file name // Check folder permission and define file location if (wp_mkdir_p($bookstore_exhibition_upload_dir['path'])) { $bookstore_exhibition_file = $bookstore_exhibition_upload_dir['path'].'/'.$bookstore_exhibition_filename; } else { $bookstore_exhibition_file = $bookstore_exhibition_upload_dir['basedir'].'/'.$bookstore_exhibition_filename; } file_put_contents($bookstore_exhibition_file, $bookstore_exhibition_image_data); // Check image file type $wp_filetype = wp_check_filetype($bookstore_exhibition_filename, null); // Set attachment data $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => sanitize_file_name($bookstore_exhibition_filename), 'post_type' => 'product', 'post_status' => 'inherit', ); // Create the attachment $bookstore_exhibition_attach_id = wp_insert_attachment($attachment, $bookstore_exhibition_file, $bookstore_exhibition_uqpost_id); // Define attachment metadata $attach_data = wp_generate_attachment_metadata($bookstore_exhibition_attach_id, $bookstore_exhibition_file); // Assign metadata to attachment wp_update_attachment_metadata($bookstore_exhibition_attach_id, $attach_data); if ( count( $bookstore_exhibition_product_image_gallery ) < 3 ) { array_push( $bookstore_exhibition_product_image_gallery, $bookstore_exhibition_attach_id ); } // // And finally assign featured image to post set_post_thumbnail($bookstore_exhibition_uqpost_id, $bookstore_exhibition_attach_id); ++$bookstore_exhibition_n; } // Create product END ++$bookstore_exhibition_k; } // Add Gallery in first simple product and second variable product START $bookstore_exhibition_product_image_gallery = implode( ',', $bookstore_exhibition_product_image_gallery ); foreach ( $bookstore_exhibition_product_ids as $bookstore_exhibition_product_id ) { update_post_meta( $bookstore_exhibition_product_id, 'bookstore_exhibition_product_image_gallery', $bookstore_exhibition_product_image_gallery ); } } }