ProductStock plugins

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!
traiyani75
Advanced
Posts: 114
Joined: 08 Nov 2019, 11:56

Re: ProductStock plugins

Post by traiyani75 » 10 Aug 2020, 19:15

Hello,

I added dd on the following methods and wanted to check if they are getting invoked or not. It seems that the methods are not getting invoked when I call the following api i.e http://ebdaa-ecommerce.test/syaanh/json ... elatedid=0

This is from the ProductStock -:
/**
* Receives a notification from a publisher object
*
* @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket instance implementing publisher interface
* @param string $action Name of the action to listen for
* @param mixed $value Object or value changed in publisher
* @return mixed Modified value parameter
* @throws \Aimeos\MShop\Plugin\Provider\Exception if checks fail
*/
public function update( \Aimeos\MW\Observer\Publisher\Iface $order, string $action, $value = null )
{
dd('update debug');
if( ( $value & \Aimeos\MShop\Order\Item\Base\Base::PARTS_PRODUCT ) === 0 ) {
return $value;
}
\Aimeos\MW\Common\Base::checkClass( \Aimeos\MShop\Order\Item\Base\Iface::class, $order );
if( ( $outOfStock = $this->checkStock( $order ) ) !== [] )
{
$msg = $this->getContext()->getI18n()->dt( 'mshop', 'Products out of stock' );
throw new \Aimeos\MShop\Plugin\Provider\Exception( $msg, -1, null, array( 'product' => $outOfStock ) );
}
return $value;
}
/**
* Checks if all products in the basket have enough stock
*
* @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket object
* @return array Associative list of basket product positions as keys and the error codes as values
*/
protected function checkStock( \Aimeos\MShop\Order\Item\Base\Iface $order ) : array
{
dd('checkStock debug');
$productCodes = $stockTypes = $stockMap = [];
foreach( $order->getProducts() as $orderProduct )
{
$productCodes[] = $orderProduct->getProductCode();
$stockTypes[] = $orderProduct->getStockType();
}
foreach( $this->getStockItems( $productCodes, $stockTypes ) as $stockItem ) {
$stockMap[$stockItem->getProductCode()][$stockItem->getType()] = $stockItem;
}
return $this->checkStockLevels( $order, $stockMap );
}
/**
* Checks if the products in the basket have enough stock
*
* Removes products from the basket which are out of stock and decreases the
* quantities of orders products if there's not enough stock.
*
* @param \Aimeos\MW\Observer\Publisher\Iface $order Shop basket object
* @param array $stockMap Multi-dimensional associative list of product ID / stock type as keys and stock level as values
* @return array Associative list of basket positions as keys and error codes as values
*/
protected function checkStockLevels( \Aimeos\MShop\Order\Item\Base\Iface $order, array $stockMap ) : array
{
dd('checkStockLevels debug');
$outOfStock = [];
$products = $order->getProducts();
foreach( $products as $pos => $orderProduct )
{
$stocklevel = 0;
$type = $orderProduct->getStockType();
$code = $orderProduct->getProductCode();
if( isset( $stockMap[$code] ) && array_key_exists( $type, $stockMap[$code] ) )
{
$orderProduct->setTimeFrame( $stockMap[$code][$type]->getTimeFrame() );
if( ( $stocklevel = $stockMap[$code][$type]->getStockLevel() ) === null ) {
continue;
}
if( $stocklevel >= $orderProduct->getQuantity() )
{
$stock = $stockMap[$code][$type]->getStockLevel() - $orderProduct->getQuantity();
$stockMap[$code][$type]->setStockLevel( $stock );
continue;
}
}
if( $stocklevel > 0 ) { // update quantity to actual stock level
$order->addProduct( $orderProduct->setQuantity( $stocklevel ), $pos );
} else {
$order->deleteProduct( $pos );
}
$outOfStock[$pos] = 'stock.notenough';
}
return $outOfStock;
}
My config/shop.php
'jsonapi' => [
'product' => [
'levels' => 3
],
'debug' => 1,
'order' => [
'name' => 'EbdaaClientStandard'
],
'basket' => [
'product' => [
'name' => 'EbdaaBasketProductStandard'
]
],
],
],
Thanks

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

Re: ProductStock plugins

Post by aimeos » 11 Aug 2020, 17:59

The plugin was not executed because it only listened to "check.after" but not for "addProduct.after". We've fixed that in the master and the latest 2020.07.x-dev branch.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply