Skip to content

Commit

Permalink
Merge pull request #3315 from vahonc/3275-elasticsuite-getting-curren…
Browse files Browse the repository at this point in the history
…t-storeid-on-widget-2.10-fix

[Page Builder] Fix #3275, improved getting current storeId from widget collection
  • Loading branch information
romainruaud authored Jul 10, 2024
2 parents 9c0ac8b + ae72ea3 commit 7297a98
Showing 1 changed file with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\CatalogWidget\Block\Product\ProductsList;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Widget\Helper\Conditions;
use Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Fulltext\Collection;
Expand Down Expand Up @@ -100,7 +101,10 @@ public function __construct(
*/
public function beforeCreateCollection(ProductsList $subject)
{
$storeId = $this->storeManager->getStore()->getId();
// Get the current store ID.
$storeId = $this->getCurrentStoreId($subject);

// Set the store ID back to the widget.
$subject->setData('store_id', $storeId);

return [];
Expand All @@ -120,7 +124,7 @@ public function beforeCreateCollection(ProductsList $subject)
*/
public function afterCreateCollection(ProductsList $subject, $collection)
{
$storeId = $this->storeManager->getStore()->getId();
$storeId = $this->getCurrentStoreId($subject);
$sortOption = $subject->getData('sort_order');
$conditionOption = $subject->getData('condition_option');

Expand Down Expand Up @@ -181,4 +185,24 @@ public function afterCreateCollection(ProductsList $subject, $collection)

return $collection;
}

/**
* Get the current store ID from the widget's data or default store.
*
* @param ProductsList $subject Widget product list.
* @return int
* @throws NoSuchEntityException
*/
private function getCurrentStoreId(ProductsList $subject)
{
// Get the store ID from the widget's data.
$storeId = (int) $subject->getData('store_id');

// If the store ID is not specified (0), use the default store ID from the store manager.
if ($storeId === Store::DEFAULT_STORE_ID) {
$storeId = $this->storeManager->getStore()->getId();
}

return $storeId;
}
}

0 comments on commit 7297a98

Please sign in to comment.