Upload media and attach to product from custom form

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
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Upload media and attach to product from custom form

Post by GagDalakyan » 26 Mar 2021, 14:49

Good day !
I have created custom pages for specific users. They can create products with attributes, prices, categories from that page. I used aimeos managers, everything works fine.
But i have an issue with media upload. This is my code.

Code: Select all

$mediaFile = $request->file('main-image');
$mediaManager = \Aimeos\MShop::create( $context, 'media' );

$mediaItem = $mediaManager->createItem();
$mediaItem->setDomain('product');
$mediaItem->setUrl($mediaFile);
$mediaItem->setPreview($mediaFile);
$mediaItem->setStatus('1');
$mediaItem->setType('default');
$mediaItem->setMimeType($mediaFile->getMimeType());
            
$mediaManager->saveItem($mediaItem, true);
How can i create previews, with sizes 240, 720, 800px, such as aimeos medias in database.
And generate media link same as aimeos do this.

Many thanks!

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

Re: Upload media and attach to product from custom form

Post by aimeos » 28 Mar 2021, 08:04

Use the media controller for that:
https://github.com/aimeos/aimeos-core/b ... /Iface.php

Here you can see how it's used by the Aimeos admin backend:
https://github.com/aimeos/ai-admin-jqad ... #L380-L384
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Upload media and attach to product from custom form

Post by GagDalakyan » 28 Mar 2021, 15:36

Many thanks for your reply !
I shall try it.

Now i have another problem.
I want to get products which have stock greater than zero.
This is my code.

Code: Select all

$products = \Aimeos\Controller\Frontend::create( $context, 'product' )
		->sort( $sort )
		->category( $catids, 'default', $level )
		->allOf( $view->param( 'f_attrid', [] ) )
		->text( $view->param( 'f_search' ) )
		->uses( $domains );
		
		if( (isset($_GET['f_stock']) && $_GET['f_stock'] == '1')) {
			$products = $products->isInStock();
		}
				
		$products = $products->search( $total );
And this is isInStock method.

Code: Select all

	public function isInStock() : Iface
	{
		$hasfcn = $this->filter->createFunction( 'product:has', ['stock'] );
		$this->conditions['is-on-sale'] = $this->filter->compare( '>=', $hasfcn, '1' );
		return $this;
	}
I tried everything )) Please help me if it is possible to do.

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

Re: Upload media and attach to product from custom form

Post by aimeos » 31 Mar 2021, 12:39

There's no easy way to filter products by stock because there's no direct relation. Stock levels are loaded in by an async AJAX call because they are highly volatile. There have been discussed strategies using an "in-stock" attribute but you would have to implement the whole handling yourself.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

User avatar
GagDalakyan
Posts: 17
Joined: 03 Dec 2020, 13:13

Re: Upload media and attach to product from custom form

Post by GagDalakyan » 01 Apr 2021, 08:16

I did it :)) May be someone else need this. Here is example

This is product builder

Code: Select all

if( (isset($_GET['f_stock']) && $_GET['f_stock'] == '1')) {
	$products = $products->isInStock();
}

public function isInStock() : Iface
{
	$func = $this->filter->createFunction( 'product:stock', [$this->getContext()->getLocale()->getCurrencyId()] );
	$this->conditions['is-in-stock'] = $this->filter->compare( '>', $func, 0 );

	return $this;
}

'product:stock' => array(
	'code' => 'product:stock()',
	'internalcode' => 'msto."stocklevel"',
	'internaldeps' => ['INNER JOIN "mshop_stock" AS msto ON ( msto."productcode" = mpro."code" )'],
	'label' => 'Product stock, parameter(<currency ID>)',
	'type' => 'integer',
	'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
	'public' => true,
),

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

Re: Upload media and attach to product from custom form

Post by aimeos » 02 Apr 2021, 17:03

Be aware that joining the stock table for products is painfully slow if you have more than a few hundered products!
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply