0

I am using Magento 2.2.5. In that, I am creating the products using the below code.

$product = $this->objectManager->create ( 'Magento\Catalog\Model\Product' )->load ( $productId );
$product->setName ( $name );
$product->setDescription ( $description );
$product->setPrice ( $price );
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$id = null;
$manager = $this->objectManager->get ( 'Magento\Store\Model\StoreManagerInterface' );
$store = (int) $this->getRequest()->getParam('store', 0);//$manager->getStore ( $id );
$websiteId = $store->getWebsiteId ();
$product->setWebsiteIds ( array ( $websiteId) );
$product->setStockData ( array ('use_config_manage_stock' => 0,'is_in_stock' => 1,'manage_stock' => 0,'use_config_notify_stock_qty' => 0 ) );
$product->save ();

It is saving. When I loading with Block using following code:

 protected function _construct() {
    parent::_construct ();
    $collection = $this->getFilterProducts ();
    $this->setCollection ( $collection );
}
public function getFilterProducts() {        
    /**
     * Filter by product attributes
     */
    $product = $this->productFactory->addAttributeToSelect ( '*' )->addAttributeToFilter ( 'user_id', $customerSession->getCustomer()->getId () );
    $product->addAttributeToFilter ( 'visibility', array (
            'eq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH
    ) );
    $product->addAttributeToSort ( 'entity_id', 'DESC' );

    /**
     * Return product object
     */
    return $product;
}
protected function _prepareLayout() {
    parent::_prepareLayout ();
    /**
     *
     * @var \Magento\Theme\Block\Html\Pager
     */
    $pager = $this->getLayout ()->createBlock ( 'Magento\Theme\Block\Html\Pager', 'mymodule.product.list.pager' );
    $pager->setAvailableLimit(array(
            10 => 10,
            20 => 20,
            50 => 50
        ))->setShowAmounts ( false )->setCollection ($this->getCollection ());
    $this->setChild ( 'pager', $pager );
    $this->getCollection () ->load();
    return $this;
}

public function getPagerHtml() {
    return $this->getChildHtml ( 'pager' );
}

In this block file I am trying to print the product collection, It has all the products with the user_id(custom attribute). But When I am printing the collection in the phtml file as $this->getCollection() it has the filter with stock data. But I am not including this stock filter anywhere. Is this Default or How I can get the product collection without Stock data. Please help me to get the product collection.

1 Answer 1

1

You can remove stock filter overriding 2 classes from catalog inventory module:

  <preference for="Magento\CatalogInventory\Model\ResourceModel\Stock\Status" type="Namespace\Catalog_Inventory\Model\ResourceModel\Stock\Status" />
<preference for="Magento\CatalogInventory\Helper\Stock" type="NameSpace\Catalog_Inventory\Helper\Stock" />

Namespace\Catalog_Inventory\Helper\Stock\Status file like below:

                <?php

          namespace Namespace\Catalog_Inventory\Model\ResourceModel\Stock;

       use Magento\CatalogInventory\Model\Stock;
     use Magento\CatalogInventory\Api\StockConfigurationInterface;
     use Magento\Framework\App\ObjectManager;

     class Status extends \Magento\CatalogInventory\Model\ResourceModel\Stock\Status
        {
/**
 * Store model manager
 *
 * @var \Magento\Store\Model\StoreManagerInterface
 * @deprecated 100.1.0
 */
protected $_storeManager;

/**
 * Website model factory
 *
 * @var \Magento\Store\Model\WebsiteFactory
 */
protected $_websiteFactory;

/**
 * @var \Magento\Eav\Model\Config
 */
protected $eavConfig;

/**
 * @var StockConfigurationInterface
 */
protected  $stockConfiguration;

/**
 * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
 * @param \Magento\Store\Model\StoreManagerInterface $storeManager
 * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
 * @param \Magento\Eav\Model\Config $eavConfig
 * @param string $connectionName
 */
public function __construct(
    \Magento\Framework\Model\ResourceModel\Db\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Store\Model\WebsiteFactory $websiteFactory,
    \Magento\Eav\Model\Config $eavConfig,
     $connectionName = null

) {
    parent::__construct($context,$storeManager,$websiteFactory,$eavConfig,$connectionName);

    $this->_storeManager = $storeManager;
    $this->_websiteFactory = $websiteFactory;
    $this->eavConfig = $eavConfig;
    $this->scopeConfig = $scopeConfig;
}

public function addStockDataToCollection($collection, $isFilterInStock)
{
    $manageStock=$this->scopeConfig->getValue('cataloginventory/item_options/manage_stock', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    if($manageStock!=0)
    {
        $websiteId = $this->getStockConfiguration()->getDefaultScopeId();
        $joinCondition = $this->getConnection()->quoteInto(
            'e.entity_id = stock_status_index.product_id' . ' AND stock_status_index.website_id = ?',
            $websiteId
        );

        $joinCondition .= $this->getConnection()->quoteInto(
            ' AND stock_status_index.stock_id = ?',
            Stock::DEFAULT_STOCK_ID
        );
        $method = $isFilterInStock ? 'join' : 'joinLeft';
        $collection->getSelect()->$method(
            ['stock_status_index' => $this->getMainTable()],
            $joinCondition,
            ['is_salable' => 'stock_status']
        );

        if ($isFilterInStock) {
            $collection->getSelect()->where(
                'stock_status_index.stock_status = ?',
                Stock\Status::STATUS_IN_STOCK
            );
        }
}
    return $collection;
}

/**
 * @return StockConfigurationInterface
 *
 * @deprecated 100.1.0
 */
protected function getStockConfiguration()
{
    if ($this->stockConfiguration === null) {
        $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
    }
    return $this->stockConfiguration;
}

}

and NameSpace\Catalog_Inventory\Helper\Stock:

<?php

 namespace Tatva\Catalog_Inventory\Helper;

 use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\ResourceModel\Collection\AbstractCollection;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status;
use Magento\CatalogInventory\Model\ResourceModel\Stock\StatusFactory;
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\StoreManagerInterface;


class Stock extends \Magento\CatalogInventory\Helper\Stock

{ /** * Store model manager * * @var StoreManagerInterface */ protected $storeManager;

/**
 * Core store config
 *
 * @var ScopeConfigInterface
 */
protected $scopeConfig;

/**
 * @var Status
 */
protected $stockStatusResource;

/**
 * @var StatusFactory
 */
protected $stockStatusFactory;

/**
 * @var StockRegistryProviderInterface
 */
private $stockRegistryProvider;


private $stockConfiguration;


public function __construct(
    StoreManagerInterface $storeManager,
    ScopeConfigInterface $scopeConfig,
    StatusFactory $stockStatusFactory,
    StockRegistryProviderInterface $stockRegistryProvider
) {
    $this->storeManager = $storeManager;
    $this->scopeConfig = $scopeConfig;
    $this->stockStatusFactory  = $stockStatusFactory;
    $this->stockRegistryProvider = $stockRegistryProvider;
    return parent::__construct($storeManager,$scopeConfig,$stockStatusFactory,$stockRegistryProvider);
}

/**
 * Assign stock status information to product
 *
 * @param Product $product
 * @param int $status
 * @return void
 */
public function assignStatusToProduct(Product $product, $status = null)
{
    if ($status === null) {
        $scopeId = $this->getStockConfiguration()->getDefaultScopeId();
        $stockStatus = $this->stockRegistryProvider->getStockStatus($product->getId(), $scopeId);
        $status = $stockStatus->getStockStatus();
    }
    $product->setIsSalable(true);
}
  protected function getStockConfiguration()
{
    if ($this->stockConfiguration === null) {
        $this->stockConfiguration = \Magento\Framework\App\ObjectManager::getInstance()
            ->get(\Magento\CatalogInventory\Api\StockConfigurationInterface::class);
    }
    return $this->stockConfiguration;
}

}

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.