BxDolAIProviderShopifyAdmin.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /*
  10. * Login for Owner/Collaborator account:
  11. * https://admin.shopify.com/store/your_shop/
  12. *
  13. * Integration:
  14. * 0. Custom apps creation guide (for a shop Owner/Collaborator, not a Partner):
  15. * https://help.shopify.com/en/manual/apps/app-types/custom-apps
  16. *
  17. * 1. Make authenticated requests to the REST Admin API:
  18. * https://shopify.dev/docs/apps/auth/admin-app-access-tokens
  19. *
  20. * 2. REST Admin API reference:
  21. * https://shopify.dev/docs/api/admin-rest
  22. *
  23. * 3. REST Admin API -> Product
  24. * https://shopify.dev/docs/api/admin-rest/2023-10/resources/product
  25. *
  26. * 4. REST Admin API -> Order
  27. * https://shopify.dev/docs/api/admin-rest/2023-10/resources/order
  28. *
  29. */
  30. class BxDolAIProviderShopifyAdmin extends BxDolAIProvider
  31. {
  32. public static $NAME = 'shopify_admin';
  33. protected $_sShopDomain;
  34. protected $_sAccessToken;
  35. protected $_sEndpoint;
  36. protected $_sStorefront;
  37. public function __construct($iProfile, $aSource)
  38. {
  39. $this->_sName = self::$NAME;
  40. parent::__construct($iProfile, $aSource);
  41. $this->_sShopDomain = $this->getOption('shop_domain');
  42. $this->_sAccessToken = $this->getOption('access_token');
  43. $this->_sEndpoint = "https://{$this->_sShopDomain}/admin/api/2023-10/";
  44. $this->_sStorefront = "https://{$this->_sShopDomain}/";
  45. }
  46. public function getEntry($sId)
  47. {
  48. $CNF = &$this->_oModule->_oConfig->CNF;
  49. $sProduct = $this->_call('products/' . $sId . '.json', [
  50. 'fields' => 'id,title,handle,body_html,tags,variants',
  51. ], 'get');
  52. if(empty($sProduct))
  53. return [];
  54. $aProduct = json_decode($sProduct, true);
  55. if(empty($aProduct) || !is_array($aProduct) || empty($aProduct['product']))
  56. return [];
  57. return $aProduct['product'];
  58. }
  59. /**
  60. * Internal methods.
  61. */
  62. protected function _call($sRequest, $aParams, $sMethod = 'post-json', $aHeaders = [])
  63. {
  64. $aHeaders[] = 'Content-Type: application/json';
  65. if(!empty($this->_sAccessToken))
  66. $aHeaders[] = 'X-Shopify-Access-Token: ' . $this->_sAccessToken;
  67. else
  68. $aHeaders[] = 'Authorization: Basic ' . base64_encode($this->_sApiKey . ':' . $this->_sApiSecretKey);
  69. return bx_file_get_contents($this->_sEndpoint . $sRequest, $aParams, $sMethod, $aHeaders);
  70. }
  71. protected function _dateI2S($iTimestamp)
  72. {
  73. return date("Y-m-d", $iTimestamp);
  74. }
  75. }