Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Msi 2030 add ability to mark orders placed with store pickup #2082

Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation;

use Magento\Framework\App\ResourceConnection;

/**
* Get Pickup Location identifier by order identifier.
*/
class GetPickupLocationByOrderId
{
private const ORDER_ID = 'order_id';

private const PICKUP_LOCATION_CODE = 'pickup_location_code';

/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $connection;

/**
* @param \Magento\Framework\App\ResourceConnection $connection
*/
public function __construct(
ResourceConnection $connection
) {
$this->connection = $connection;
}

/**
* Fetch pickup location identifier by order identifier.
*
* @param int $orderId
*
* @return string|null
*/
public function execute(int $orderId): ?string
{
$connection = $this->connection->getConnection();
$table = $this->connection->getTableName('inventory_pickup_location_order');

$select = $connection->select()
->from($table, [self::PICKUP_LOCATION_CODE => self::PICKUP_LOCATION_CODE])
->where(self::ORDER_ID . '= ?', $orderId)
->limit(1);

$id = $connection->fetchOne($select);

return $id ?: null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation;

use Magento\Framework\App\ResourceConnection;

/**
* Save Order Pickup Location
*/
class SaveOrderPickupLocation
{
private const ORDER_ID = 'order_id';
private const PICKUP_LOCATION_CODE = 'pickup_location_code';

/**
* @var \Magento\Framework\App\ResourceConnection
*/
private $connection;

/**
* GetPickupLocationByOrderId constructor.
*
* @param \Magento\Framework\App\ResourceConnection $connection
*/
public function __construct(
ResourceConnection $connection
) {
$this->connection = $connection;
}

/**
* Fetch pickup location identifier by order identifier.
*
* @param int $orderId
* @param string $pickupLocationCode
*
* @return void
*/
public function execute(int $orderId, string $pickupLocationCode): void
{
$connection = $this->connection->getConnection();
$table = $this->connection->getTableName('inventory_pickup_location_order');

$data = [
self::ORDER_ID => $orderId,
self::PICKUP_LOCATION_CODE => $pickupLocationCode
];

$connection->insertOnDuplicate($table, $data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Plugin\Sales\Order;

use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\GetPickupLocationByOrderId;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;

/**
* Set Pickup Location identifier to Order Entity.
*/
class GetPickupLocationForOrderPlugin
{
/**
* @var OrderExtensionFactory
*/
private $orderExtensionFactory;

/**
* @var GetPickupLocationByOrderId
*/
private $getPickupLocationByOrderId;

/**
* @param OrderExtensionFactory $orderExtensionFactory
* @param GetPickupLocationByOrderId $getPickupLocationByOrderId
*/
public function __construct(
OrderExtensionFactory $orderExtensionFactory,
GetPickupLocationByOrderId $getPickupLocationByOrderId
) {
$this->orderExtensionFactory = $orderExtensionFactory;
$this->getPickupLocationByOrderId = $getPickupLocationByOrderId;
}

/**
* Add Pickup Location Code extension attribute when loading Order with OrderRepository.
*
* @param OrderRepositoryInterface $orderRepository
* @param OrderInterface $order
*
* @return OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterGet(OrderRepositoryInterface $orderRepository, OrderInterface $order): OrderInterface
{
$extension = $order->getExtensionAttributes();

if (empty($extension)) {
$extension = $this->orderExtensionFactory->create();
}

if ($extension->getPickupLocationCode()) {
return $order;
}

$pickupLocationCode = $this->getPickupLocationByOrderId->execute((int)$order->getEntityId());

if ($pickupLocationCode) {
$extension->setPickupLocationCode($pickupLocationCode);
}

$order->setExtensionAttributes($extension);

return $order;
}
}
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\Plugin\Sales\Order;

use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\SaveOrderPickupLocation;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;

/**
* Save Pickup Location identifier, related to the Order Entity.
*/
class SavePickupLocationForOrderPlugin
{
/**
* @var SaveOrderPickupLocation
*/
private $saveOrderPickupLocation;

/**
* @param SaveOrderPickupLocation $saveOrderPickupLocation
*/
public function __construct(SaveOrderPickupLocation $saveOrderPickupLocation)
{
$this->saveOrderPickupLocation = $saveOrderPickupLocation;
}

/**
* Save Order to Pickup Location relation when saving the order.
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
* @param \Magento\Sales\Api\Data\OrderInterface $result
* @param \Magento\Sales\Api\Data\OrderInterface $entity
*
* @return \Magento\Sales\Api\Data\OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSave(
OrderRepositoryInterface $orderRepository,
OrderInterface $result,
OrderInterface $entity
) {
$extension = $result->getExtensionAttributes();

if (!empty($extension) && $extension->getPickupLocationCode()) {
$this->saveOrderPickupLocation->execute((int)$result->getEntityId(), $extension->getPickupLocationCode());
}

return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\InventoryInStorePickup\Test\Integration;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

class PickupLocationOrderTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;

/** @var OrderRepositoryInterface */
private $orderRepository;

/** @var SearchCriteriaBuilder */
private $searchCriteriaBuilder;

/** @var OrderExtensionFactory */
private $orderExtensionFactory;

protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();

$this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
$this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
$this->orderExtensionFactory = $this->objectManager->get(OrderExtensionFactory::class);
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/create_in_store_pickup_quote_on_eu_website.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/place_order.php
*
* @magentoDbIsolation disabled
*/
public function testPickupLocationSaveWithOrder()
{
$sourceId = 'eu-1';

$searchCriteria = $this->searchCriteriaBuilder
->addFilter('increment_id', 'in_store_pickup_test_order')
->create();
/** @var OrderInterface $createdOrder */
$createdOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
$orderId = $createdOrder->getEntityId();

$extension = $createdOrder->getExtensionAttributes();

if (empty($extension)) {
/** @var \Magento\Sales\Api\Data\OrderExtensionInterface $extension */
$extension = $this->orderExtensionFactory->create();
}

$extension->setPickupLocationCode($sourceId);
$createdOrder->setExtensionAttributes($extension);

$this->orderRepository->save($createdOrder);

// Remove value to re-load from DB during 'get'.
$extension->setPickupLocationCode(null);

$order = $this->orderRepository->get($orderId);

$this->assertEquals($order->getExtensionAttributes()->getPickupLocationCode(), $sourceId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\AddressInterfaceFactory;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;

/** @var CartRepositoryInterface $cartRepository */
$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
/** @var CartManagementInterface $cartManagement */
$cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
/** @var AddressInterfaceFactory $addressFactory */
$addressFactory = Bootstrap::getObjectManager()->get(AddressInterfaceFactory::class);
/** @var StoreRepositoryInterface $storeRepository */
$storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
/** @var StoreManagerInterface\ $storeManager */
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);

$cartId = $cartManagement->createEmptyCart();
$cart = $cartRepository->get($cartId);
$cart->setCustomerEmail('admin@example.com');
$cart->setCustomerIsGuest(true);
$store = $storeRepository->get('store_for_eu_website');
$cart->setStoreId($store->getId());
$storeManager->setCurrentStore($store->getCode());

/** @var AddressInterface $address */
$address = $addressFactory->create(
[
'data' => [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how you save a US address to the Quote in fixture designed for EU Website :)

AddressInterface::KEY_COUNTRY_ID => 'US',
AddressInterface::KEY_REGION_ID => 15,
AddressInterface::KEY_LASTNAME => 'Doe',
AddressInterface::KEY_FIRSTNAME => 'John',
AddressInterface::KEY_STREET => 'example street',
AddressInterface::KEY_EMAIL => 'customer@example.com',
AddressInterface::KEY_CITY => 'Los Angeles',
AddressInterface::KEY_TELEPHONE => '937 99 92',
AddressInterface::KEY_POSTCODE => 12345
]
]
);
$cart->setReservedOrderId('in_store_pickup_test_order');
$cart->setBillingAddress($address);
$cart->setShippingAddress($address);
$cart->getPayment()->setMethod('checkmo');
/** Will be replaced with 'In Store Pickup' delivery method */
$cart->getShippingAddress()->setShippingMethod('flatrate_flatrate');
$cart->getShippingAddress()->setCollectShippingRates(true);
$cart->getShippingAddress()->collectShippingRates();
$cartRepository->save($cart);
Loading