shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

amazonus_listing.php (5895B)


      1 <?php
      2 class ModelExtensionOpenBayAmazonusListing extends Model {
      3 	private $tabs = array();
      4 
      5 	public function search($search_string) {
      6 
      7 		$search_params = array(
      8 			'search_string' => $search_string,
      9 		);
     10 
     11 		$results = json_decode($this->openbay->amazonus->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 = 'http://www.amazon.com/gp/product/' . $result['asin'] . '/';
     24 
     25 				$products[] = array(
     26 					'name' => $result['name'],
     27 					'asin' => $result['asin'],
     28 					'image' => $result['image'],
     29 					'price' => $price,
     30 					'link' => $link,
     31 				);
     32 			}
     33 		}
     34 
     35 		return $products;
     36 	}
     37 
     38 	public function getProductByAsin($asin) {
     39 		$data = array(
     40 			'asin' => $asin,
     41 		);
     42 
     43 		$results = json_decode($this->openbay->amazonus->call('productv3/getProduct', $data), 1);
     44 
     45 		return $results;
     46 	}
     47 
     48 	public function getBestPrice($asin, $condition) {
     49 		$search_params = array(
     50 			'asin' => $asin,
     51 			'condition' => $condition,
     52 		);
     53 
     54 		$best_price = '';
     55 
     56 		$result = json_decode($this->openbay->amazonus->call('productv3/getPrice', $search_params), 1);
     57 
     58 		if (isset($result['Price']['Amount']) && $result['Price']['Currency'] && $this->currency->has($result['Price']['Currency'])) {
     59 			$best_price['amount'] = number_format($this->currency->convert($result['Price']['Amount'], $result['Price']['Currency'], $this->config->get('config_currency')), 2, '.', '');
     60 			$best_price['shipping'] = number_format($this->currency->convert($result['Price']['Shipping'], $result['Price']['Currency'], $this->config->get('config_currency')), 2, '.', '');
     61 			$best_price['currency'] = $result['Price']['Currency'];
     62 		}
     63 
     64 		return $best_price;
     65 	}
     66 
     67 	public function simpleListing($data) {
     68 		$request = array(
     69 			'asin' => $data['asin'],
     70 			'sku' => $data['sku'],
     71 			'quantity' => $data['quantity'],
     72 			'price' => $data['price'],
     73 			'sale' => array(
     74 				'price' => $data['sale_price'],
     75 				'from' => $data['sale_from'],
     76 				'to' => $data['sale_to'],
     77 			),
     78 			'condition' => $data['condition'],
     79 			'condition_note' => $data['condition_note'],
     80 			'start_selling' => $data['start_selling'],
     81 			'restock_date' => $data['restock_date'],
     82 			'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazonus/listing',
     83 			'product_id' => $data['product_id'],
     84 		);
     85 
     86 		$response = $this->openbay->amazonus->call('productv3/simpleListing', $request);
     87 		$response = json_decode($response);
     88 		if (empty($response)) {
     89 			return array(
     90 				'status' => 0,
     91 				'message' => 'Problem connecting OpenBay: API'
     92 			);
     93 		}
     94 		$response = (array)$response;
     95 
     96 		if ($response['status'] === 1) {
     97 			$this->db->query("
     98 			REPLACE INTO `" . DB_PREFIX . "amazonus_product`
     99 			SET `product_id` = " . (int)$data['product_id'] . ",
    100 				`status` = 'uploaded',
    101 				`version` = 3,
    102 				`var` = ''
    103 		");
    104 		}
    105 
    106 		return $response;
    107 	}
    108 
    109 	public function getBrowseNodes($request) {
    110 		return $this->openbay->amazonus->call('productv3/getBrowseNodes', $request);
    111 	}
    112 
    113 	public function deleteSearchResults($product_ids) {
    114 		$imploded_ids = array();
    115 
    116 		foreach ($product_ids as $product_id) {
    117 			$imploded_ids[] = (int)$product_id;
    118 		}
    119 
    120 		$imploded_ids = implode(',', $imploded_ids);
    121 
    122 		$this->db->query("
    123 			DELETE FROM " . DB_PREFIX .  "amazonus_product_search
    124 			WHERE product_id IN ($imploded_ids)
    125 		");
    126 	}
    127 
    128 	public function doBulkListing($data) {
    129 		$this->load->model('catalog/product');
    130 		$request = array();
    131 
    132 		foreach($data['products'] as $product_id => $asin) {
    133 			$product = $this->model_catalog_product->getProduct($product_id);
    134 
    135 			if ($product) {
    136 				$price = $product['price'];
    137 
    138 				if ($this->config->get('openbay_amazonus_listing_tax_added') && $this->config->get('openbay_amazonus_listing_tax_added') > 0) {
    139 					$price += $price * ($this->config->get('openbay_amazonus_listing_tax_added') / 100);
    140 				}
    141 
    142 				$request[] = array(
    143 					'asin' => $asin,
    144 					'sku' => $product['sku'],
    145 					'quantity' => $product['quantity'],
    146 					'price' => number_format($price, 2, '.', ''),
    147 					'sale' => array(),
    148 					'condition' => (isset($data['condition']) ? $data['condition'] : ''),
    149 					'condition_note' => (isset($data['condition_note']) ? $data['condition_note'] : ''),
    150 					'start_selling' => (isset($data['start_selling']) ? $data['start_selling'] : ''),
    151 					'restock_date' => '',
    152 					'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazonus/listing',
    153 					'product_id' => $product['product_id'],
    154 				);
    155 			}
    156 		}
    157 
    158 		if ($request) {
    159 			$response = $this->openbay->amazonus->call('productv3/bulkListing', $request);
    160 
    161 			$response = json_decode($response, 1);
    162 
    163 			if ($response['status'] == 1) {
    164 				foreach ($request as $product) {
    165 					$this->db->query("
    166 						REPLACE INTO `" . DB_PREFIX . "amazonus_product`
    167 						SET `product_id` = " . (int)$product['product_id'] . ",
    168 							`status` = 'uploaded',
    169 							`var` = '',
    170 							`version` = 3
    171 					");
    172 				}
    173 
    174 				return true;
    175 			}
    176 		}
    177 
    178 		return false;
    179 	}
    180 
    181 	public function doBulkSearch($search_data) {
    182 		foreach ($search_data as $products) {
    183 			foreach ($products as $product) {
    184 				$this->db->query("
    185 					REPLACE INTO " . DB_PREFIX . "amazonus_product_search (product_id, `status`)
    186 					VALUES (" . (int)$product['product_id'] . ", 'searching')");
    187 			}
    188 		}
    189 
    190 		$request_data = array(
    191 			'search' => $search_data,
    192 			'response_url' => HTTPS_CATALOG . 'index.php?route=extension/openbay/amazonus/search'
    193 		);
    194 
    195 		$response = $this->openbay->amazonus->call('productv3/bulkSearch', $request_data);
    196 	}
    197 }