amazon_listing.php (7042B)
1 <?php 2 class ModelExtensionOpenBayAmazonListing extends Model { 3 private $tabs = array(); 4 5 public function search($search_string, $marketplace) { 6 $search_params = array( 7 'search_string' => $search_string, 8 'marketplace' => $marketplace, 9 ); 10 11 $results = json_decode($this->openbay->amazon->call('productv3/search', $search_params), 1); 12 13 $products = array(); 14 15 if (!empty($results)) { 16 foreach ($results['Products'] as $result) { 17 if ($result['price']['amount'] && $result['price']['currency']) { 18 $price = $result['price']['amount'] . ' ' . $result['price']['currency']; 19 } else { 20 $price = '-'; 21 } 22 23 $link = ''; 24 25 switch ($marketplace) { 26 case 'uk': 27 $link = 'https://www.amazon.co.uk/dp/' . $result['asin'] . '/'; 28 break; 29 case 'de': 30 $link = 'https://www.amazon.de/dp/' . $result['asin'] . '/'; 31 break; 32 case 'fr': 33 $link = 'https://www.amazon.fr/dp/' . $result['asin'] . '/'; 34 break; 35 case 'it': 36 $link = 'https://www.amazon.it/dp/' . $result['asin'] . '/'; 37 break; 38 case 'es': 39 $link = 'https://www.amazon.es/dp/' . $result['asin'] . '/'; 40 break; 41 } 42 43 $products[] = array( 44 'name' => $result['name'], 45 'asin' => $result['asin'], 46 'image' => $result['image'], 47 'price' => $price, 48 'link' => $link, 49 ); 50 } 51 } 52 53 return $products; 54 } 55 56 public function getProductByAsin($asin, $market) { 57 $data = array( 58 'asin' => $asin, 59 'marketplace' => $market, 60 ); 61 62 $results = json_decode($this->openbay->amazon->call('productv3/getProduct', $data), 1); 63 64 return $results; 65 } 66 67 public function getBestPrice($asin, $condition, $marketplace) { 68 $search_params = array( 69 'asin' => $asin, 70 'condition' => $condition, 71 'marketplace' => $marketplace, 72 ); 73 74 $best_price = ''; 75 76 $result = json_decode($this->openbay->amazon->call('productv3/getPrice', $search_params), 1); 77 78 if (isset($result['Price']['Amount']) && $result['Price']['Currency'] && $this->currency->has($result['Price']['Currency'])) { 79 $best_price['amount'] = number_format($this->currency->convert($result['Price']['Amount'], $result['Price']['Currency'], $this->config->get('config_currency')), 2, '.', ''); 80 $best_price['shipping'] = number_format($this->currency->convert($result['Price']['Shipping'], $result['Price']['Currency'], $this->config->get('config_currency')), 2, '.', ''); 81 $best_price['currency'] = $result['Price']['Currency']; 82 } 83 84 return $best_price; 85 } 86 87 public function simpleListing($data) { 88 $request = array( 89 'asin' => $data['asin'], 90 'sku' => $data['sku'], 91 'quantity' => $data['quantity'], 92 'price' => $data['price'], 93 'sale' => array( 94 'price' => $data['sale_price'], 95 'from' => $data['sale_from'], 96 'to' => $data['sale_to'], 97 ), 98 'condition' => $data['condition'], 99 'condition_note' => $data['condition_note'], 100 'start_selling' => $data['start_selling'], 101 'restock_date' => $data['restock_date'], 102 'marketplace' => $data['marketplace'], 103 'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazon/listing', 104 'product_id' => $data['product_id'], 105 ); 106 107 $response = $this->openbay->amazon->call('productv3/simpleListing', $request); 108 $response = json_decode($response); 109 110 if (empty($response)) { 111 return array( 112 'status' => 0, 113 'message' => 'Problem connecting OpenBay: API' 114 ); 115 } 116 117 $response = (array)$response; 118 119 if (($response['status'] === 1)) { 120 $this->db->query(" 121 REPLACE INTO `" . DB_PREFIX . "amazon_product` 122 SET `product_id` = " . (int)$data['product_id'] . ", 123 `status` = 'uploaded', 124 `marketplaces` = '" . $this->db->escape($data['marketplace']) . "', 125 `version` = 3, 126 `var` = '' 127 "); 128 } 129 130 return $response; 131 } 132 133 public function getBrowseNodes($request) { 134 return $this->openbay->amazon->call('productv3/getBrowseNodes', $request); 135 } 136 137 public function doBulkSearch($search_data) { 138 foreach ($search_data as $products) { 139 foreach ($products as $product) { 140 $this->db->query(" 141 REPLACE INTO " . DB_PREFIX . "amazon_product_search (product_id, `status`, marketplace) 142 VALUES (" . (int)$product['product_id'] . ", 'searching', '" . $this->db->escape($product['marketplace']) . "')"); 143 } 144 } 145 146 $request_data = array( 147 'search' => $search_data, 148 'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazon/search' 149 ); 150 151 $this->openbay->amazon->call('productv3/bulkSearch', $request_data); 152 } 153 154 public function deleteSearchResults($marketplace, $product_ids) { 155 $imploded_ids = array(); 156 157 foreach ($product_ids as $product_id) { 158 $imploded_ids[] = (int)$product_id; 159 } 160 161 $imploded_ids = implode(',', $imploded_ids); 162 163 $this->db->query(" 164 DELETE FROM " . DB_PREFIX . "amazon_product_search 165 WHERE marketplace = '" . $this->db->escape($marketplace) . "' AND product_id IN ($imploded_ids) 166 "); 167 } 168 169 public function doBulkListing($data) { 170 $this->load->model('catalog/product'); 171 $request = array(); 172 173 $marketplace_mapping = array( 174 'uk' => 'A1F83G8C2ARO7P', 175 'de' => 'A1PA6795UKMFR9', 176 'fr' => 'A13V1IB3VIYZZH', 177 'it' => 'APJ6JRA9NG5V4', 178 'es' => 'A1RKKUPIHCS9HS', 179 ); 180 181 foreach($data['products'] as $product_id => $asin) { 182 $product = $this->model_catalog_product->getProduct($product_id); 183 184 if ($product) { 185 $price = $product['price']; 186 187 if ($this->config->get('openbay_amazon_listing_tax_added') && $this->config->get('openbay_amazon_listing_tax_added') > 0) { 188 $price += $price * ($this->config->get('openbay_amazon_listing_tax_added') / 100); 189 } 190 191 $request[] = array( 192 'asin' => $asin, 193 'sku' => $product['sku'], 194 'quantity' => $product['quantity'], 195 'price' => number_format($price, 2, '.', ''), 196 'sale' => array(), 197 'condition' => (isset($data['condition']) ? $data['condition'] : ''), 198 'condition_note' => (isset($data['condition_note']) ? $data['condition_note'] : ''), 199 'start_selling' => (isset($data['start_selling']) ? $data['start_selling'] : ''), 200 'restock_date' => '', 201 'marketplace' => $data['marketplace'], 202 'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazon/listing', 203 'product_id' => $product['product_id'], 204 ); 205 } 206 } 207 208 if ($request) { 209 $response = $this->openbay->amazon->call('productv3/bulkListing', $request); 210 211 $response = json_decode($response, 1); 212 213 if ($response['status'] == 1) { 214 foreach ($request as $product) { 215 $this->db->query(" 216 REPLACE INTO `" . DB_PREFIX . "amazon_product` 217 SET `product_id` = " . (int)$product['product_id'] . ", 218 `status` = 'uploaded', 219 `marketplaces` = '" . $this->db->escape($data['marketplace']) . "', 220 `version` = 3, 221 `var` = '' 222 "); 223 } 224 225 return true; 226 } 227 } 228 229 return false; 230 } 231 }