mshop_product instock column not updating, filtering by stock (display only products in stock)
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!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
mshop_product instock column not updating, filtering by stock (display only products in stock)
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?
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?
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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, give us a star
If you like Aimeos, give us a star
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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
ai-client-html/src/Client/Html/Catalog/Lists/Standard
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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, give us a star
If you like Aimeos, give us a star
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
I want to include the stock to every product and then filter it.
I have included it to the domains like so:
The stock appears like this, I wonder why it does not have a prefix:
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?
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',
],
]
]
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?
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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, give us a star
If you like Aimeos, give us a star
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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.
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.
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
Thank you very much!
Your PR is merged and available in dev-master and 2022.10.x-dev now.
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, give us a star
If you like Aimeos, give us a star
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
/**
* 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;
}
* 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;
}
Re: mshop_product instock column not updating, filtering by stock (display only products in stock)
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, give us a star
If you like Aimeos, give us a star