public function applyLowestPricesToSelectedItems()
 {
     $item_ids = $_REQUEST['listing'];
     $items = WPLA_ListingsModel::getItems($item_ids);
     if (empty($items)) {
         return;
     }
     $changed_product_ids1 = WPLA_RepricingHelper::adjustLowestPriceForProducts($items, true);
     $changed_product_ids2 = WPLA_RepricingHelper::resetProductsToMaxPrice($items, true);
     $changed_product_ids = array_merge($changed_product_ids1, $changed_product_ids2);
     $this->showMessage(count($changed_product_ids) . ' of ' . count($items) . ' product prices have been updated.');
 }
 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);
         }
     }
 }