Skip to content

Commit

Permalink
Advanced Shipping Mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin-Magmodules committed Jan 29, 2025
1 parent 8f78d2e commit 32e9548
Show file tree
Hide file tree
Showing 15 changed files with 703 additions and 45 deletions.
15 changes: 12 additions & 3 deletions Api/Config/System/OrderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface OrderInterface extends ReturnsInterface
const XML_PATH_SHIPPING_METHOD = 'magmodules_channable_marketplace/order/shipping_method';
const XML_PATH_SHIPPING_CUSTOM = 'magmodules_channable_marketplace/order/shipping_method_custom';
const XML_PATH_SHIPPING_METHOD_FALLBACK = 'magmodules_channable_marketplace/order/shipping_method_fallback';
const XML_PATH_ADVANCED_SHIPPING_MAPPING = 'magmodules_channable_marketplace/order/advanced_shipment_mapping';
const XML_PATH_RETURN_LABEL = 'magmodules_channable_marketplace/order/return_label';
const XML_PATH_RETURN_LABEL_REGEXP = 'magmodules_channable_marketplace/order/return_label_regexp';
const XML_PATH_IMPORT_CUSTOMER = 'magmodules_channable_marketplace/order/import_customer';
Expand Down Expand Up @@ -90,6 +91,14 @@ public function getCustomShippingMethodLogic(int $storeId = null): array;
*/
public function getFallbackShippingMethod(int $storeId = null): ?string;

/**
* Retrieve the advanced shipping mapping for a given store.
*
* @param int|null $storeId
* @return array|null
*/
public function getAdvancedShippingMapping(?int $storeId = null): ?array;

/**
* Create customer on order import
*
Expand All @@ -109,8 +118,8 @@ public function createCustomerOnImport(int $storeId = null): bool;
public function customerGroupForOrderImport(int $storeId = null): ?string;

/**
* Seperate housenumber into 'streets'. Option is used when second steet
* is used as housenumber field.
* Separate house number into 'streets'. Option is used when second street
* is used as house number field.
*
* @param null|int $storeId
*
Expand Down Expand Up @@ -269,7 +278,7 @@ public function disableStockMovementForLvbOrders(int $storeId = null): bool;

/**
* Create shipment for LVB/FBV orders on order import.
* As these orders are shipped directly by the channel no handeling should be done on Magento side.
* As these orders are shipped directly by the channel no handling should be done on Magento side.
*
* @param null|int $storeId
*
Expand Down
12 changes: 12 additions & 0 deletions Api/Order/Data/DataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface DataInterface extends ExtensibleDataInterface
const CHANNABLE_ID = 'channable_id';
const CHANNEL_ID = 'channel_id';
const CHANNEL_NAME = 'channel_name';
const SHIPMENT_METHOD = 'shipment_method';
const CHANNEL_LABEL = 'channel_label';
const CHANNABLE_ORDER_STATUS = 'channable_order_status';
const IS_TEST = 'is_test';
Expand Down Expand Up @@ -81,6 +82,17 @@ public function getChannelName(): string;
*/
public function setChannelName(string $channelName): self;

/**
* @return string|null
*/
public function getShipmentMethod(): string;

/**
* @param string|null $method
* @return $this
*/
public function setShipmentMethod(?string $method): self;

/**
* @return string
*/
Expand Down
110 changes: 110 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/AdvancedShipmentMapping.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/*
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field;

use Exception;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\DataObject;
use Magento\Framework\View\Element\BlockInterface as ElementBlockInterface;
use Magmodules\Channable\Api\Log\RepositoryInterface as LogRepository;

/**
* Represents a table for advanced shipping method mapping in the admin configuration
*/
class AdvancedShipmentMapping extends AbstractFieldArray
{
public const OPTION_PATTERN = 'option_%s';
public const SELECTED = 'selected="selected"';
public const RENDERERS = [
'channel' => Renderer\Channels::class,
'method' => Renderer\ShippingMethods::class,
];

private array $renderers = [];
private LogRepository $logger;

public const COLUMN_CHANNEL = 'channel';
public const COLUMN_CHANNABLE_CARRIER = 'channable_carrier';
public const COLUMN_METHOD = 'method';

public function __construct(
Context $context,
LogRepository $logger,
array $data = []
) {
$this->logger = $logger;
parent::__construct($context, $data);
}

/**
* Prepare columns for rendering.
*
* @return void
*/
protected function _prepareToRender()
{
$this->addColumn(self::COLUMN_CHANNEL, [
'label' => (string)__('Channel'),
'class' => 'required-entry',
'renderer' => $this->getRenderer('channel'),
]);
$this->addColumn(self::COLUMN_CHANNABLE_CARRIER, [
'label' => (string)__('Carrier'),
'class' => 'required-entry',
]);
$this->addColumn(self::COLUMN_METHOD, [
'label' => (string)__('Magento Method'),
'class' => 'required-entry',
'renderer' => $this->getRenderer('method'),
]);
$this->_addAfter = false;
$this->_addButtonLabel = (string)__('Add');
}

/**
* Get renderer for a specific column type.
*
* @param string $type
* @return ElementBlockInterface
*/
public function getRenderer(string $type): ElementBlockInterface
{
if (!isset($this->renderers[$type])) {
try {
$this->renderers[$type] = $this->getLayout()->createBlock(
self::RENDERERS[$type],
'',
['data' => ['is_render_to_js_template' => true]]
);
} catch (Exception $e) {
$this->logger->addErrorLog('RendererCreationError', $e->getMessage());
}
}

return $this->renderers[$type];
}

/**
* Prepare extra attributes for array row rendering.
*
* @param DataObject $row
* @return void
*/
protected function _prepareArrayRow(DataObject $row)
{
$options = [];
foreach (array_keys(self::RENDERERS) as $element) {
if ($elementData = $row->getData($element)) {
$renderer = $this->getRenderer($element);
$options[sprintf(self::OPTION_PATTERN, $renderer->calcOptionHash($elementData))] = self::SELECTED;
}
}
$row->setData('option_extra_attrs', $options);
}
}
56 changes: 56 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/Renderer/Channels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field\Renderer;

use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magmodules\Channable\Model\Config\Source\Channels as ChannelsSource;

class Channels extends Select
{

private ChannelsSource $channels;

public function __construct(
Context $context,
ChannelsSource $channels,
array $data = []
) {
parent::__construct($context, $data);
$this->channels = $channels;
}

/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml(): string
{
if (empty($this->getOptions())) {
$this->addOption(null, '-- select --');
foreach ($this->channels->toOptionArray() as $channel) {
$this->addOption($channel['value'], $channel['label']);
}
}

return parent::_toHtml();
}

/**
* Sets the name for the input element.
*
* @param string $value
* @return $this
*/
public function setInputName(string $value): self
{
$this->setData('name', $value);
return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magmodules\Channable\Block\Adminhtml\System\Config\Form\Field\Renderer;

use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Html\Select;
use Magmodules\Channable\Model\Config\Source\ShippingMethods as ShippingMethodsSource;

class ShippingMethods extends Select
{

private ShippingMethodsSource $shippingMethods;

public function __construct(
Context $context,
ShippingMethodsSource $shippingMethods,
array $data = []
) {
parent::__construct($context, $data);
$this->shippingMethods = $shippingMethods;
}

/**
* Render block HTML.
*
* @return string
*/
protected function _toHtml(): string
{
if (empty($this->getOptions())) {
foreach ($this->shippingMethods->toOptionArray() as $method) {
$this->addOption($method['value'], $method['label']);
}
}

return parent::_toHtml();
}

/**
* Sets the name for the input element.
*
* @param string $value
* @return $this
*/
public function setInputName(string $value): self
{
$this->setName($value);
return $this;
}
}
Loading

0 comments on commit 32e9548

Please sign in to comment.