I am using store pickup of magento 2.4. I want to my store option to be preselected on checkout, as user have to search for it every time. Anyone have done this? https://nimb.ws/VNMWI1
6 Answers
We can rewrite the method "execute" of the file vendor/magento/module-inventory-in-store-pickup/Model/GetPickupLocations.php
and put the following content in the method.
public function execute(SearchRequestInterface $searchRequest): SearchResultInterface
{
$searchResult = $this->sourceRepository->getList()->getItems();
unset($searchResult['default']);
$sources = $searchResult;
$pickupLocations = [];
foreach ($sources as $source) {
$pickupLocations[] = $this->mapper->map($source);
}
return $this->searchResultFactory->create(
[
'items' => $pickupLocations,
'totalCount' => count($pickupLocations),
'searchRequest' => $searchRequest
]
);
}
This code avoids the queries to the google map api (paid api)
I think your issue is most likely with the Magento determining the distance from the customer to the store. Using this it can suggest the closest store. (Even if you only have one store, if it doesn't know the distance it won't suggest any.) Have you setup Google Map API? Make sure the Latitude and Longitude of your configured source is entered and correct. Make sure your testing from a location that is within the search radius configured under Delivery Methods.
It gave me a hard time originally but after messing with it I got it to show up automatically.
-
Can't we show the source directly without messing with google maps? I haven't enabled maps or coordinates for my source. Also where to get google maps api and how much does that cost? Mar 5, 2021 at 6:34
-
If you go to Stores > Configuration > Delivery Methods > In Store Pick Up and set 'Search Radius' to 40,000 it'll make your store the closest one no matter where your billing address is set. Then it pops up automatically. Seems to work so far for me.
Chen Hanhan, how could I put the method suggested by you into a custom module to override original pickup location method in the storefront?
try this code down which is selected auto pickup by checking website: /** * @inheritdoc / class GetPickupLocations implements GetPickupLocationsInterface { /* * @var Mapper */ private $mapper;
/**
* @var SourceRepositoryInterface
*/
private $sourceRepository;
/**
* @var SearchCriteriaResolverInterface
*/
private $searchCriteriaResolver;
/**
* @var ExtractorInterface
*/
private $extractor;
/**
* @var SearchResultInterfaceFactory
*/
private $searchResultFactory;
/**
* @param Mapper $mapper
* @param SourceRepositoryInterface $sourceRepository
* @param ExtractorInterface $extractor
* @param SearchCriteriaResolverInterface $searchCriteriaResolver
* @param SearchResultInterfaceFactory $searchResultFactory
*/
public function __construct(
Mapper $mapper,
SourceRepositoryInterface $sourceRepository,
ExtractorInterface $extractor,
SearchCriteriaResolverInterface $searchCriteriaResolver,
SearchResultInterfaceFactory $searchResultFactory,
InventoryCollection $inventoryCollection,
SortOrderBuilder $sortOrderBuilder,
GetStockSourceLinksInterface $getStockSourceLinks,
ResourceConnection $resourceConnection,
SearchCriteriaBuilder $searchCriteriaBuilder,
StoreManagerInterface $_storeManager
) {
$this->inventoryCollection = $inventoryCollection;
$this->sortOrderBuilder = $sortOrderBuilder;
$this->getStockSourceLinks = $getStockSourceLinks;
$this->resourceConnection = $resourceConnection;
$this->_searchCriteriaBuilder = $searchCriteriaBuilder;
$this->_storeManager = $_storeManager;
$this->mapper = $mapper;
$this->sourceRepository = $sourceRepository;
$this->searchCriteriaResolver = $searchCriteriaResolver;
$this->extractor = $extractor;
$this->searchResultFactory = $searchResultFactory;
}
/**
* @inheritdoc
*/
public function execute(SearchRequestInterface $searchRequest): SearchResultInterface {
$searchResult = $this->sourceRepository->getList()->getItems();
// searchresult remove default
unset($searchResult['default']);
$sources = $searchResult;
$pickupLocations = [];
//get currnt store source
$currentSource = $this->getInventorySources();
foreach ($sources as $source) {
$checkPickupSource = $this->mapper->map($source);
if ($checkPickupSource->getName() == $currentSource['0']['sourceName']) {
$pickupLocations[] = $checkPickupSource;
}
}
return $this->searchResultFactory->create(
[
'items' => $pickupLocations,
'totalCount' => count($pickupLocations),
'searchRequest' => $searchRequest
]
);
}
/**
* store source Inventory
* @return String
*/
public function getInventorySources() {
$websiteCode = $this->_storeManager->getStore()->getWebsite()->getCode();
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName('inventory_stock_sales_channel');
$select = $connection->select()
->from($tableName)
->where('code = ?', $websiteCode);
$StockCollection = $connection->fetchAll($select);
$stockId = $StockCollection[0]['stock_id'];
$sortOrder = $this->sortOrderBuilder
->setField(StockSourceLinkInterface::PRIORITY)
->setAscendingDirection()
->create();
$searchCriteria = $this->_searchCriteriaBuilder
->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
->addSortOrder($sortOrder)
->create();
$searchResult = $this->getStockSourceLinks->execute($searchCriteria);
if ($searchResult->getTotalCount() === 0) {
return [];
}
$assignedSourcesData = [];
foreach ($searchResult->getItems() as $link) {
$source = $this->sourceRepository->get($link->getSourceCode());
$assignedSourcesData[] = [
'sourceName' => $source->getName(),
];
}
return $assignedSourcesData;
}
}
To set the "Store Pickup" option as the default shipping method on the Magento 2.4 checkout page, you can follow these steps:
- Log in to your Magento 2.4 admin panel.
- Go to the "Stores" tab in the main menu and select "Configuration".
- Under the "Sales" section in the left-hand menu, select "Shipping Methods".
- Scroll down to the "Store Pickup" section and click on it to expand the options.
- Find the "Enabled" option and set it to "Yes" to enable the "Store Pickup" shipping method.
- Set the "Title" option to "Store Pickup" or any other desired title for the shipping method.
- Set the "Ship to Applicable Countries" option to "Specific Countries" and select the countries where you want the "Store Pickup" option to be available.
- Set the "Show Method if Not Applicable" option to "No" to hide the "Store Pickup" option if it is not available in the selected countries.
- Set the "Sort Order" option to a lower number than other shipping methods to ensure that the "Store Pickup" option appears first.
- Save the configuration.