tuyennn / magento2-outofstockatlast

Magento 2.4.x module Sort Out Of Stock Product At last the product list
GNU General Public License v3.0
55 stars 12 forks source link

order of Widget new products is broken #50

Closed ppeyman closed 2 months ago

ppeyman commented 10 months ago

module

Preconditions

I installed it on magento 2.4.6-p3 with elasticsearch version 7.17.7.

reproduce

Everything Works Well But when i add a new Products Widget to Home page or etc sort of new Products Broken and show old or random products

Expected Result

image

Actual result

image

as you see old products show after enable this module

Thank you !

tuyennn commented 10 months ago

Thanks @ppeyman for the report, as I know the new products widget used news_from_date as default sorting instead custom sorting out of stock attribute. Please see below for reference https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/Catalog/Block/Product/Widget/NewWidget.php#L86-L118 https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/Catalog/Block/Product/NewProduct.php#L147-L150

Further more we support the Product List only, there was no plans for widget yet.

ppeyman commented 10 months ago

Thanks @ppeyman for the report, as I know the new products widget used news_from_date as default sorting instead custom sorting out of stock attribute. Please see below for reference https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/Catalog/Block/Product/Widget/NewWidget.php#L86-L118 https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/Catalog/Block/Product/NewProduct.php#L147-L150

Further more we support the Product List only, there was no plans for widget yet.

so that mean i disable new products in our site ?

tuyennn commented 10 months ago

@ppeyman not sure, but for sure our module took no effect to the New Products widget and the widget works as Out of the box Magento

ppeyman commented 10 months ago

Owner

so why when i disable your module problem of widget solved ?

tuyennn commented 10 months ago

Here what I archived before and after enabling/disabling module, reindexing Before Selection_157

After Selection_158

See https://github.com/magento/magento2/blob/2.4.6/app/code/Magento/Catalog/Block/Product/Widget/NewWidget.php#L86-L118

I supposed the logic as your screenshot came to Magento/Catalog/Block/Product/Widget/NewWidget.php::_getRecentlyAddedProductsCollection Where the order was sorting by created_at desc

Are you able to double check the logic?

ppeyman commented 10 months ago

my logic :

  protected function _getProductCollection()
    {
        switch ($this->getDisplayType()) {
            case self::DISPLAY_TYPE_NEW_PRODUCTS:
                $collection = parent::_getProductCollection()
                    ->setPageSize($this->getPageSize())
                    ->setCurPage($this->getCurrentPage());
                break;
            default:
                $collection = $this->_getRecentlyAddedProductsCollection();
                break;
        }
        return $collection;
    }

    /**
     * Prepare collection for recent product list
     *
     * @return \Magento\Catalog\Model\ResourceModel\Product\Collection|Object|\Magento\Framework\Data\Collection
     */
    protected function _getRecentlyAddedProductsCollection()
    {
        /** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
        $collection = $this->_productCollectionFactory->create();
        $collection->setVisibility($this->_catalogProductVisibility->getVisibleInCatalogIds());

        $collection = $this->_addProductAttributesAndPrices($collection)
            ->addStoreFilter()
            ->addAttributeToSort('created_at', 'desc')
            ->setPageSize($this->getPageSize())
            ->setCurPage($this->getCurrentPage());
        return $collection;
    }
ppeyman commented 10 months ago

@tuyennn Hi dear i test it on new fresh magento installation and add four old products and add four new products when your module is enabled doesn`t show new products and showing another products please check it like me and see results

in adminpanel shows well and true :

image

in frontend show false :

image

thank you