From 53913e900b9b265f531d140e85a5c8e23648a441 Mon Sep 17 00:00:00 2001 From: "Kevin.Schulz" Date: Wed, 10 Nov 2021 09:28:45 +0100 Subject: [PATCH 1/2] FLOW-12 Added logic for checkout identifier selection. --- Block/Checkout/Onepage/Success.php | 42 ++++++++++----- Helper/Data.php | 47 ++++++++++++++++ Model/Config/Source/CheckoutIdentifier.php | 63 ++++++++++++++++++++++ etc/adminhtml/system.xml | 5 ++ etc/config.xml | 1 + 5 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 Helper/Data.php create mode 100644 Model/Config/Source/CheckoutIdentifier.php diff --git a/Block/Checkout/Onepage/Success.php b/Block/Checkout/Onepage/Success.php index b0d9159..1409a40 100644 --- a/Block/Checkout/Onepage/Success.php +++ b/Block/Checkout/Onepage/Success.php @@ -1,32 +1,48 @@ -helper = $helper; $this->checkoutSession = $checkoutSession; } @@ -44,9 +60,9 @@ protected function prepareConfig(): void 'debug' => $this->isDebugJavaScript(), 'orderId' => \ltrim($order->getIncrementId(), '#'), 'products' => \array_map( - function ($item) { + function ($item){ return [ - 'id' => (string) $item->getSku(), + 'id' => (string) $item->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 From 312915d4c91c6bb6c2ac83d35a06a0afc4dc8329 Mon Sep 17 00:00:00 2001 From: "Kevin.Schulz" Date: Wed, 10 Nov 2021 10:18:40 +0100 Subject: [PATCH 2/2] Add extra line to get product by sku --- Block/Checkout/Onepage/Success.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/Block/Checkout/Onepage/Success.php b/Block/Checkout/Onepage/Success.php index 1409a40..dce89af 100644 --- a/Block/Checkout/Onepage/Success.php +++ b/Block/Checkout/Onepage/Success.php @@ -11,6 +11,7 @@ use Magento\Cookie\Helper\Cookie; use Magento\Framework\Encryption\EncryptorInterface; use Magento\Framework\View\Element\Template\Context; +use Magento\Catalog\Model\ProductRepository; class Success extends \Itonomy\Flowbox\Block\Base { @@ -24,12 +25,18 @@ class Success extends \Itonomy\Flowbox\Block\Base */ private $helper; + /** + * @var ProductRepository + */ + private $productRepository; + /** * Success constructor. * @param Context $context * @param EncryptorInterface $encryptor * @param Cookie $cookie * @param Session $checkoutSession + * @param ProductRepository $productRepository * @param Data $helper * @param array $data */ @@ -38,12 +45,14 @@ public function __construct( EncryptorInterface $encryptor, Cookie $cookie, Session $checkoutSession, + ProductRepository $productRepository, Data $helper, array $data = [] ) { parent::__construct($context, $cookie, $encryptor, $data); $this->helper = $helper; $this->checkoutSession = $checkoutSession; + $this->productRepository = $productRepository; } /** @@ -54,6 +63,7 @@ protected function prepareConfig(): void try { $order = $this->checkoutSession->getLastRealOrder(); + $this->setData('flowbox', [ 'allowCookies' => $this->isUserAllowSaveCookies(), 'apiKey' => (string) $this->getApiKey(), @@ -61,8 +71,9 @@ protected function prepareConfig(): void 'orderId' => \ltrim($order->getIncrementId(), '#'), 'products' => \array_map( function ($item){ + $product = $this->productRepository->get($item->getSku()); return [ - 'id' => (string) $item->getData($this->helper->getAttributeCode()), + 'id' => (string) $product->getData($this->helper->getAttributeCode()), 'quantity' => (int) $item->getQtyOrdered() ]; },