ShopContent.php
154 lines
| 1 | <?php |
| 2 | namespace Kubio\Ai; |
| 3 | |
| 4 | use Kubio\Core\Importer; |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | |
| 7 | class ShopContent { |
| 8 | public static function save_products_categories( $categories = array() ) { |
| 9 | $saved_categories = array(); |
| 10 | if ( ! empty( $categories ) ) { |
| 11 | foreach ( $categories as $category ) { |
| 12 | $category_title = Arr::get( $category, 'title' ); |
| 13 | $image_keywords = Arr::get( $category, 'keywords' ); |
| 14 | |
| 15 | $saved_category = static::create_category( |
| 16 | array( |
| 17 | 'title' => $category_title, |
| 18 | 'image_keywords' => $image_keywords, |
| 19 | ) |
| 20 | ); |
| 21 | |
| 22 | if ( $saved_category ) { |
| 23 | $saved_categories[] = $saved_category; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return $saved_categories; |
| 29 | } |
| 30 | public static function save_products_by_category( $products = array(), $category_id = 0 ) { |
| 31 | $posts = array(); |
| 32 | if ( ! empty( $products ) ) { |
| 33 | foreach ( $products as $product ) { |
| 34 | $post_id = self::create_product( |
| 35 | array( |
| 36 | 'title' => $product['title'], |
| 37 | 'imageKeywords' => $product['imageKeywords'], |
| 38 | 'imageURL' => $product['imageURL'], |
| 39 | 'category_id' => $category_id, |
| 40 | ) |
| 41 | ); |
| 42 | |
| 43 | if ( ! is_wp_error( $post_id ) ) { |
| 44 | $posts[] = $post_id; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | return $posts; |
| 49 | } |
| 50 | |
| 51 | public static function create_category( $category_details = array() ) { |
| 52 | $saved_category = null; |
| 53 | |
| 54 | $category_title = Arr::get( $category_details, 'title' ); |
| 55 | $image_keywords = Arr::get( $category_details, 'image_keywords' ); |
| 56 | |
| 57 | $existing_category = term_exists( $category_title, 'product_cat' ); |
| 58 | if ( $existing_category ) { |
| 59 | $saved_category = get_term( $existing_category['term_id'], 'product_cat' ); |
| 60 | } else { |
| 61 | $term_result = wp_insert_term( |
| 62 | $category_title, // the term |
| 63 | 'product_cat' // the taxonomy |
| 64 | ); |
| 65 | if ( is_wp_error( $term_result ) ) { |
| 66 | return null; |
| 67 | } |
| 68 | $term_id = $term_result['term_id']; |
| 69 | $term = get_term( $term_id, 'product_cat' ); |
| 70 | if ( $image_keywords ) { |
| 71 | $attach_id = PostImage::get_featured_image_from_keywords( |
| 72 | $image_keywords |
| 73 | ); |
| 74 | |
| 75 | if ( $attach_id ) { |
| 76 | update_term_meta( $term_id, 'thumbnail_id', $attach_id ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | $saved_category = $term; |
| 81 | } |
| 82 | if ( ! empty( $saved_category ) ) { |
| 83 | return array( |
| 84 | 'id' => $saved_category->term_id, |
| 85 | 'name' => $saved_category->name, |
| 86 | 'link' => get_term_link( $saved_category->term_id ), |
| 87 | ); |
| 88 | } else { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public static function create_product( $product_details = array() ) { |
| 94 | $product = new \WC_Product_Simple(); |
| 95 | |
| 96 | $product->set_name( $product_details['title'] ); // product title |
| 97 | |
| 98 | $product->set_slug( sanitize_title( $product_details['title'] ) ); |
| 99 | |
| 100 | // random price |
| 101 | $regular_price = \wp_rand( 10, 300 ); |
| 102 | $product->set_regular_price( $regular_price ); // in current shop currency |
| 103 | |
| 104 | $product->set_sale_price( $regular_price * 0.75 ); |
| 105 | |
| 106 | $product->set_short_description( |
| 107 | self::generate_ipsum( |
| 108 | wp_rand( 1, 2 ) |
| 109 | ) |
| 110 | ); |
| 111 | $product->set_description( |
| 112 | self::generate_ipsum( |
| 113 | wp_rand( 5, 8 ) |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | $attach_id = null; |
| 118 | |
| 119 | if ( isset( $product_details['imageURL'] ) && $product_details['imageURL'] ) { |
| 120 | $image = Importer::importRemoteFile( |
| 121 | $product_details['imageURL'] |
| 122 | ); |
| 123 | |
| 124 | if ( $image ) { |
| 125 | $attach_id = $image['id']; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if ( ! $attach_id ) { |
| 130 | $attach_id = PostImage::get_featured_image_from_keywords( |
| 131 | $product_details['imageKeywords'] |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | if ( $attach_id ) { |
| 136 | $product->set_image_id( $attach_id ); |
| 137 | } |
| 138 | |
| 139 | $product->set_featured( true ); |
| 140 | $product->set_category_ids( array( $product_details['category_id'] ) ); |
| 141 | |
| 142 | $product->add_meta_data( '_kubio_created_product', 1 ); |
| 143 | $product->save(); |
| 144 | |
| 145 | return $product->get_id(); |
| 146 | } |
| 147 | |
| 148 | public static function generate_ipsum( $paragraphs = 6 ) { |
| 149 | $faker = \Faker\Factory::create(); |
| 150 | // faker lorem |
| 151 | return $faker->paragraphs( $paragraphs, true ); |
| 152 | } |
| 153 | } |
| 154 |