Skip to content

Commit d3ead30

Browse files
Merge pull request #2082 from magento-engcom/MSI-2030-add-ability-to-mark-orders-placed-with-store-pickup
Msi 2030 add ability to mark orders placed with store pickup
2 parents cde7372 + 97556b5 commit d3ead30

15 files changed

+561
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
/**
13+
* Get Pickup Location identifier by order identifier.
14+
*/
15+
class GetPickupLocationByOrderId
16+
{
17+
private const ORDER_ID = 'order_id';
18+
19+
private const PICKUP_LOCATION_CODE = 'pickup_location_code';
20+
21+
/**
22+
* @var \Magento\Framework\App\ResourceConnection
23+
*/
24+
private $connection;
25+
26+
/**
27+
* @param \Magento\Framework\App\ResourceConnection $connection
28+
*/
29+
public function __construct(
30+
ResourceConnection $connection
31+
) {
32+
$this->connection = $connection;
33+
}
34+
35+
/**
36+
* Fetch pickup location identifier by order identifier.
37+
*
38+
* @param int $orderId
39+
*
40+
* @return string|null
41+
*/
42+
public function execute(int $orderId): ?string
43+
{
44+
$connection = $this->connection->getConnection();
45+
$table = $this->connection->getTableName('inventory_pickup_location_order');
46+
47+
$select = $connection->select()
48+
->from($table, [self::PICKUP_LOCATION_CODE => self::PICKUP_LOCATION_CODE])
49+
->where(self::ORDER_ID . '= ?', $orderId)
50+
->limit(1);
51+
52+
$id = $connection->fetchOne($select);
53+
54+
return $id ?: null;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation;
9+
10+
use Magento\Framework\App\ResourceConnection;
11+
12+
/**
13+
* Save Order Pickup Location
14+
*/
15+
class SaveOrderPickupLocation
16+
{
17+
private const ORDER_ID = 'order_id';
18+
private const PICKUP_LOCATION_CODE = 'pickup_location_code';
19+
20+
/**
21+
* @var \Magento\Framework\App\ResourceConnection
22+
*/
23+
private $connection;
24+
25+
/**
26+
* GetPickupLocationByOrderId constructor.
27+
*
28+
* @param \Magento\Framework\App\ResourceConnection $connection
29+
*/
30+
public function __construct(
31+
ResourceConnection $connection
32+
) {
33+
$this->connection = $connection;
34+
}
35+
36+
/**
37+
* Fetch pickup location identifier by order identifier.
38+
*
39+
* @param int $orderId
40+
* @param string $pickupLocationCode
41+
*
42+
* @return void
43+
*/
44+
public function execute(int $orderId, string $pickupLocationCode): void
45+
{
46+
$connection = $this->connection->getConnection();
47+
$table = $this->connection->getTableName('inventory_pickup_location_order');
48+
49+
$data = [
50+
self::ORDER_ID => $orderId,
51+
self::PICKUP_LOCATION_CODE => $pickupLocationCode
52+
];
53+
54+
$connection->insertOnDuplicate($table, $data);
55+
}
56+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryInStorePickup\Plugin\Sales\Order;
9+
10+
use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\GetPickupLocationByOrderId;
11+
use Magento\Sales\Api\Data\OrderExtensionFactory;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
use Magento\Sales\Api\OrderRepositoryInterface;
14+
15+
/**
16+
* Set Pickup Location identifier to Order Entity.
17+
*/
18+
class GetPickupLocationForOrderPlugin
19+
{
20+
/**
21+
* @var OrderExtensionFactory
22+
*/
23+
private $orderExtensionFactory;
24+
25+
/**
26+
* @var GetPickupLocationByOrderId
27+
*/
28+
private $getPickupLocationByOrderId;
29+
30+
/**
31+
* @param OrderExtensionFactory $orderExtensionFactory
32+
* @param GetPickupLocationByOrderId $getPickupLocationByOrderId
33+
*/
34+
public function __construct(
35+
OrderExtensionFactory $orderExtensionFactory,
36+
GetPickupLocationByOrderId $getPickupLocationByOrderId
37+
) {
38+
$this->orderExtensionFactory = $orderExtensionFactory;
39+
$this->getPickupLocationByOrderId = $getPickupLocationByOrderId;
40+
}
41+
42+
/**
43+
* Add Pickup Location Code extension attribute when loading Order with OrderRepository.
44+
*
45+
* @param OrderRepositoryInterface $orderRepository
46+
* @param OrderInterface $order
47+
*
48+
* @return OrderInterface
49+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
50+
*/
51+
public function afterGet(OrderRepositoryInterface $orderRepository, OrderInterface $order): OrderInterface
52+
{
53+
$extension = $order->getExtensionAttributes();
54+
55+
if (empty($extension)) {
56+
$extension = $this->orderExtensionFactory->create();
57+
}
58+
59+
if ($extension->getPickupLocationCode()) {
60+
return $order;
61+
}
62+
63+
$pickupLocationCode = $this->getPickupLocationByOrderId->execute((int)$order->getEntityId());
64+
65+
if ($pickupLocationCode) {
66+
$extension->setPickupLocationCode($pickupLocationCode);
67+
}
68+
69+
$order->setExtensionAttributes($extension);
70+
71+
return $order;
72+
}
73+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryInStorePickup\Plugin\Sales\Order;
9+
10+
use Magento\InventoryInStorePickup\Model\ResourceModel\OrderPickupLocation\SaveOrderPickupLocation;
11+
use Magento\Sales\Api\OrderRepositoryInterface;
12+
use Magento\Sales\Api\Data\OrderInterface;
13+
14+
/**
15+
* Save Pickup Location identifier, related to the Order Entity.
16+
*/
17+
class SavePickupLocationForOrderPlugin
18+
{
19+
/**
20+
* @var SaveOrderPickupLocation
21+
*/
22+
private $saveOrderPickupLocation;
23+
24+
/**
25+
* @param SaveOrderPickupLocation $saveOrderPickupLocation
26+
*/
27+
public function __construct(SaveOrderPickupLocation $saveOrderPickupLocation)
28+
{
29+
$this->saveOrderPickupLocation = $saveOrderPickupLocation;
30+
}
31+
32+
/**
33+
* Save Order to Pickup Location relation when saving the order.
34+
*
35+
* @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
36+
* @param \Magento\Sales\Api\Data\OrderInterface $result
37+
* @param \Magento\Sales\Api\Data\OrderInterface $entity
38+
*
39+
* @return \Magento\Sales\Api\Data\OrderInterface
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function afterSave(
43+
OrderRepositoryInterface $orderRepository,
44+
OrderInterface $result,
45+
OrderInterface $entity
46+
) {
47+
$extension = $result->getExtensionAttributes();
48+
49+
if (!empty($extension) && $extension->getPickupLocationCode()) {
50+
$this->saveOrderPickupLocation->execute((int)$result->getEntityId(), $extension->getPickupLocationCode());
51+
}
52+
53+
return $result;
54+
}
55+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\InventoryInStorePickup\Test\Integration;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Sales\Api\Data\OrderExtensionFactory;
13+
use Magento\Sales\Api\Data\OrderInterface;
14+
use Magento\Sales\Api\OrderRepositoryInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class PickupLocationOrderTest extends TestCase
19+
{
20+
/** @var ObjectManagerInterface */
21+
private $objectManager;
22+
23+
/** @var OrderRepositoryInterface */
24+
private $orderRepository;
25+
26+
/** @var SearchCriteriaBuilder */
27+
private $searchCriteriaBuilder;
28+
29+
/** @var OrderExtensionFactory */
30+
private $orderExtensionFactory;
31+
32+
protected function setUp()
33+
{
34+
$this->objectManager = Bootstrap::getObjectManager();
35+
36+
$this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
37+
$this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
38+
$this->orderExtensionFactory = $this->objectManager->get(OrderExtensionFactory::class);
39+
}
40+
41+
/**
42+
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
43+
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
44+
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stocks.php
45+
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/stock_source_links.php
46+
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/websites_with_stores.php
47+
* @magentoDataFixture ../../../../app/code/Magento/InventorySalesApi/Test/_files/stock_website_sales_channels.php
48+
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source_items.php
49+
* @magentoDataFixture ../../../../app/code/Magento/InventoryIndexer/Test/_files/reindex_inventory.php
50+
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/create_in_store_pickup_quote_on_eu_website.php
51+
* @magentoDataFixture ../../../../app/code/Magento/InventoryInStorePickup/Test/_files/place_order.php
52+
*
53+
* @magentoDbIsolation disabled
54+
*/
55+
public function testPickupLocationSaveWithOrder()
56+
{
57+
$sourceId = 'eu-1';
58+
59+
$searchCriteria = $this->searchCriteriaBuilder
60+
->addFilter('increment_id', 'in_store_pickup_test_order')
61+
->create();
62+
/** @var OrderInterface $createdOrder */
63+
$createdOrder = current($this->orderRepository->getList($searchCriteria)->getItems());
64+
$orderId = $createdOrder->getEntityId();
65+
66+
$extension = $createdOrder->getExtensionAttributes();
67+
68+
if (empty($extension)) {
69+
/** @var \Magento\Sales\Api\Data\OrderExtensionInterface $extension */
70+
$extension = $this->orderExtensionFactory->create();
71+
}
72+
73+
$extension->setPickupLocationCode($sourceId);
74+
$createdOrder->setExtensionAttributes($extension);
75+
76+
$this->orderRepository->save($createdOrder);
77+
78+
// Remove value to re-load from DB during 'get'.
79+
$extension->setPickupLocationCode(null);
80+
81+
$order = $this->orderRepository->get($orderId);
82+
83+
$this->assertEquals($order->getExtensionAttributes()->getPickupLocationCode(), $sourceId);
84+
}
85+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Quote\Api\CartManagementInterface;
9+
use Magento\Quote\Api\CartRepositoryInterface;
10+
use Magento\Quote\Api\Data\AddressInterface;
11+
use Magento\Quote\Api\Data\AddressInterfaceFactory;
12+
use Magento\Store\Api\StoreRepositoryInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
16+
/** @var CartRepositoryInterface $cartRepository */
17+
$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
18+
/** @var CartManagementInterface $cartManagement */
19+
$cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
20+
/** @var AddressInterfaceFactory $addressFactory */
21+
$addressFactory = Bootstrap::getObjectManager()->get(AddressInterfaceFactory::class);
22+
/** @var StoreRepositoryInterface $storeRepository */
23+
$storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
24+
/** @var StoreManagerInterface\ $storeManager */
25+
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
26+
27+
$cartId = $cartManagement->createEmptyCart();
28+
$cart = $cartRepository->get($cartId);
29+
$cart->setCustomerEmail('admin@example.com');
30+
$cart->setCustomerIsGuest(true);
31+
$store = $storeRepository->get('store_for_eu_website');
32+
$cart->setStoreId($store->getId());
33+
$storeManager->setCurrentStore($store->getCode());
34+
35+
/** @var AddressInterface $address */
36+
$address = $addressFactory->create(
37+
[
38+
'data' => [
39+
AddressInterface::KEY_COUNTRY_ID => 'US',
40+
AddressInterface::KEY_REGION_ID => 15,
41+
AddressInterface::KEY_LASTNAME => 'Doe',
42+
AddressInterface::KEY_FIRSTNAME => 'John',
43+
AddressInterface::KEY_STREET => 'example street',
44+
AddressInterface::KEY_EMAIL => 'customer@example.com',
45+
AddressInterface::KEY_CITY => 'Los Angeles',
46+
AddressInterface::KEY_TELEPHONE => '937 99 92',
47+
AddressInterface::KEY_POSTCODE => 12345
48+
]
49+
]
50+
);
51+
$cart->setReservedOrderId('in_store_pickup_test_order');
52+
$cart->setBillingAddress($address);
53+
$cart->setShippingAddress($address);
54+
$cart->getPayment()->setMethod('checkmo');
55+
/** Will be replaced with 'In Store Pickup' delivery method */
56+
$cart->getShippingAddress()->setShippingMethod('flatrate_flatrate');
57+
$cart->getShippingAddress()->setCollectShippingRates(true);
58+
$cart->getShippingAddress()->collectShippingRates();
59+
$cartRepository->save($cart);

0 commit comments

Comments
 (0)