0

My issue is that I can't get my different frontends to show different "Display out of Stock Products" options.
Does anyone have any tips on which stock Magento module I need to look into to build a plugin or extension off of?

I have a Magento 2.4.6 site setup like this:

  • Customer Group defines store view
  • Shared inventory added to both websites through backend
  • Website View 1 (default) - Logged in and not logged in customers
    Should show only in stock items in frontend store view.
  • Website View 2 - Logged in customers that have been changed to customer group 2
    Should show in stock and out of stock items in the frontend store view

Frontend view is defined by customer group
This is working correctly using this extension:
https://stackoverflow.com/questions/60206046/change-store-view-automatically-for-different-customer-groups-in-magento-2

I've changed the Scope of the "Display out of Stock Products" from global to website based scopes using this extension:
https://community.magento.com/t5/Magento-2-x-Programming/Change-scope-of-Display-Out-of-Stock-Products-from-global-to/td-p/452846

I am able to apply the "Display out of Stock Products" dropdown selector to either website view in the backend but "Website View 2" is still dependent on the selection of the default "Website View 1" and doesn't change.

My question as stated above: Does anyone have any tips on which stock Magento module I need to look into to build a plugin or extension off of?

I had thought maybe the Magento / CatalogInventory / Helper / Stock.php function below might be right since it's doesn't specify the $store or $website scope after the ScopeInterface::SCOPE_STORE

 public function addIsInStockFilterToCollection($collection)
{
    $stockFlag = 'has_stock_status_filter';
    if (!$collection->hasFlag($stockFlag)) {
        $isShowOutOfStock = $this->scopeConfig->getValue(
            \Magento\CatalogInventory\Model\Configuration::XML_PATH_SHOW_OUT_OF_STOCK,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
        $resource = $this->getStockStatusResource();
        $resource->addStockDataToCollection(
            $collection,
            !$isShowOutOfStock
        );
        $collection->setFlag($stockFlag, true);
    }
}

or the Magento / CatalogInventory / Model /Configuration.php but this file does list the $store ScopeInterface::SCOPE_STORE, $store:

    /**
 * Display out of stock products option
 *
 * @param null|string|bool|int|\Magento\Store\Model\Store $store
 * @return bool
 */
public function isShowOutOfStock($store = null)
{
    return $this->scopeConfig->isSetFlag(
        self::XML_PATH_SHOW_OUT_OF_STOCK,
        ScopeInterface::SCOPE_STORE,
        $store
    );
}

Any pointers, tips or corrections are much appreciated! Thanks!

1 Answer 1

0

I think this is solved for now. Looking more at the Magento / CatalogInventory / Helper / Stock.php you can see this function which pulls the default scope ID and doesn't take into account the Website or store Scope:

    public function addStockStatusToProducts(AbstractCollection $productCollection)
{
    $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
    foreach ($productCollection as $product) {
        $productId = $product->getId();
        $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $scopeId);
        $status = $stockStatus->getStockStatus();
        $product->setIsSalable($status);
    }
}

Just grabbing the code from the following function which looks for the scope ID of the Store (I believe the website value would reflect the store choice in this case based on the hiearchy) :

getValue(
        \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );

And adding that code into the addStockStatusToProducts function:

 public function addStockStatusToProducts(AbstractCollection $productCollection)
{
    $scopeId = $this->getStockConfiguration()->getValue(
        \Magento\CatalogInventory\Model\Configuration::XML_PATH_MANAGE_STOCK,
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE
    );
    foreach ($productCollection as $product) {
        $productId = $product->getId();
        $stockStatus = $this->stockRegistryProvider->getStockStatus($productId, $scopeId);
        $status = $stockStatus->getStockStatus();
        $product->setIsSalable($status);
    }
}

My site is now pulling the correct "Display out of Stock Products" values on each of the two store fronts. Now I need to make a plugin for this so my code changes aren't in the core files.

If anyone has corrections or any suggestions they are always appreciated! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.