| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- class ProductMapper {
- /**
- * Build a Reverb listing payload from an OpenCart product array.
- *
- * @param array $product Row from oc_product joined with oc_product_description.
- * @param array $reverb_data Row from oc_reverb_product_map (condition_uuid, reverb_category_uuid).
- * @param array $settings Global Reverb settings (currency, shipping_domestic, shipping_international, store_image_url).
- * @return array Ready to POST/PUT to /listings.
- */
- public static function toReverb(array $product, array $reverb_data, array $settings) {
- $currency = !empty($settings['currency']) ? $settings['currency'] : 'AUD';
- $payload = [
- 'title' => $product['name'],
- 'description' => strip_tags($product['description']),
- 'sku' => $product['model'],
- 'inventory' => max(0, (int)$product['quantity']),
- 'has_inventory' => true,
- 'publish' => true,
- 'price' => [
- 'amount' => number_format((float)$product['price'], 2, '.', ''),
- 'currency' => $currency,
- ],
- ];
- if (!empty($reverb_data['condition_uuid'])) {
- $payload['condition'] = ['uuid' => $reverb_data['condition_uuid']];
- }
- if (!empty($reverb_data['reverb_category_uuid'])) {
- $payload['categories'] = [['uuid' => $reverb_data['reverb_category_uuid']]];
- }
- // Shipping rates
- $rates = [];
- if (!empty($settings['shipping_domestic'])) {
- $rates[] = [
- 'region_code' => 'AU',
- 'rate' => [
- 'amount' => number_format((float)$settings['shipping_domestic'], 2, '.', ''),
- 'currency' => $currency,
- ],
- ];
- }
- if (!empty($settings['shipping_international'])) {
- $rates[] = [
- 'region_code' => 'XX',
- 'rate' => [
- 'amount' => number_format((float)$settings['shipping_international'], 2, '.', ''),
- 'currency' => $currency,
- ],
- ];
- }
- if ($rates) {
- $payload['shipping'] = ['rates' => $rates];
- }
- return $payload;
- }
- /**
- * Extract updatable OC product fields from a Reverb listing payload
- * (used for Reverb → OpenCart sync).
- *
- * @param array $listing A listing object from the Reverb API.
- * @return array Partial product data to merge into OC.
- */
- public static function fromReverb(array $listing) {
- $data = [];
- if (isset($listing['title'])) {
- $data['name'] = $listing['title'];
- }
- if (isset($listing['description'])) {
- $data['description'] = $listing['description'];
- }
- if (isset($listing['price']['amount'])) {
- $data['price'] = (float)$listing['price']['amount'];
- }
- if (isset($listing['inventory'])) {
- $data['quantity'] = (int)$listing['inventory'];
- }
- return $data;
- }
- /**
- * Build photo upload payloads for a product's images.
- *
- * @param array $images Array of image paths relative to OC image dir.
- * @param string $store_url Full base URL of the store (e.g. https://example.com/).
- * @return array Array of ['image_url' => '...'] payloads.
- */
- public static function buildPhotoPayloads(array $images, $store_url) {
- $payloads = [];
- $base = rtrim($store_url, '/') . '/image/';
- foreach ($images as $path) {
- if (!empty($path)) {
- $payloads[] = ['image_url' => $base . ltrim($path, '/')];
- }
- }
- return $payloads;
- }
- }
|