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