ProductMapper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'has_inventory' => true,
  19. 'publish' => true,
  20. 'price' => [
  21. 'amount' => number_format((float)$product['price'], 2, '.', ''),
  22. 'currency' => $currency,
  23. ],
  24. ];
  25. if (!empty($reverb_data['condition_uuid'])) {
  26. $payload['condition'] = ['uuid' => $reverb_data['condition_uuid']];
  27. }
  28. if (!empty($reverb_data['reverb_category_uuid'])) {
  29. $payload['categories'] = [['uuid' => $reverb_data['reverb_category_uuid']]];
  30. }
  31. // Shipping rates
  32. $rates = [];
  33. if (!empty($settings['shipping_domestic'])) {
  34. $rates[] = [
  35. 'region_code' => 'AU',
  36. 'rate' => [
  37. 'amount' => number_format((float)$settings['shipping_domestic'], 2, '.', ''),
  38. 'currency' => $currency,
  39. ],
  40. ];
  41. }
  42. if (!empty($settings['shipping_international'])) {
  43. $rates[] = [
  44. 'region_code' => 'XX',
  45. 'rate' => [
  46. 'amount' => number_format((float)$settings['shipping_international'], 2, '.', ''),
  47. 'currency' => $currency,
  48. ],
  49. ];
  50. }
  51. if ($rates) {
  52. $payload['shipping'] = ['rates' => $rates];
  53. }
  54. return $payload;
  55. }
  56. /**
  57. * Extract updatable OC product fields from a Reverb listing payload
  58. * (used for Reverb → OpenCart sync).
  59. *
  60. * @param array $listing A listing object from the Reverb API.
  61. * @return array Partial product data to merge into OC.
  62. */
  63. public static function fromReverb(array $listing) {
  64. $data = [];
  65. if (isset($listing['title'])) {
  66. $data['name'] = $listing['title'];
  67. }
  68. if (isset($listing['description'])) {
  69. $data['description'] = $listing['description'];
  70. }
  71. if (isset($listing['price']['amount'])) {
  72. $data['price'] = (float)$listing['price']['amount'];
  73. }
  74. if (isset($listing['inventory'])) {
  75. $data['quantity'] = (int)$listing['inventory'];
  76. }
  77. return $data;
  78. }
  79. /**
  80. * Build photo upload payloads for a product's images.
  81. *
  82. * @param array $images Array of image paths relative to OC image dir.
  83. * @param string $store_url Full base URL of the store (e.g. https://example.com/).
  84. * @return array Array of ['image_url' => '...'] payloads.
  85. */
  86. public static function buildPhotoPayloads(array $images, $store_url) {
  87. $payloads = [];
  88. $base = rtrim($store_url, '/') . '/image/';
  89. foreach ($images as $path) {
  90. if (!empty($path)) {
  91. $payloads[] = ['image_url' => $base . ltrim($path, '/')];
  92. }
  93. }
  94. return $payloads;
  95. }
  96. }