Show cheapest variant price as selection product's price

How to configure and adapt Aimeos based shops as developer
Forum rules
Always add your Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Show cheapest variant price as selection product's price

Post by niloofar » 11 Sep 2021, 11:51

Hi.

I would like to change the logic behind the price shown for a select product.

On this page, the price of the Demo selection article must get changed.
The price must be the cheapest price, among all the variant products. For instance, by opening the product's page you can see there are two sub-products. One is colored blue, and another is beige. If the blue one's price is 100$ and the beige's price is 120$, on the home page, lists page, suggested product list, etc. 100$ must be shown as the Demo selection article's price.

I considered adding a new method to the product's controller, but how can I call a controller in

Code: Select all

common/partials/products-standard
?

PS-
In config I have set:

Code: Select all

mshop/common/manager/maxdepth = 3
There are multiple sites in my shop, so each sub-product may have multiple sellers. Some of the prices are useless if the stock item for the corresponding seller is 0.
I have to filter through the price items based on the stock level of the seller, and also remove those sellers that are not enabled (since I use the 'ai-sites' extension).

So the question is how to call a method for getting the cheapest valid price in 'common/partials/products-standard'
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7893
Joined: 01 Jan 1970, 00:00

Re: Show cheapest variant price as selection product's price

Post by aimeos » 12 Sep 2021, 17:04

If you've passed

Code: Select all

['product', 'price', 'text', 'stock', ...]
as domains that are fetched along with the products shown in the list view, you can do the following to get the cheapest price of all sub-products for one product item:

Code: Select all

$prices = $product->getRefItems( 'product', 'default', 'default' ) // get all variant articles
	->filter( function( $item, $id ) {  // filter products with stock
		return $item->getStockItems()->getStockLevel()
			->filter( function( $stock) { // filter stock levels > 0 and unlimited
				return $stock=== null || $stock > 0;
			} );
	} )->getRefItems( 'price', 'default', 'default' ); // get all variant price items

$price = \Aimeos\MShop::create( $context, 'price' )->getLowestPrice( $prices, 1 );
Please keep in mind that this isn't the most performant way because you have to load a lot of data and that sorting by price won't yield the correct order. If you are able to add the cheapest price to the selection product when saving the variant articles (e.g. in the admin backend), this will be much faster and requires less data loaded from the database.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Show cheapest variant price as selection product's price

Post by niloofar » 09 Nov 2021, 14:29

Hi

The solution mentioned here works fine for me.
However, I have an issue in getting price items on the home page.

Is there a way to change the domains here somehow, so that promoted products are returned with their referenced subProducts? And subProducts
must have their prices with them as reference items.
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7893
Joined: 01 Jan 1970, 00:00

Re: Show cheapest variant price as selection product's price

Post by aimeos » 10 Nov 2021, 07:48

You've already found the configuration options for changing the used domains:
https://github.com/aimeos/ai-client-htm ... #L439-L440
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Show cheapest variant price as selection product's price

Post by niloofar » 10 Nov 2021, 13:15

That's right.
I want to know how to change the Value of config so that all promoted products are retrieved with their stock, price, and subproducts items.

maybe something like:

Code: Select all

['media', 'media/property', 'price', 'text', 'product' => ['promotion' => ['stock', 'product', ...]]
Right now the promotion products only have text and media as their reference items.
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7893
Joined: 01 Jan 1970, 00:00

Re: Show cheapest variant price as selection product's price

Post by aimeos » 11 Nov 2021, 07:00

Use:

Code: Select all

['media', 'media/property', 'price', 'text', 'product' => ['promotion', 'default'], 'stock']
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
niloofar
Posts: 16
Joined: 04 Apr 2021, 09:41

Re: Show cheapest variant price as selection product's price

Post by niloofar » 17 Nov 2021, 08:33

Stock items are not added to list items by adding `stock` to this array. Interesting is, that by deleting `media` from this array the media items won't be fetched. I even tested this with a fresh installation of the Aimeos project.

Are there any other files that I should check?
Aimeos version: ~2021.07
Laravel version: ^8.40
PHP: 7.4.9

User avatar
aimeos
Administrator
Posts: 7893
Joined: 01 Jan 1970, 00:00

Re: Show cheapest variant price as selection product's price

Post by aimeos » 19 Nov 2021, 09:51

Stock items are available by using:

Code: Select all

$product->getStockItems()
They are not linked via the mshop_product_list table
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply