How to display custom media in basket view?

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: 124
Joined: 09 Oct 2019, 09:42

How to display custom media in basket view?

Post by columbo » 04 May 2023, 20:41

Hi,

we are using two different product media types ("default" and "custom").
On basket and checkout page the custom media should be displayed.

How can we select the custom media type here (\packages\myext\templates\client\html\common\summary\detail.php):

Code: Select all

<img src="<?= $this->content( $product->getMediaUrl() ) ?>" />

e.g. for favorite view (\packages\myext\templates\client\html\account\favorite\body.php) we used:

Code: Select all

$productItem->getRefItems('media', 'custom', 'default')
But this does not work for the basket view.

Thank you!

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

Re: How to display custom media in basket view?

Post by aimeos » 07 May 2023, 11:46

You need to overwrite the copyFrom() method in order product item:
https://github.com/aimeos/aimeos-core/b ... #L747-L773

In the next versions, you will able to be overwrite the code by using macros:

Code: Select all

\Aimeos\MShop\Order\Item\Product\Standard::macro( 'copyFrom', function( \Aimeos\MShop\Product\Item\Iface $product ) : \Aimeos\MShop\Order\Item\Base\Product\Iface {
	// ...
	return $this->setModified();
}
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: How to display custom media in basket view?

Post by columbo » 16 Nov 2023, 11:08

as the file has changed with the new version 2023.10 I think copyFrom() in file /aimeos-core/src/MShop/Order/Item/Product/Standard.php is the right one.

As suggested, I tried with macro in /app/ProvidersApp/ServiceProvider.php and changed the image form "default" to "custom"

Code: Select all

...
public function boot()
    {
        \Aimeos\MShop\Order\Item\Product\Standard::macro( 'copyFrom', function( \Aimeos\MShop\Product\Item\Iface $product ) : \Aimeos\MShop\Order\Item\Base\Product\Iface {
        
            $this->fromArray( $values );
            $this->setSiteId( $product->getSiteId() );
            $this->setProductCode( $product->getCode() );
            $this->setProductId( $product->getId() );
            $this->setType( $product->getType() );
            $this->setScale( $product->getScale() );
            $this->setTarget( $product->getTarget() );
            $this->setName( $product->getName() );

            if( $item = $product->getRefItems( 'text', 'basket', 'default' )->first() ) {
                $this->setDescription( $item->getContent() );
            }

            if( $item = $product->getRefItems( 'media', 'custom', 'default' )->first() ) {
                $this->setMediaUrl( $item->getPreview() );
            }

            if( $item = $product->getSiteItem() ) {
                $this->setVendor( $item->getLabel() );
            }

            return $this->setModified();
        });        
    } 
laravel.log after adding an item to basekt:

Code: Select all

ERROR: Call to undefined method App\Providers\AppServiceProvider::fromArray() {"userId":1,"exception":"[object] (Error(code: 0): Call to undefined method App\\Providers\\AppServiceProvider::fromArray() at /app/Providers/AppServiceProvider.php:32)
[stacktrace]
#0 /vendor/aimeos/aimeos-core/src/MShop/Order/Item/Product/Standard.php(766): App\\Providers\\AppServiceProvider->App\\Providers\\{closure}()
#1 /vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Basket/Standard.php(346): Aimeos\\MShop\\Order\\Item\\Product\\Standard->copyFrom()
#2 /vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Basket/Decorator/Category.php(59): Aimeos\\Controller\\Frontend\\Basket\\Standard->addProduct()
#3 /vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Basket/Decorator/Bundle.php(44): Aimeos\\Controller\\Frontend\\Basket\\Decorator\\Category->addProduct()
#4 /vendor/aimeos/ai-controller-frontend/src/Controller/Frontend/Basket/Decorator/Select.php(43): Aimeos\\Controller\\Frontend\\Basket\\Decorator\\Bundle->addProduct()
#5 /vendor/aimeos/ai-client-html/src/Client/Html/Basket/Standard/Standard.php(218): Aimeos\\Controller\\Frontend\\Basket\\Decorator\\Select->addProduct()
Thank you

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

Re: How to display custom media in basket view?

Post by aimeos » 17 Nov 2023, 16:11

Two mistakes:

1.) The function wasn't bound to the order product item object so $this referred to the AppServiceProvider where you've added the closure. This is now fixed and you need to execute:

Code: Select all

composer req aimeos/aimeos-core:2023.10.x-dev
2.) You are missing one line in your function:

Code: Select all

$values = $product->toArray();
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

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

Re: How to display custom media in basket view?

Post by columbo » 20 Nov 2023, 21:46

for the sake of completeness - there was a third mistake in return parameter:

wrong: \Aimeos\MShop\Order\Item\Base\Product\Iface
-> correct: \Aimeos\MShop\Order\Item\Product\Iface

this code it is working fine:

Code: Select all

public function boot()
    {
        \Aimeos\MShop\Order\Item\Product\Standard::macro( 'copyFrom', function( \Aimeos\MShop\Product\Item\Iface $product ) : \Aimeos\MShop\Order\Item\Product\Iface {                                                                                                                    
            
            $values = $product->toArray();
		    $this->fromArray( $values );

            $this->setSiteId( $product->getSiteId() );
            $this->setProductCode( $product->getCode() );
            $this->setProductId( $product->getId() );
            $this->setType( $product->getType() );
            $this->setScale( $product->getScale() );
            $this->setTarget( $product->getTarget() );
            $this->setName( $product->getName() );

            if( $item = $product->getRefItems( 'text', 'basket', 'default' )->first() ) {
                $this->setDescription( $item->getContent() );
            }

            if( $item = $product->getRefItems( 'media', 'custom', 'default' )->first() ) {
                $this->setMediaUrl( $item->getPreview() );
            }

            if( $item = $product->getSiteItem() ) {
                $this->setVendor( $item->getLabel() );
            }

            return $this->setModified();
        });
    } 
Thank you!

Post Reply