I'm trying to show a product list in a block with this condition:
show only products that currently are in "special price"!
Can I do it programmatically? how to filter the product list?
For example, maybe I need to read two product information: the "original price" and the "final price" and if they differ then show the product!?.. but I'm newbe in magento 2 and I don't know how to made it.
Thanks in advance
yes but how can I do it via widget?
a product list? but I didn't understand how to filter records
I didn't tried but maybe you can check this idea:
Edit your CMS page:
And insert a new widget.
Now you'll see a widget selection page.
Select the "Catalog Product List" widget.
These will be the options for this widget:
At the end you can set conditions. There you'll be able to set products with special price grather than or another types of rules.
Maybe you can use this feature to avoid the need of coding a new/custom widget type.
thank you, I had looked at this possibility .. In fact my problem is how to set the rule:
what kind of condition can I set?
there are "placeholder / global variables" that can be inserted in the rule?
I'd like to use this rule (but I do not know if possible):
"Special price" is less than "regular price" (and obviously different from zero)
Maybe (as I said I didn't tried) you can set "where special price is grather than 1".
This should get allproducts with special price greater than value. If a product hasn't special price shouldn't be included (i guess).
Can you try that idea?
We had a similar request recently. You can create the product collection as follows:
/** @var ProductCollection $productCollection */ $productCollection = $this->productCollectionFactory->create(); $productCollection ->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds()) ->addMinimalPrice() ->addFinalPrice() ->addTaxPercents() ->addAttributeToSelect($this->catalogConfig->getProductAttributes()) ->addUrlRewrite(); ->addStoreFilter() ->addAttributeToSort( 'minimal_price', 'asc' ); $productCollection->getSelect()->where( 'price_index.final_price < price_index.price' );
If you want to support prices for children products, i.e. of grouped products, more work may be necessary. With this answer, I assume you know how to create a Block and use a Factory.
Using the code from @avstudnitz you can try to create a custom widget.
Here you'll find a lof of examples about cusotm widgets:
Also, I have a sample/test module that contains the base code for widgets too: https://github.com/barbanet/magento2-samplemodule
Hi Damian.. " special price is grather than 1" it works.
It seems that solutions doesn't work with "Marketing\Catalog Price Rule" active but just if I edit a product and insert a special price.
Unfortunatly, because this way I have to edit edit every single product (with a rule I have not)
Thanks avstudnitz, your code works (like a condition "special price greater than 1" using a widget).
Does not work with a "catalog price rule" active. I can't understand this behavior in magento:
I can see products that correspond to the rule with a discounted price but in $productCollection they have the "price" and the "final_price" with the same value!
but it's not true! cause the price showed on the screen, resulting from the application of the rule, it's lower than regular price.
Is it possible that the collection also reads the discounted prices falling in a "catalog price rule"?