public function jobs_run_task() { // quit if no job name provided if (!isset($_REQUEST['job'])) { return false; } if (!isset($_REQUEST['task'])) { return false; } $job = $_REQUEST['job']; $task = $_REQUEST['task']; // register shutdown handler global $wpla_shutdown_handler_enabled; $wpla_shutdown_handler_enabled = true; register_shutdown_function(array($this, 'shutdown_handler')); WPLA()->logger->info('running task: ' . print_r($task, 1)); // handle job name switch ($task['task']) { // update listing from Amazon (current used for new listings without ASIN) case 'updateProduct': // init $lm = new WPLA_ListingsModel(); $listing = $lm->getItem($task['id']); $account = WPLA_AmazonAccount::getAccount($listing['account_id']); $api = new WPLA_AmazonAPI($account->id); // get product attributes // $product = $api->getProduct( $listing['asin'] ); $result = $api->getMatchingProductForId($listing['sku'], 'SellerSKU'); // echo "<pre>";print_r($product);echo"</pre>";#die(); // echo "<pre>";print_r($product);echo"</pre>";die(); if ($result->success) { if (!empty($result->product->ASIN)) { // update listing attributes $listing_id = $listing['id']; // $lm->updateItemAttributes( $product, $listing_id ); // $listing = $lm->getItem( $listing_id ); // update values $lm->updateWhere(array('id' => $listing_id), array('asin' => $result->product->ASIN)); WPLA()->logger->info('new ASIN for listing #' . $listing['id'] . ': ' . $result->product->ASIN); // update product // $woo = new WPLA_ProductBuilder(); // $woo->updateProducts( array( $listing ) ); $success = true; $errors = ''; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']); $errors .= ' The product data received from Amazon was empty.'; $success = false; } } elseif ($result->Error->Message) { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br>Error: ' . $result->Error->Message; $success = false; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']); $success = false; } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors)); $response->success = $success; $this->returnJSON($response); exit; // create new WooCommerce product from imported listing // create new WooCommerce product from imported listing case 'createProduct': // init $lm = new WPLA_ListingsModel(); // $listing = $lm->getItem( $task['id'] ); $listing_id = $task['id']; // create product $ProductsImporter = new WPLA_ProductsImporter(); $success = $ProductsImporter->createProductFromAmazonListing($listing_id); $error = $ProductsImporter->lastError; $delay = $ProductsImporter->request_count * 1000; // ms // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($error) ? array() : array(array('HtmlMessage' => $error)); $response->success = $success; $response->delay = $delay; $this->returnJSON($response); exit; // fetch full product description from Amazon and update WooCommerce product // fetch full product description from Amazon and update WooCommerce product case 'fetchFullProductDescription': $webHelper = new WPLA_AmazonWebHelper(); $webHelper->loadListingDetails($task['id']); // echo "<pre>";print_r($webHelper->images);echo"</pre>";#die(); $lm = new WPLA_ListingsModel(); $item = $lm->getItem($task['id']); if (!empty($webHelper->description)) { // update product $post_id = $item['post_id']; $post_data = array('ID' => $post_id, 'post_content' => trim($webHelper->description)); wp_update_post($post_data); $success = true; $errors = ''; } else { $errors = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $item['asin']); $errors .= ' The product description received from Amazon was empty.'; $success = false; } // build response $response = new stdClass(); $response->job = $job; $response->task = $task; $response->errors = empty($errors) ? array() : array(array('HtmlMessage' => $errors)); $response->success = $success; $this->returnJSON($response); exit; // process Merchant or FBA Report and create / update listings // process Merchant or FBA Report and create / update listings case 'processReportPage': // process report page - both Merchant and FBA reports $response = WPLA_ImportHelper::ajax_processReportPage($job, $task); $this->returnJSON($response); exit; // process single row (SKU) Merchant or FBA Report - and create / update listings // process single row (SKU) Merchant or FBA Report - and create / update listings case 'processSingleSkuFromReport': // process report page - both Merchant and FBA reports $response = WPLA_ImportHelper::ajax_processReportPage($job, $task, true); $this->returnJSON($response); exit; default: // echo "unknown task"; // exit(); } }
public function handleActions() { // handle save listing if ($this->requestAction() == 'save_listing') { $this->saveListing(); } // trigger create product if ($this->requestAction() == 'create_product') { $lm = new WPLA_ListingsModel(); $listing = $lm->getItem($_REQUEST['listing']); if (!$listing) { return; } // create product $ProductsImporter = new WPLA_ProductsImporter(); $success = $ProductsImporter->createProductFromAmazonListing($listing); $error = $ProductsImporter->lastError; $post_id = $ProductsImporter->lastPostID; $message = $ProductsImporter->message; if ($success) { // get parent post_id - for View Product button $_product = get_product($post_id); if ('variation' == $_product->product_type) { $post_id = $_product->parent->id; } $msg = $message ? $message : sprintf(__('A new product (ID %s) was created for ASIN %s.', 'wpla'), $post_id, $listing['asin']); $msg .= sprintf(' <a href="post.php?post=%s&action=edit" class="button button-small" target="_blank">%s</a>', $post_id, __('View product', 'wpla')); $this->showMessage($msg); } else { $error_msg = sprintf(__('Item %s could not be imported.', 'wpla'), $listing['asin']) . '<br>Error: ' . $error; $this->showMessage($error_msg, 1); } } // handle update from Amazon action if ($this->requestAction() == 'update') { // $this->initAC(); // $this->EC->updateItemsFromEbay( $_REQUEST['listing'] ); // $this->EC->closeEbay(); // $this->showMessage( __('Selected items were updated from Amazon.','wpla') ); $this->showMessage(__('Not implemented yet.', 'wpla')); } // handle delete action if ($this->requestAction() == 'delete') { $lm = new WPLA_ListingsModel(); if (is_array($_REQUEST['listing'])) { foreach ($_REQUEST['listing'] as $id) { $lm->deleteItem($id); } } elseif (is_numeric($_REQUEST['listing'])) { $lm->deleteItem($_REQUEST['listing']); } $this->showMessage(__('Selected listings were removed from WP-Lister.', 'wpla')); } // handle trash_listing action if ($this->requestAction() == 'trash_listing') { $items = is_array($_REQUEST['listing']) ? $_REQUEST['listing'] : array($_REQUEST['listing']); $lm = new WPLA_ListingsModel(); foreach ($items as $id) { $lm->updateWhere(array('id' => $id), array('status' => 'trash')); } $this->showMessage(__('Selected items have been scheduled to be removed from your Amazon account.', 'wpla')); } // handle resubmit action if ($this->requestAction() == 'resubmit') { $items = is_array($_REQUEST['listing']) ? $_REQUEST['listing'] : array($_REQUEST['listing']); $lm = new WPLA_ListingsModel(); foreach ($items as $id) { $lm->resubmitItem($id); } $this->showMessage(__('Selected items were prepared for resubmission.', 'wpla')); } if ($this->requestAction() == 'resubmit_all_failed') { $lm = new WPLA_ListingsModel(); $items = $lm->getWhere('status', 'failed'); foreach ($items as $item) { $lm->resubmitItem($item->id); } $this->showMessage(sprintf(__('%s failed items were prepared for resubmission.', 'wpla'), count($items))); } if ($this->requestAction() == 'wpla_clear_import_queue') { $lm = new WPLA_ListingsModel(); $items = $lm->getWhere('status', 'imported'); foreach ($items as $item) { $lm->deleteItem($item->id); } $this->showMessage(sprintf(__('%s items have been removed from the import queue.', 'wpla'), count($items))); } // handle toolbar action - prepare listing from product if ($this->requestAction() == 'wpla_prepare_single_listing') { // get profile $profile = isset($_REQUEST['profile_id']) ? WPLA_AmazonProfile::getProfile($_REQUEST['profile_id']) : false; if ($profile) { // prepare product $listingsModel = new WPLA_ListingsModel(); $success = $listingsModel->prepareProductForListing($_REQUEST['product_id'], $_REQUEST['profile_id']); // $listingsModel->applyProfileToNewListings( $profile ); if ($success) { $this->showMessage(__('New listing was prepared from product.', 'wpla')); } else { $this->showMessage(join('<br>', $listingsModel->warnings), 1); } } } // handle bulk action - get_compet_price if ($this->requestAction() == 'get_compet_price') { $this->get_compet_price(); $this->get_lowest_offers(); // do both } // handle bulk action - get_lowest_offers if ($this->requestAction() == 'get_lowest_offers') { $this->get_lowest_offers(); } // handle wpla_dismiss_imported_products_notice action if ($this->requestAction() == 'wpla_dismiss_imported_products_notice') { self::updateOption('dismiss_imported_products_notice', '1'); } }