Skip to content

Commit

Permalink
Source Entity expansion with Pickup Location attributes #2182.
Browse files Browse the repository at this point in the history
Frontend name and frontend description admin UI.
Some refactoring.
  • Loading branch information
novikor committed May 18, 2019
1 parent 4995e4c commit c537d6a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 45 deletions.
55 changes: 55 additions & 0 deletions InventoryInStorePickup/Model/Source/SetExtensionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\Source;

use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\DataObject;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;

/**
* Set store-pickup related source extension attributes
*/
class SetExtensionAttributes
{
/**
* @var ExtensionAttributesFactory
*/
private $extensionAttributesFactory;

/**
* @param ExtensionAttributesFactory $extensionAttributesFactory
*/
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory)
{
$this->extensionAttributesFactory = $extensionAttributesFactory;
}
/**
* @param SourceInterface|DataObject $source
*/
public function execute(SourceInterface $source): void
{
$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE);
$frontendName = $source->getData(PickupLocationInterface::FRONTEND_NAME);
$frontendDescription = $source->getData(PickupLocationInterface::FRONTEND_DESCRIPTION);

$extensionAttributes = $source->getExtensionAttributes();

if ($extensionAttributes === null) {
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class);
/** @noinspection PhpParamsInspection */
$source->setExtensionAttributes($extensionAttributes);
}

$extensionAttributes
->setIsPickupLocationActive((bool)$pickupAvailable)
->setFrontendName($frontendName)
->setFrontendDescription($frontendDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,24 @@

namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository;

use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\Data\SourceSearchResultsInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes;

class LoadInStorePickupOnGetListPlugin
{
/**
* @var ExtensionAttributesFactory
* @var SetExtensionAttributes
*/
private $extensionAttributesFactory;
private $setExtensionAttributes;

/**
* @param ExtensionAttributesFactory $extensionAttributesFactory
* @param SetExtensionAttributes $setExtensionAttributes
*/
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory)
{
$this->extensionAttributesFactory = $extensionAttributesFactory;
public function __construct(
SetExtensionAttributes $setExtensionAttributes
) {
$this->setExtensionAttributes = $setExtensionAttributes;
}

/**
Expand All @@ -40,18 +39,12 @@ public function __construct(ExtensionAttributesFactory $extensionAttributesFacto
public function afterGetList(
SourceRepositoryInterface $subject,
SourceSearchResultsInterface $sourceSearchResults
):SourceSearchResultsInterface {
foreach ($sourceSearchResults->getItems() as $source) {
$extensionAttributes = $source->getExtensionAttributes();

if ($extensionAttributes === null) {
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class);
$source->setExtensionAttributes($extensionAttributes);
}

$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE);
$extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable);
}
): SourceSearchResultsInterface {
$items = $sourceSearchResults->getItems();
array_walk(
$items,
[$this->setExtensionAttributes, 'execute']
);

return $sourceSearchResults;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,41 @@

namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository;

use Magento\Framework\Api\ExtensionAttributesFactory;
use Magento\Framework\DataObject;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface;
use Magento\InventoryInStorePickup\Model\Source\SetExtensionAttributes;

class LoadInStorePickupOnGetPlugin
{
/**
* @var \Magento\Framework\Api\ExtensionAttributesFactory
* @var SetExtensionAttributes
*/
private $extensionAttributesFactory;
private $setExtensionAttributes;

/**
* @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionAttributesFactory
* @param SetExtensionAttributes $setExtensionAttributes
*/
public function __construct(ExtensionAttributesFactory $extensionAttributesFactory)
{
$this->extensionAttributesFactory = $extensionAttributesFactory;
public function __construct(
SetExtensionAttributes $setExtensionAttributes
) {
$this->setExtensionAttributes = $setExtensionAttributes;
}

/**
* Enrich the given Source Objects with the In-Store pickup attribute
*
* @param SourceRepositoryInterface $subject
* @param SourceInterface $source
* @param SourceInterface|DataObject $source
*
* @return SourceInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(
SourceRepositoryInterface $subject,
SourceInterface $source
):SourceInterface {
$pickupAvailable = $source->getData(PickupLocationInterface::IS_PICKUP_LOCATION_ACTIVE);

$extensionAttributes = $source->getExtensionAttributes();

if ($extensionAttributes === null) {
$extensionAttributes = $this->extensionAttributesFactory->create(SourceInterface::class);
$source->setExtensionAttributes($extensionAttributes);
}

$extensionAttributes->setIsPickupLocationActive((bool)$pickupAvailable);
): SourceInterface {
$this->setExtensionAttributes->execute($source);

return $source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@

namespace Magento\InventoryInStorePickup\Plugin\InventoryApi\SourceRepository;

use Magento\Framework\DataObject;
use Magento\InventoryApi\Api\Data\SourceInterface;
use Magento\InventoryApi\Api\SourceRepositoryInterface;
use Magento\InventoryInStorePickupApi\Api\Data\PickupLocationInterface as Location;

class SaveInStorePickupPlugin
{
/**
* Persist the In-Store pickup attribute on Source save
*
* @param SourceRepositoryInterface $subject
* @param SourceInterface $source
* @param SourceInterface|DataObject $source
*
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeSave(
SourceRepositoryInterface $subject,
SourceInterface $source
):array {
): array {
$extensionAttributes = $source->getExtensionAttributes();

if ($extensionAttributes !== null && $extensionAttributes->getIsPickupLocationActive() !== null) {
$source->setIsPickupLocationActive($extensionAttributes->getIsPickupLocationActive());
if ($extensionAttributes !== null) {
$source->setData(Location::IS_PICKUP_LOCATION_ACTIVE, $extensionAttributes->getIsPickupLocationActive());
$source->setData(Location::FRONTEND_NAME, $extensionAttributes->getFrontendName());
$source->setData(Location::FRONTEND_DESCRIPTION, $extensionAttributes->getFrontendDescription());
}

return [$source];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,38 @@
</formElements>
</field>
</fieldset>
<fieldset name="pickup_location" sortOrder="100">
<settings>
<collapsible>true</collapsible>
<label translate="true">Pickup Location</label>
<dataScope>general</dataScope>
</settings>
<field name="frontend_name">
<settings>
<dataScope>extension_attributes.frontend_name</dataScope>
<dataType>string</dataType>
<label translate="true">Frontend name</label>
<tooltip>
<description translate="true">Pickup Location name visible for the customer on checkout</description>
</tooltip>
</settings>
</field>
<field name="frontend_description" formElement="wysiwyg">
<settings>
<dataScope>extension_attributes.frontend_description</dataScope>
<dataType>string</dataType>
<label translate="true">Frontend description</label>
<tooltip>
<description translate="true">Pickup Location description</description>
</tooltip>
</settings>
<formElements>
<wysiwyg>
<settings>
<wysiwyg>true</wysiwyg>
</settings>
</wysiwyg>
</formElements>
</field>
</fieldset>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
interface PickupLocationInterface extends ExtensibleDataInterface
{
const IS_PICKUP_LOCATION_ACTIVE = 'is_pickup_location_active';
const FRONTEND_NAME = 'frontend_name';
const FRONTEND_DESCRIPTION = 'frontend_description';

/**
* Get source code of Pickup Location
Expand Down

0 comments on commit c537d6a

Please sign in to comment.