ProductMapper.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. class ProductMapper {
  3. /**
  4. * Build a Reverb listing payload from an OpenCart product array.
  5. *
  6. * @param array $product Row from oc_product joined with oc_product_description.
  7. * @param array $reverb_data Row from oc_reverb_product_map (condition_uuid, reverb_category_uuid).
  8. * @param array $settings Global Reverb settings (currency, shipping_domestic, shipping_international, store_image_url).
  9. * @return array Ready to POST/PUT to /listings.
  10. */
  11. public static function toReverb(array $product, array $reverb_data, array $settings) {
  12. $currency = !empty($settings['currency']) ? $settings['currency'] : 'AUD';
  13. $payload = [
  14. 'title' => $product['name'],
  15. 'description' => strip_tags($product['description']),
  16. 'sku' => $product['model'],
  17. 'inventory' => max(0, (int)$product['quantity']),
  18. 'price' => [
  19. 'amount' => number_format((float)$product['price'], 2, '.', ''),
  20. 'currency' => $currency,
  21. ],
  22. ];
  23. if (!empty($reverb_data['condition_uuid'])) {
  24. $payload['condition'] = ['uuid' => $reverb_data['condition_uuid']];
  25. }
  26. if (!empty($reverb_data['reverb_category_uuid'])) {
  27. $payload['categories'] = [['uuid' => $reverb_data['reverb_category_uuid']]];
  28. }
  29. // Shipping rates
  30. $rates = [];
  31. if (!empty($settings['shipping_domestic'])) {
  32. $rates[] = [
  33. 'region_code' => 'AU',
  34. 'rate' => [
  35. 'amount' => number_format((float)$settings['shipping_domestic'], 2, '.', ''),
  36. 'currency' => $currency,
  37. ],
  38. ];
  39. }
  40. if (!empty($settings['shipping_international'])) {
  41. $rates[] = [
  42. 'region_code' => 'XX',
  43. 'rate' => [
  44. 'amount' => number_format((float)$settings['shipping_international'], 2, '.', ''),
  45. 'currency' => $currency,
  46. ],
  47. ];
  48. }
  49. if ($rates) {
  50. $payload['shipping'] = ['rates' => $rates];
  51. }
  52. return $payload;
  53. }
  54. /**
  55. * Extract updatable OC product fields from a Reverb listing payload
  56. * (used for Reverb → OpenCart sync).
  57. *
  58. * @param array $listing A listing object from the Reverb API.
  59. * @return array Partial product data to merge into OC.
  60. */
  61. public static function fromReverb(array $listing) {
  62. $data = [];
  63. if (isset($listing['title'])) {
  64. $data['name'] = $listing['title'];
  65. }
  66. if (isset($listing['description'])) {
  67. $data['description'] = $listing['description'];
  68. }
  69. if (isset($listing['price']['amount'])) {
  70. $data['price'] = (float)$listing['price']['amount'];
  71. }
  72. if (isset($listing['inventory'])) {
  73. $data['quantity'] = (int)$listing['inventory'];
  74. }
  75. return $data;
  76. }
  77. /**
  78. * Build photo upload payloads for a product's images.
  79. *
  80. * @param array $images Array of image paths relative to OC image dir.
  81. * @param string $store_url Full base URL of the store (e.g. https://example.com/).
  82. * @return array Array of ['image_url' => '...'] payloads.
  83. */
  84. public static function buildPhotoPayloads(array $images, $store_url) {
  85. $payloads = [];
  86. $base = rtrim($store_url, '/') . '/image/';
  87. foreach ($images as $path) {
  88. if (!empty($path)) {
  89. $payloads[] = ['image_url' => $base . ltrim($path, '/')];
  90. }
  91. }
  92. return $payloads;
  93. }
  94. }