add from order-history with deactivated products -> error

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!
columbo
Advanced
Posts: 125
Joined: 09 Oct 2019, 09:42

add from order-history with deactivated products -> error

Post by columbo » 08 Dec 2023, 12:54

Hi,

all products are listed in the order history as available, even if they have been deactivated in the meantime.
  1. customer adds a "history-basket" to his current basket
  2. history-basket contains at least one deactivated product
  3. -> Error Eintrag mit ID "1234" in "product.id" nicht gefunden {"userId":1,"exception":"[object] (Aimeos\\MShop\\Exception(code: 404): Eintrag mit ID \"1234\" in \"product.id\" nicht gefunden at \\vendor\\aimeos\\aimeos-core\\src\\MShop\\Common\\Manager\\DB.php:635)
Plugin "Checks for deleted products" (ProductGone) is enabled

I tried to get the product.status in account history to
  • exclude deactivated products from being added to customers basket
  • highlight products that are currently not available / deactivated
but I have not found a working solution

What is your recommendation and the most efficient solution?
Thank you.

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

Re: add from order-history with deactivated products -> error

Post by aimeos » 10 Dec 2023, 11:07

The ProductGone plugin is only used if the product is already in the basket but removed before the checkout is completed.
The best option might be to catch the product exceptions here and add an error message to the basket view that at least one product couldn't be added to the basket:
https://github.com/aimeos/ai-client-htm ... #L192-L229

Are you able to provide a PR for the change?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

columbo
Advanced
Posts: 125
Joined: 09 Oct 2019, 09:42

Re: add from order-history with deactivated products -> error

Post by columbo » 11 Dec 2023, 17:16

tried to create a PR but not sure if done right.

addProducts() function with try/catch:

Code: Select all

protected function addProducts( \Aimeos\Base\View\Iface $view )
	{
		$context = $this->context();
		$domains = ['attribute', 'catalog', 'media', 'price', 'product', 'text', 'locale/site'];

		$basketCntl = \Aimeos\Controller\Frontend::create( $context, 'basket' );
		$productCntl = \Aimeos\Controller\Frontend::create( $context, 'product' )->uses( $domains );

		if( ( $prodid = $view->param( 'b_prodid', '' ) ) !== '' && $view->param( 'b_quantity', 0 ) > 0 )
		{
			try
			{
				$basketCntl->addProduct(
					$productCntl->get( $prodid ),
					(float) $view->param( 'b_quantity', 0 ),
					(array) $view->param( 'b_attrvarid', [] ),
					$this->getAttributeMap( $view->param( 'b_attrconfid', [] ) ),
					array_filter( (array) $view->param( 'b_attrcustid', [] ) ),
					(string) $view->param( 'b_stocktype', 'default' ),
					$view->param( 'b_siteid' )
				);
			}
			catch( \Exception $e )
			{
				//eg. product has been deactivated
				$msg = $view->translate( 'client', "At least one product couldn't be added to the basket." );
				$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
			}
		}
		else
		{
			foreach( (array) $view->param( 'b_prod', [] ) as $values )
			{
				if( ( $values['prodid'] ?? null ) && ( $values['quantity'] ?? 0 ) > 0 )
				{
					try
					{
						$basketCntl->addProduct( $productCntl->get( $values['prodid'] ),
							(float) ( $values['quantity'] ?? 0 ),
							array_filter( (array) ( $values['attrvarid'] ?? [] ) ),
							$this->getAttributeMap( (array) ( $values['attrconfid'] ?? [] ) ),
							array_filter( (array) ( $values['attrcustid'] ?? [] ) ),
							(string) ( $values['stocktype'] ?? 'default' ),
							$values['siteid'] ?? null
						);
					}
					catch( \Exception $e )
					{
						//eg. product has been deactivated
						$msg = $view->translate( 'client', "At least one product couldn't be added to the basket." );
						$view->errors = array_merge( $view->get( 'errors', [] ), [$msg] );
					}
				}
			}
		}

		$this->clearCached();
	}
Are there any options to highlight products that are currently not available / deactivated?
eg. set a "currently not availabe" badge or change to productname-color

thank you

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

Re: add from order-history with deactivated products -> error

Post by aimeos » 12 Dec 2023, 12:41

Thanks! Highlighting products upfront is only possible if the original products are loaded too in the history view. It's possible to add "product" to the list of fetched domains here:
https://github.com/aimeos/ai-client-htm ... hp#L70-L83
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply