create-product.php
376 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateProduct. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateProduct |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\StoreEngine\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use Exception; |
| 19 | |
| 20 | /** |
| 21 | * CreateProduct |
| 22 | * |
| 23 | * @category CreateProduct |
| 24 | * @package SureTriggers |
| 25 | * @author BSF <username@example.com> |
| 26 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 | * @link https://www.brainstormforce.com/ |
| 28 | * @since 1.0.0 |
| 29 | * |
| 30 | * @psalm-suppress UndefinedTrait |
| 31 | */ |
| 32 | class CreateProduct extends AutomateAction { |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Integration type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'StoreEngine'; |
| 41 | |
| 42 | /** |
| 43 | * Action name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $action = 'se_create_product'; |
| 48 | |
| 49 | use SingletonLoader; |
| 50 | |
| 51 | /** |
| 52 | * Register action. |
| 53 | * |
| 54 | * @param array $actions action data. |
| 55 | * @return array |
| 56 | */ |
| 57 | public function register( $actions ) { |
| 58 | $actions[ $this->integration ][ $this->action ] = [ |
| 59 | 'label' => __( 'Create Product', 'suretriggers' ), |
| 60 | 'action' => 'se_create_product', |
| 61 | 'function' => [ $this, 'action_listener' ], |
| 62 | ]; |
| 63 | return $actions; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Action listener. |
| 68 | * |
| 69 | * @param int $user_id user_id. |
| 70 | * @param int $automation_id automation_id. |
| 71 | * @param array $fields fields. |
| 72 | * @param array $selected_options selectedOptions. |
| 73 | * @return array |
| 74 | * @throws Exception Error. |
| 75 | */ |
| 76 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 77 | |
| 78 | if ( ! class_exists( 'StoreEngine' ) ) { |
| 79 | return [ |
| 80 | 'status' => 'error', |
| 81 | 'message' => __( 'StoreEngine plugin is not active.', 'suretriggers' ), |
| 82 | |
| 83 | ]; |
| 84 | } |
| 85 | |
| 86 | $product_name = isset( $selected_options['product_name'] ) ? sanitize_text_field( $selected_options['product_name'] ) : ''; |
| 87 | $product_description = isset( $selected_options['product_description'] ) ? wp_kses_post( $selected_options['product_description'] ) : ''; |
| 88 | $product_excerpt = isset( $selected_options['product_excerpt'] ) ? sanitize_textarea_field( $selected_options['product_excerpt'] ) : ''; |
| 89 | $product_status = isset( $selected_options['product_status'] ) ? sanitize_text_field( $selected_options['product_status'] ) : 'publish'; |
| 90 | $product_price = isset( $selected_options['regular_price'] ) ? floatval( $selected_options['regular_price'] ) : ( isset( $selected_options['product_price'] ) ? floatval( $selected_options['product_price'] ) : 0 ); |
| 91 | $sale_price = isset( $selected_options['sale_price'] ) ? floatval( $selected_options['sale_price'] ) : ''; |
| 92 | $product_type = isset( $selected_options['product_type'] ) ? sanitize_text_field( $selected_options['product_type'] ) : 'simple'; |
| 93 | $product_sku = isset( $selected_options['product_sku'] ) ? sanitize_text_field( $selected_options['product_sku'] ) : ''; |
| 94 | $product_slug = isset( $selected_options['product_slug'] ) ? sanitize_title( $selected_options['product_slug'] ) : ''; |
| 95 | $downloadable_file_name = isset( $selected_options['downloadable_file_name'] ) ? $selected_options['downloadable_file_name'] : ''; |
| 96 | $downloadable_file_url = isset( $selected_options['downloadable_file_url'] ) ? esc_url_raw( $selected_options['downloadable_file_url'] ) : ''; |
| 97 | |
| 98 | |
| 99 | if ( empty( $product_name ) ) { |
| 100 | return [ |
| 101 | 'status' => 'error', |
| 102 | 'message' => __( 'Product name is required.', 'suretriggers' ), |
| 103 | |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | $valid_types = [ 'simple', 'variable', 'grouped', 'external', 'virtual', 'downloadable' ]; |
| 108 | if ( empty( $product_type ) || ! in_array( $product_type, $valid_types ) ) { |
| 109 | $product_type = 'simple'; |
| 110 | } |
| 111 | |
| 112 | if ( ! is_numeric( $product_price ) || $product_price < 0 ) { |
| 113 | $product_price = 0; |
| 114 | } |
| 115 | |
| 116 | if ( ! empty( $sale_price ) && ( ! is_numeric( $sale_price ) || $sale_price < 0 ) ) { |
| 117 | $sale_price = ''; |
| 118 | } |
| 119 | |
| 120 | $author_id = ( ! empty( $user_id ) && is_numeric( $user_id ) && $user_id > 0 ) ? $user_id : 1; |
| 121 | |
| 122 | if ( empty( $product_status ) || ! in_array( $product_status, [ 'publish', 'draft', 'pending', 'private' ] ) ) { |
| 123 | $product_status = 'publish'; |
| 124 | } |
| 125 | |
| 126 | $downloadable_files = []; |
| 127 | |
| 128 | if ( ! empty( $downloadable_file_name ) && ! empty( $downloadable_file_url ) ) { |
| 129 | $downloadable_files[] = [ |
| 130 | 'id' => md5( $downloadable_file_name . $downloadable_file_url ), |
| 131 | 'name' => sanitize_text_field( $downloadable_file_name ), |
| 132 | 'file' => $downloadable_file_url, |
| 133 | 'enabled' => true, |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | $product_data = [ |
| 138 | 'post_title' => $product_name, |
| 139 | 'post_content' => $product_description, |
| 140 | 'post_excerpt' => $product_excerpt, |
| 141 | 'post_status' => $product_status, |
| 142 | 'post_type' => 'storeengine_product', |
| 143 | 'post_author' => $author_id, |
| 144 | ]; |
| 145 | |
| 146 | if ( ! empty( $product_slug ) ) { |
| 147 | $product_data['post_name'] = $product_slug; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Insert the product post. |
| 152 | * |
| 153 | * @var int|\WP_Error $product_id |
| 154 | */ |
| 155 | $product_id = wp_insert_post( $product_data ); |
| 156 | |
| 157 | if ( is_wp_error( $product_id ) ) { |
| 158 | return [ |
| 159 | 'status' => 'error', |
| 160 | 'message' => sprintf( |
| 161 | __( 'Failed to create product: %s', 'suretriggers' ), |
| 162 | $product_id->get_error_message() |
| 163 | ), |
| 164 | ]; |
| 165 | } |
| 166 | |
| 167 | if ( ! $product_id || ! is_numeric( $product_id ) ) { |
| 168 | return [ |
| 169 | 'status' => 'error', |
| 170 | 'message' => __( 'Failed to create product: Invalid product ID returned', 'suretriggers' ), |
| 171 | |
| 172 | ]; |
| 173 | } |
| 174 | |
| 175 | update_post_meta( $product_id, '_storeengine_product_price', $product_price ); |
| 176 | update_post_meta( $product_id, '_storeengine_regular_price', $product_price ); |
| 177 | if ( class_exists( '\\StoreEngine\\Classes\\Price' ) ) { |
| 178 | $price = new \StoreEngine\Classes\Price(); |
| 179 | $price->set_product_id( $product_id ); |
| 180 | |
| 181 | if ( ! empty( $sale_price ) && $sale_price < $product_price ) { |
| 182 | $price->set_price( $sale_price ); |
| 183 | $price->set_compare_price( $product_price ); |
| 184 | update_post_meta( $product_id, '_storeengine_sale_price', $sale_price ); |
| 185 | update_post_meta( $product_id, '_storeengine_product_price', $sale_price ); |
| 186 | } else { |
| 187 | $price->set_price( $product_price ); |
| 188 | $price->set_compare_price( $product_price ); |
| 189 | } |
| 190 | |
| 191 | $price->set_price_name( 'Regular Price' ); |
| 192 | $price->set_price_type( 'onetime' ); |
| 193 | $price->set_order( 0 ); |
| 194 | |
| 195 | if ( ! empty( $selected_options['product_expiry'] ) && is_numeric( $selected_options['product_expiry'] ) ) { |
| 196 | $expire_days = absint( $selected_options['product_expiry'] ); |
| 197 | if ( $expire_days > 0 ) { |
| 198 | $price->set_expire( true ); |
| 199 | $price->set_expire_days( $expire_days ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | $price->save(); |
| 204 | } |
| 205 | |
| 206 | update_post_meta( $product_id, '_storeengine_product_type', $product_type ); |
| 207 | $shipping_type = isset( $selected_options['shipping_type'] ) ? sanitize_text_field( $selected_options['shipping_type'] ) : 'physical'; |
| 208 | if ( ! in_array( $shipping_type, [ 'physical', 'digital' ] ) ) { |
| 209 | $shipping_type = 'physical'; |
| 210 | } |
| 211 | update_post_meta( $product_id, '_storeengine_product_shipping_type', $shipping_type ); |
| 212 | |
| 213 | if ( 'physical' === $shipping_type && ! empty( $selected_options['product_weight'] ) ) { |
| 214 | $weight = floatval( $selected_options['product_weight'] ); |
| 215 | if ( $weight >= 0 ) { |
| 216 | update_post_meta( $product_id, '_storeengine_product_physical_weight', $weight ); |
| 217 | |
| 218 | $weight_unit = isset( $selected_options['weight_unit'] ) ? sanitize_text_field( $selected_options['weight_unit'] ) : 'kg'; |
| 219 | if ( ! in_array( $weight_unit, [ 'kg', 'g' ] ) ) { |
| 220 | $weight_unit = 'kg'; |
| 221 | } |
| 222 | update_post_meta( $product_id, '_storeengine_product_physical_weight_unit', $weight_unit ); |
| 223 | |
| 224 | if ( class_exists( '\\StoreEngine\\Classes\\Product' ) ) { |
| 225 | $product_obj = new \StoreEngine\Classes\Product( $product_id ); |
| 226 | $product_obj->set_weight( $weight ); |
| 227 | $product_obj->set_weight_unit( $weight_unit ); |
| 228 | $product_obj->save(); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if ( ! empty( $product_sku ) ) { |
| 234 | update_post_meta( $product_id, '_storeengine_sku', $product_sku ); |
| 235 | } |
| 236 | |
| 237 | if ( ! empty( $downloadable_files ) ) { |
| 238 | update_post_meta( $product_id, '_storeengine_product_downloadable_files', $downloadable_files ); |
| 239 | } |
| 240 | |
| 241 | if ( isset( $selected_options['manage_stock'] ) && 'yes' === sanitize_key( $selected_options['manage_stock'] ) ) { |
| 242 | update_post_meta( $product_id, '_storeengine_manage_stock', 'yes' ); |
| 243 | |
| 244 | $stock_quantity = isset( $selected_options['stock_quantity'] ) ? absint( $selected_options['stock_quantity'] ) : 0; |
| 245 | update_post_meta( $product_id, '_storeengine_stock_quantity', $stock_quantity ); |
| 246 | |
| 247 | $stock_status = $stock_quantity > 0 ? 'instock' : 'outofstock'; |
| 248 | update_post_meta( $product_id, '_storeengine_stock_status', $stock_status ); |
| 249 | } else { |
| 250 | update_post_meta( $product_id, '_storeengine_manage_stock', 'no' ); |
| 251 | $stock_status = isset( $selected_options['stock_status'] ) ? sanitize_text_field( $selected_options['stock_status'] ) : 'instock'; |
| 252 | update_post_meta( $product_id, '_storeengine_stock_status', $stock_status ); |
| 253 | } |
| 254 | |
| 255 | |
| 256 | if ( ! empty( $selected_options['product_categories'] ) ) { |
| 257 | $categories = is_array( $selected_options['product_categories'] ) |
| 258 | ? $selected_options['product_categories'] |
| 259 | : array_map( 'trim', explode( ',', $selected_options['product_categories'] ) ); |
| 260 | |
| 261 | $category_ids = []; |
| 262 | foreach ( $categories as $category ) { |
| 263 | if ( is_numeric( $category ) ) { |
| 264 | $category_ids[] = absint( $category ); |
| 265 | } else { |
| 266 | $term = get_term_by( 'name', $category, 'storeengine_product_category' ); |
| 267 | if ( $term ) { |
| 268 | $category_ids[] = $term->term_id; |
| 269 | } else { |
| 270 | $new_term = wp_insert_term( $category, 'storeengine_product_category' ); |
| 271 | if ( ! is_wp_error( $new_term ) ) { |
| 272 | $category_ids[] = $new_term['term_id']; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ( ! empty( $category_ids ) ) { |
| 279 | wp_set_object_terms( $product_id, $category_ids, 'storeengine_product_category' ); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | if ( ! empty( $selected_options['product_tags'] ) ) { |
| 284 | $tags = is_array( $selected_options['product_tags'] ) |
| 285 | ? $selected_options['product_tags'] |
| 286 | : array_map( 'trim', explode( ',', $selected_options['product_tags'] ) ); |
| 287 | |
| 288 | $tag_ids = []; |
| 289 | foreach ( $tags as $tag ) { |
| 290 | if ( is_numeric( $tag ) ) { |
| 291 | $tag_ids[] = absint( $tag ); |
| 292 | } else { |
| 293 | $term = get_term_by( 'name', $tag, 'storeengine_product_tag' ); |
| 294 | if ( $term ) { |
| 295 | $tag_ids[] = $term->term_id; |
| 296 | } else { |
| 297 | $new_term = wp_insert_term( $tag, 'storeengine_product_tag' ); |
| 298 | if ( ! is_wp_error( $new_term ) ) { |
| 299 | $tag_ids[] = $new_term['term_id']; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if ( ! empty( $tag_ids ) ) { |
| 306 | wp_set_object_terms( $product_id, $tag_ids, 'storeengine_product_tag' ); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | $product = get_post( $product_id ); |
| 311 | |
| 312 | if ( ! $product ) { |
| 313 | return [ |
| 314 | 'status' => 'error', |
| 315 | 'message' => __( 'Failed to retrieve created product.', 'suretriggers' ), |
| 316 | |
| 317 | ]; |
| 318 | } |
| 319 | |
| 320 | $response = [ |
| 321 | 'product_id' => $product_id, |
| 322 | 'product_name' => $product->post_title, |
| 323 | 'product_slug' => $product->post_name, |
| 324 | 'product_url' => get_permalink( $product_id ), |
| 325 | 'product_status' => $product->post_status, |
| 326 | 'product_type' => get_post_meta( $product_id, '_storeengine_product_type', true ), |
| 327 | 'regular_price' => get_post_meta( $product_id, '_storeengine_regular_price', true ), |
| 328 | 'sale_price' => get_post_meta( $product_id, '_storeengine_sale_price', true ), |
| 329 | 'current_price' => get_post_meta( $product_id, '_storeengine_product_price', true ), |
| 330 | 'sku' => get_post_meta( $product_id, '_storeengine_sku', true ), |
| 331 | 'stock_status' => get_post_meta( $product_id, '_storeengine_stock_status', true ), |
| 332 | 'stock_quantity' => get_post_meta( $product_id, '_storeengine_stock_quantity', true ), |
| 333 | 'manage_stock' => get_post_meta( $product_id, '_storeengine_manage_stock', true ), |
| 334 | 'shipping_type' => get_post_meta( $product_id, '_storeengine_product_shipping_type', true ), |
| 335 | 'weight' => get_post_meta( $product_id, '_storeengine_product_physical_weight', true ), |
| 336 | 'weight_unit' => get_post_meta( $product_id, '_storeengine_product_physical_weight_unit', true ), |
| 337 | 'downloadable_files' => get_post_meta( $product_id, '_storeengine_product_downloadable_files', true ), |
| 338 | 'created_date' => $product->post_date, |
| 339 | 'created_date_gmt' => $product->post_date_gmt, |
| 340 | 'author_id' => $product->post_author, |
| 341 | ]; |
| 342 | |
| 343 | $categories = wp_get_object_terms( $product_id, 'storeengine_product_category', [ 'fields' => 'all' ] ); |
| 344 | if ( ! is_wp_error( $categories ) && ! empty( $categories ) ) { |
| 345 | $response['categories'] = array_map( |
| 346 | function ( $cat ) { |
| 347 | return [ |
| 348 | 'id' => $cat->term_id, |
| 349 | 'name' => $cat->name, |
| 350 | 'slug' => $cat->slug, |
| 351 | ]; |
| 352 | }, |
| 353 | $categories |
| 354 | ); |
| 355 | } |
| 356 | |
| 357 | $tags = wp_get_object_terms( $product_id, 'storeengine_product_tag', [ 'fields' => 'all' ] ); |
| 358 | if ( ! is_wp_error( $tags ) && ! empty( $tags ) ) { |
| 359 | $response['tags'] = array_map( |
| 360 | function ( $tag ) { |
| 361 | return [ |
| 362 | 'id' => $tag->term_id, |
| 363 | 'name' => $tag->name, |
| 364 | 'slug' => $tag->slug, |
| 365 | ]; |
| 366 | }, |
| 367 | $tags |
| 368 | ); |
| 369 | } |
| 370 | |
| 371 | return $response; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | CreateProduct::get_instance(); |
| 376 |