Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 40 additions & 13 deletions Block/Checkout/Onepage/Success.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,58 @@
<?php declare(strict_types=1);
<?php

/**
* Copyright © Itonomy BV. All rights reserved.
* See LICENSE.md for license details.
*/

namespace Itonomy\Flowbox\Block\Checkout\Onepage;

use Itonomy\Flowbox\Helper\Data;
use Magento\Checkout\Model\Session;
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
{
private \Magento\Checkout\Model\Session $checkoutSession;
/**
* @var Session
*/
private $checkoutSession;

/**
* @var Data
*/
private $helper;

/**
* @var ProductRepository
*/
private $productRepository;

/**
* Success constructor.
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Magento\Cookie\Helper\Cookie $cookie
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param Context $context
* @param EncryptorInterface $encryptor
* @param Cookie $cookie
* @param Session $checkoutSession
* @param ProductRepository $productRepository
* @param Data $helper
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Cookie\Helper\Cookie $cookie,
\Magento\Checkout\Model\Session $checkoutSession,
Context $context,
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;
}

/**
Expand All @@ -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()
];
},
Expand Down
47 changes: 47 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Itonomy\Flowbox\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Data extends AbstractHelper
{
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* @param ScopeConfigInterface $scopeConfig
* @param StoreManagerInterface $storeManager
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager
) {
$this->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()
);
}
}
63 changes: 63 additions & 0 deletions Model/Config/Source/CheckoutIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php


namespace Itonomy\Flowbox\Model\Config\Source;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection;
use Magento\Framework\Data\OptionSourceInterface;


class CheckoutIdentifier implements OptionSourceInterface
{
/**
* @var Collection
*/
private $collection;


/**
* @param Collection $collection
*/
public function __construct(
Collection $collection
)
{
$this->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;
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<comment>Your Flowbox API key</comment>
<backend_model>Magento\Config\Model\Config\Backend\Encrypted</backend_model>
</field>
<field id="checkout_identifier" type="select" translate="label comment" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Checkout Identifier</label>
<source_model>Itonomy\Flowbox\Model\Config\Source\CheckoutIdentifier</source_model>
<comment>Select the product identifier for the checkout script</comment>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<general>
<enable>1</enable>
<debug_javascript>0</debug_javascript>
<checkout_identifier>sku</checkout_identifier>
</general>
</itonomy_flowbox>
<dev>
Expand Down