diff --git a/Block/Checkout/Onepage/Success.php b/Block/Checkout/Onepage/Success.php index b0d9159..dce89af 100644 --- a/Block/Checkout/Onepage/Success.php +++ b/Block/Checkout/Onepage/Success.php @@ -1,33 +1,58 @@ -helper = $helper; $this->checkoutSession = $checkoutSession; + $this->productRepository = $productRepository; } /** @@ -38,15 +63,17 @@ protected function prepareConfig(): void try { $order = $this->checkoutSession->getLastRealOrder(); + $this->setData('flowbox', [ 'allowCookies' => $this->isUserAllowSaveCookies(), 'apiKey' => (string) $this->getApiKey(), 'debug' => $this->isDebugJavaScript(), 'orderId' => \ltrim($order->getIncrementId(), '#'), 'products' => \array_map( - function ($item) { + function ($item){ + $product = $this->productRepository->get($item->getSku()); return [ - 'id' => (string) $item->getSku(), + 'id' => (string) $product->getData($this->helper->getAttributeCode()), 'quantity' => (int) $item->getQtyOrdered() ]; }, diff --git a/Helper/Data.php b/Helper/Data.php new file mode 100644 index 0000000..7675b91 --- /dev/null +++ b/Helper/Data.php @@ -0,0 +1,47 @@ +scopeConfig = $scopeConfig; + $this->storeManager = $storeManager; + } + + /** + * @return string + * @throws NoSuchEntityException + */ + public function getAttributeCode(): string + { + return $this->scopeConfig->getValue( + 'itonomy_flowbox/general/checkout_identifier', + ScopeInterface::SCOPE_STORE, + $this->storeManager->getStore()->getId() + ); + } +} diff --git a/Model/Config/Source/CheckoutIdentifier.php b/Model/Config/Source/CheckoutIdentifier.php new file mode 100644 index 0000000..4991149 --- /dev/null +++ b/Model/Config/Source/CheckoutIdentifier.php @@ -0,0 +1,63 @@ +collection = $collection; + } + + /** + * @return array + */ + public function getUniqueAttributes(): array + { + $collection = $this->collection; + + $collection->addFieldToFilter( + 'entity_type_id', + ['eq' => '4'] + ); + $collection->addFieldToFilter( + 'is_unique', + ['eq' => '1'] + ); + return $collection->toArray(); + } + + /** + * @return array + */ + public function toOptionArray(): array + { + $items = $this->getUniqueAttributes(); + $options = []; + + if (array_key_exists('items', $items)) { + foreach ($items['items'] as $item) { + $options[] = ['value' => $item['attribute_code'], + 'label' => $item['frontend_label'] + ]; + } + } + return $options; + } +} diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index fc942cf..f161064 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -28,6 +28,11 @@ Your Flowbox API key Magento\Config\Model\Config\Backend\Encrypted + + + Itonomy\Flowbox\Model\Config\Source\CheckoutIdentifier + Select the product identifier for the checkout script + diff --git a/etc/config.xml b/etc/config.xml index c9e8453..fef5acf 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -12,6 +12,7 @@ 1 0 + sku