mshop_product instock column not updating, filtering by stock (display only products in stock)

Help for integrating the Laravel package
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by kdim95 » 18 Nov 2022, 09:58

Laravel framework version: 9.40.1
Aimeos Laravel version: * 2022.07.2
PHP Version: 8.1.12
Environment: Linux

Hello,

I am looking at the code in ai-client-html/src/Client/Html/Catalog/Lists/Standard.
I want to add a filter that only shows products that are in stock.

It looks like the filtering happens in the data() function.

My stock levels for products in the database are not updating when I add stock to the product.

I go to the product in the admin panel, add stock levels and the instock column inside the mshop_product table for the product does not update.

Am I doing something wrong here?

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

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by aimeos » 19 Nov 2022, 08:16

The product "instock" property is currently only updated by the CSV/XML importers but doesn't reflect changes you make in the admin backend or if customers buy products and decrease the stock level to zero. There are some difficulties updating the property on stock changes (performance, caching, etc.) which prevented to offer the full feature yet.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by kdim95 » 19 Nov 2022, 14:10

Can you please give me guidance on the most optimized way of adding a stock level filter in the data() here:
ai-client-html/src/Client/Html/Catalog/Lists/Standard

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

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by aimeos » 21 Nov 2022, 15:16

The "instock" property is currently the best way to show only products which are in stock but updating that property dynamically requires some more work. Thus, it's currently available for importers only.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by kdim95 » 21 Nov 2022, 15:30

I want to include the stock to every product and then filter it.

I have included it to the domains like so:

Code: Select all

'catalog' => [
	'lists' => [
		'domains' => [
			'attribute',
			'catalog',
			'catalog/stock',
			'catalog/lists',
			'media',
			'media/property',
			'price',
			'supplier',
			'text',
			'product',
			'stock',
		],
	]
]
The stock appears like this, I wonder why it does not have a prefix:
stock.PNG
stock.PNG (3.57 KiB) Viewed 2179 times
I can get the stock with $products->first()->get('.stock')

There are examples inside ai-client-html/src/Client/Html/Catalog/Lists/Standard about how to filter this data,
like $cntl->compare( '>', 'product.ratings', 0);.

But in this case the stock is inside an array, I also get this error if I attempt to filter by .stock:
Invalid name ".stock"

I was attempting a simple filter to check if the stock is not null:
$cntl->compare( '!=', '.stock', null);

My question is if there is any way to filter by the ".stock" or any other way to get the stock and filter it.

Or should I just see where the stock is updated in the administration and update the "product.instock" column myself?

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

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by aimeos » 22 Nov 2022, 14:37

You can't filter for the existence or not-existence of the stock items attached to the products because these are different data domains. Therefore, the "instock" attribute was added, which can be used for filtering but needs to be updated somehow.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

kdim95
Advanced
Posts: 194
Joined: 26 Aug 2022, 12:17

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by kdim95 » 23 Nov 2022, 14:27

I think I found an error here:
ai-admin-jqadm/src/Admin/JQAdm/Product/Stock

It looks to me that inside fromArray() there is an attempt to update the "instock" column, but using the wrong method.

I have created a pull request where it uses the correct method.
https://github.com/aimeos/ai-admin-jqadm/pull/244

Please check if this is correct.

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

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by aimeos » 23 Nov 2022, 15:14

Thank you very much!
Your PR is merged and available in dev-master and 2022.10.x-dev now.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

TGergo
Posts: 41
Joined: 24 Nov 2022, 14:35

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by TGergo » 08 Feb 2023, 15:27

/**
* Increases or decreses the stock levels of the products referenced in the order by the given value.
*
* @param \Aimeos\MShop\Order\Item\Iface $orderItem Order item object
* @param int $how Positive or negative integer number for increasing or decreasing the stock levels
* @return \Aimeos\Controller\Common\Order\Iface Order controller for fluent interface
*/
protected function updateStock( \Aimeos\MShop\Order\Item\Iface $orderItem, int $how = +1 )
{
$context = $this->context();
$stockManager = \Aimeos\MShop::create( $context, 'stock' );
$manager = \Aimeos\MShop::create( $context, 'order/base/product' );

$search = $manager->filter();
$search->setConditions( $search->compare( '==', 'order.base.product.baseid', $orderItem->getBaseId() ) );

$start = 0;

$stockManager->begin();

try
{
do
{
$items = $manager->search( $search );

foreach( $items as $item )
{

///////////////////

$productManager = \Aimeos\MShop::create( $this->context, 'product' );
$productItem = $productManager->get( $item->getProductId(), ['product' ,'stock'] );

if ( ( $productItem->getStockItems()->first()->getStockLevel() - ( -1 * $how * $item->getQuantity() ) ) == 0 ) {
$productItem->setInstock( false );
$productManager->save( $productItem, false );
}


///////////////////

$stockManager->decrease( [$item->getProductId() => -1 * $how * $item->getQuantity()], $item->getStockType() );

switch( $item->getType() ) {
case 'default':
$this->updateStockBundle( $item->getParentProductId(), $item->getStockType() ); break;
case 'select':
$this->updateStockSelection( $item->getParentProductId(), $item->getStockType() ); break;
}
}

$count = count( $items );
$start += $count;
$search->slice( $start );
}
while( $count >= $search->getLimit() );

$stockManager->commit();
}
catch( \Exception $e )
{
$stockManager->rollback();
throw $e;
}

return $this;
}

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

Re: mshop_product instock column not updating, filtering by stock (display only products in stock)

Post by aimeos » 10 Feb 2023, 08:13

The product instock column isn't updated in that method because there's no efficient way to do that. It's also not updated if you import stock levels from a CSV file. The column is meant for being updated either by the admin in the backend (done automatically depending on the stock level) or by a product importer (CSV or XML) importing products from an ERP system.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply