| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class Lasso_Process_Update_Amazon |
| 4 |
* |
| 5 |
* @package Lasso_Process_Update_Amazon |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes\Processes; |
| 9 |
|
| 10 |
use LassoLite\Classes\Amazon_Api; |
| 11 |
use LassoLite\Classes\Processes\Process; |
| 12 |
|
| 13 |
use LassoLite\Models\Amazon_Products; |
| 14 |
|
| 15 |
/** |
| 16 |
* Lasso_Process_Update_Amazon |
| 17 |
*/ |
| 18 |
class Amazon extends Process { |
| 19 |
/** |
| 20 |
* Action name |
| 21 |
* |
| 22 |
* @var string $action |
| 23 |
*/ |
| 24 |
protected $action = 'lassolite_amazon_process'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Log name |
| 28 |
* |
| 29 |
* @var string $log_name |
| 30 |
*/ |
| 31 |
protected $log_name = 'update_amazon'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Task |
| 35 |
* |
| 36 |
* Override this method to perform any actions required on each |
| 37 |
* queue item. Return the modified item for further processing |
| 38 |
* in the next pass through. Or, return false to remove the |
| 39 |
* item from the queue. |
| 40 |
* |
| 41 |
* @param mixed $product_id Queue item to iterate over. |
| 42 |
* |
| 43 |
* @return mixed |
| 44 |
*/ |
| 45 |
public function task( $product_id ) { |
| 46 |
if ( empty( $product_id ) ) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
|
| 50 |
$this->update_amazon_pricing( $product_id ); |
| 51 |
$this->set_processing_runtime(); |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Prepare data for process |
| 58 |
*/ |
| 59 |
public function run() { |
| 60 |
// ? check whether process is age out and make it can work on Lasso UI via ajax requests |
| 61 |
$this->is_process_age_out(); |
| 62 |
|
| 63 |
if ( $this->is_process_running() ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$total_amz = Amazon_Products::count_amazon_product_in_db(); |
| 68 |
$limit = intval( ceil( $total_amz / ( 20 * 3 ) ) ); |
| 69 |
$amazon_product_ids = Amazon_Products::get_amazon_product_in_db( $limit ); |
| 70 |
$count = count( $amazon_product_ids ) ?? 0; |
| 71 |
|
| 72 |
foreach ( $amazon_product_ids as $product ) { |
| 73 |
$id = Amazon_Api::get_product_id_by_url( $product['base_url'] ); |
| 74 |
$product['amazon_id'] = '' === $product['amazon_id'] ? $id : $product['amazon_id']; |
| 75 |
|
| 76 |
$this->push_to_queue( $product['amazon_id'] ); |
| 77 |
} |
| 78 |
|
| 79 |
$this->set_total( $count ); |
| 80 |
$this->set_log_file_name( $this->log_name ); |
| 81 |
$this->task_start_log(); |
| 82 |
|
| 83 |
$this->save()->dispatch(); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Refresh Amazon products pricing |
| 90 |
* |
| 91 |
* @param string $product_id Amazon product id. |
| 92 |
*/ |
| 93 |
private function update_amazon_pricing( $product_id ) { |
| 94 |
$lasso_amazon_api = new Amazon_Api(); |
| 95 |
|
| 96 |
try { |
| 97 |
// ? if a product is checked very quick, we need to delay this in a short time |
| 98 |
// ? because amazon api will response an error when we request continuously |
| 99 |
sleep( 1 ); |
| 100 |
|
| 101 |
$last_updated = gmdate( 'Y-m-d H:i:s', time() ); |
| 102 |
$amazon_db = $lasso_amazon_api->get_amazon_product_from_db( $product_id ); |
| 103 |
$amz_link = $amazon_db['monetized_url'] ?? ''; |
| 104 |
$lasso_amazon_api->fetch_product_info( $product_id, true, $last_updated, $amz_link ); |
| 105 |
} catch ( \Exception $e ) { |
| 106 |
// ? error |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
new Amazon(); |
| 111 |
|