public function handleActions() { // trigger orders update if ($this->requestAction() == 'update_orders') { do_action('wpla_update_orders'); } // load order items if ($this->requestAction() == 'load_order_items') { $lm = new WPLA_OrdersModel(); $order = $lm->getItem($_REQUEST['amazon_order']); if (!$order) { return; } $account = WPLA_AmazonAccount::getAccount($order['account_id']); if (!$account) { return; } $api = new WPLA_AmazonAPI($account->id); // get report requests $items = $api->getOrderLineItems($order['order_id']); // echo "<pre>";print_r($items);echo"</pre>";die(); if (is_array($items)) { // run the import $this->importOrderItems($items, $order['order_id']); $this->showMessage(sprintf(__('%s item(s) were processed for account %s.', 'wpla'), sizeof($items), $account->title)); } elseif ($items->Error->Message) { $this->showMessage(sprintf(__('There was a problem downloading items for account %s.', 'wpla'), $account->title) . '<br>Error: ' . $items->Error->Message, 1); } else { $this->showMessage(sprintf(__('There was a problem downloading items for account %s.', 'wpla'), $account->title), 1); } } // handle update from Amazon action if ($this->requestAction() == 'update') { $this->updateOrdersfromAmazon($_REQUEST['amazon_order']); // $this->showMessage( __('Not implemented yet.','wpla') ); } // handle delete action if ($this->requestAction() == 'delete') { $this->deleteOrders($_REQUEST['amazon_order']); $this->showMessage(__('Selected items were removed.', 'wpla')); } }
function loadFromAmazon() { global $wpdb; $table = $wpdb->prefix . self::TABLENAME; if (!$this->id) { return; } if (!$this->GeneratedReportId) { return; } $api = new WPLA_AmazonAPI($this->account_id); $this->data = $api->getReport($this->GeneratedReportId); $wpdb->update($table, array('line_count' => substr_count($this->data, "\n")), array('id' => $this->id)); $wpdb->update($table, array('data' => $this->data), array('id' => $this->id)); echo $wpdb->last_error; }
public function handleActions() { // trigger reports update if ($this->requestAction() == 'update_reports') { do_action('wpla_update_reports'); } // trigger report request if ($this->requestAction() == 'request_report') { $accounts = WPLA_AmazonAccount::getAll(); foreach ($accounts as $account) { $api = new WPLA_AmazonAPI($account->id); // request report - returns request list as array on success $reports = $api->requestReport($_REQUEST['wpla_report_type']); if (is_array($reports)) { // process the result // $this->processReportsRequestList( $reports, $account ); WPLA_AmazonReport::processReportsRequestList($reports, $account); $this->showMessage(sprintf(__('Report requested for account %s.', 'wpla'), $account->title)); } elseif ($reports->Error->Message) { $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title) . '<br>Error: ' . $reports->Error->Message, 1); } else { $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title), 1); } } } // handle load report action if ($this->requestAction() == 'load_report_from_amazon') { $report = new WPLA_AmazonReport($_REQUEST['amazon_report']); $report->loadFromAmazon(); // $api = new WPLA_AmazonAPI( $report->account_id ); // $api->getReport( $report->GeneratedReportId ); $this->showMessage(__('Report was downloaded from Amazon.', 'wpla')); } // handle process report action if ($this->requestAction() == 'process_amazon_report') { $this->processReportData($_REQUEST['amazon_report']); $this->showMessage(__('Report was processed.', 'wpla')); } // handle process report action if ($this->requestAction() == 'process_fba_shipment_report') { $this->processFbaShipmentReportData($_REQUEST['amazon_report']); } // handle delete_amazon_report action if ($this->requestAction() == 'delete_amazon_report') { $this->deleteReports($_REQUEST['amazon_report']); $this->showMessage(__('Selected items were removed.', 'wpla')); } // handle update_amazon_report action if ($this->requestAction() == 'update_amazon_report') { $this->updateReports($_REQUEST['amazon_report']); $this->showMessage(__('Selected items were updated.', 'wpla')); } }
public function request_daily_inventory_report() { $report_type = '_GET_MERCHANT_LISTINGS_DATA_'; $accounts = WPLA_AmazonAccount::getAll(); foreach ($accounts as $account) { $api = new WPLA_AmazonAPI($account->id); // request report - returns request list as array on success $reports = $api->requestReport($report_type); if (is_array($reports)) { // process the result WPLA_AmazonReport::processReportsRequestList($reports, $account, true); } elseif ($reports->Error->Message) { } else { } } // foreach account }
function loadSubmissionResult() { if (!$this->id) { return; } if (!$this->FeedSubmissionId) { return; } if ($this->FeedProcessingStatus != '_DONE_') { return; } $api = new WPLA_AmazonAPI($this->account_id); $result = $api->getFeedSubmissionResult($this->FeedSubmissionId); if ($result && $result->success) { $this->results = utf8_encode($result->content); // required for amazon.fr $this->update(); } return $result; }
public function updateFeedStatus($feed_ids) { WPLA()->logger->info("updateFeedStatus() - " . join(', ', $feed_ids)); // echo "<pre>";print_r($feed_ids);echo"</pre>";die(); if (empty($feed_ids)) { return; } $accounts = WPLA_AmazonAccount::getAll(); foreach ($feed_ids as $feed_id) { $feed = new WPLA_AmazonFeed($feed_id); $account = new WPLA_AmazonAccount($feed->account_id); $api = new WPLA_AmazonAPI($feed->account_id); // get feed submissions $feeds = $api->getFeedSubmissionList($feed->FeedSubmissionId); if (is_array($feeds)) { // run the import WPLA_AmazonFeed::processFeedsSubmissionList($feeds, $account); $msg = sprintf(__('%s feed submission(s) were found for account %s.', 'wpla'), sizeof($feeds), $account->title); WPLA()->logger->info($msg); $this->showMessage(nl2br($msg), 0, 1); } elseif ($feeds->Error->Message) { $msg = sprintf(__('There was a problem fetching feed submissions for account %s.', 'wpla'), $account->title) . ' - Error: ' . $feeds->Error->Message; WPLA()->logger->error($msg); $this->showMessage(nl2br($msg), 1, 1); } else { $msg = sprintf(__('There was a problem fetching feed submissions for account %s.', 'wpla'), $account->title); WPLA()->logger->error($msg); $this->showMessage(nl2br($msg), 1, 1); } } }
public function getVariationImageForASIN($asin, $account_id) { // init api $api = new WPLA_AmazonAPI($account_id); // get product details from amazon $result = $api->getMatchingProductForId($asin, 'ASIN'); $this->request_count++; WPLA()->logger->debug('getMatchingProductForId() returned:' . print_r($result, 1)); // handle empty result error if ($result->success && !empty($result->product->AttributeSets->ItemAttributes->SmallImage->URL)) { if (!empty($result->product->GetMatchingProductForIdResult->Error->Message)) { $this->lastError = sprintf(__('There was a problem fetching product details for %s.', 'wpla'), $listing['asin']) . '<br><code>' . $result->product->GetMatchingProductForIdResult->Error->Message . '</code>'; } // fetch image URL $img_url = $result->product->AttributeSets->ItemAttributes->SmallImage->URL; // get 600px image instead of 75px $img_url = str_replace('_SL75_', '_SL600_', $img_url); WPLA()->logger->info("variation image for ASIN {$asin}: {$img_url}"); return $img_url; } WPLA()->logger->warn("no variation image found for ASIN {$asin}"); return ''; }
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 requestNewInventoryReport($report_type = '_GET_MERCHANT_LISTINGS_DATA_') { $accounts = WPLA_AmazonAccount::getAll(); foreach ($accounts as $account) { $api = new WPLA_AmazonAPI($account->id); // request report - returns request list as array on success $reports = $api->requestReport($report_type); if (is_array($reports)) { // process the result // $this->processReportsRequestList( $reports, $account ); WPLA_AmazonReport::processReportsRequestList($reports, $account); $this->showMessage(sprintf(__('Report requested for account %s.', 'wpla'), $account->title)); } elseif ($reports->Error->Message) { $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title) . '<br>Error: ' . $reports->Error->Message, 1); } else { $this->showMessage(sprintf(__('There was a problem requesting the report for account %s.', 'wpla'), $account->title), 1); } } }
public function get_compet_price() { if (!isset($_REQUEST['listing'])) { return; } // get items $listing_ids = is_array($_REQUEST['listing']) ? $_REQUEST['listing'] : array($_REQUEST['listing']); if (!empty($listing_ids)) { $listingsModel = new WPLA_ListingsModel(); $listings = WPLA_ListingsModel::getItems($listing_ids, OBJECT); $account_id = $listings[0]->account_id; // echo "<pre>";print_r($listings);echo"</pre>";die(); // build array of ASINs $listing_ASINs = array(); foreach ($listings as $listing) { // prevent invalid marketplace errors if ($account_id != $listing->account_id) { $this->showMessage('You can only fetch pricing information from one account at a time. Item ' . $listing->asin . ' was skipped.', 1, 1); continue; } $listing_ASINs[] = $listing->asin; } // limit to 20 ASINs at a time - for now if (sizeof($listing_ASINs) > 20) { $listing_ASINs = array_splice($listing_ASINs, 0, 20); $this->showMessage('You can only fetch pricing information for up to 20 ASINs at a time.', 2, 1); } if (!empty($listing_ASINs)) { $api = new WPLA_AmazonAPI($account_id); $result = $api->getCompetitivePricingForId($listing_ASINs); // echo "<pre>";print_r($result);echo"</pre>"; if ($result->success) { $message = ''; foreach ($result->products as $asin => $product) { foreach ($product->prices as $price) { $lowest_price = $price->LandedPrice; $condition = $price->condition; $subcondition = $price->subcondition; $shipping_fee = $price->Shipping; $shipping_msg = $shipping_fee ? "incl. {$shipping_fee} shipping" : 'free shipping'; $lowest_price = number_format_i18n(floatval($lowest_price), 2); $message .= sprintf('BuyBox price for %s: %s ( %s / %s / %s )<br>', $asin, $lowest_price, $condition, $subcondition, $shipping_msg); } if (empty($product->prices)) { $message .= sprintf('No Buy Box price found for %s<br>', $asin); } } wpla_show_message($message); } // process result $listingsModel->processBuyBoxPricingResult($result); } } }
function updateMarketplaceParticipations() { if (!$this->id) { return; } $api = new WPLA_AmazonAPI($this->id); $result = $api->listMarketplaceParticipations(); if (@$result->success) { $this->allowed_markets = maybe_serialize($result->allowed_markets); $this->update(); } elseif ($result->ErrorMessage) { wpla_show_message($result->ErrorMessage, 'error'); } return $result; }