Intercepting Frontend Product Controller with Product Decorator

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!
BrianMecler
Posts: 41
Joined: 03 Jun 2023, 17:30

Intercepting Frontend Product Controller with Product Decorator

Post by BrianMecler » 23 Feb 2024, 13:50

Laravel Framework v10.45.1, php8.1-fpm, aimeos/aimeos-laravel 2023.10.7

Hello,

I want to be able to display a special variation of a product if the user passes a special query parameter in the request. I am struggling to do that. I am currently trying to to this with a Product Decorator in my own extension and intercept the get() method..

I have a new class I created for the Decorator which is located in my packages/mytheme/src/Controller/Frontend/Product/Decorator/SpecialVariant.php:

Code: Select all

<?php

namespace Aimeos\Controller\Frontend\Product\Decorator;

use Aimeos\Controller\Frontend\Product\Iface;
use Aimeos\Controller\Frontend\Product\Decorator\Base;

class Specialvariant extends Base
{
    public function get(string $id, string $ref = 'default', bool $all = false): \Aimeos\MShop\Product\Item\Iface
    {
        // Get the original product item
        $product = $this->getController()->get($id, $ref, $all);
        
        die();

        // Check if the special query parameter is present
        $specialParam = request()->query('special');
        if ($specialParam === 'true') {
            // Implement logic to handle special variation of the product
            // Modify $product accordingly
            die();
            return $product;
        }

        return $product;
    }
}
And then I added this to my shop.php 'controller' key:

Code: Select all

	'controller' => [
		'frontend' => [
			'catalog' => [
				'levels-always' => 3 // number of category levels for mega menu
			],
            'product' => [
                'standard' => [
                    'name' => 'Specialvariant',
                    'decorators' => [
                        'local' => ['Specialvariant']
                    ],
                ]
            ]
		]
	],
I would expect that anytime I try to access a product the app would die(), but this doesnt seem to be the case. It doesn't appear my Decorator is working the way I expect.. Am I approaching this the correct way?

BrianMecler
Posts: 41
Joined: 03 Jun 2023, 17:30

Re: Intercepting Frontend Product Controller with Product Decorator

Post by BrianMecler » 23 Feb 2024, 20:24

Testing further, I do believe that that my decorator is being honored. I can override the method for 'public function __construct' and see that my debugger enters that function in the 'Specialvariant' class.

So maybe that get() function is just never being called/used from the Standard class when displaying a product? I assumed that while loading a product on the website, it would call get() to get the product and I could tap into the method to do anything I want before returning the $product..

Gagik
Posts: 38
Joined: 05 Dec 2023, 06:58

Re: Intercepting Frontend Product Controller with Product Decorator

Post by Gagik » 24 Feb 2024, 14:30

Hi.

Did you clear the cache ?

BrianMecler
Posts: 41
Joined: 03 Jun 2023, 17:30

Re: Intercepting Frontend Product Controller with Product Decorator

Post by BrianMecler » 24 Feb 2024, 22:04

Hello,

I have been running this command between tests:
php artisan optimize:clear

I have also been running composer update between tests in the event that the files from my extension need to be picked up again.

There might be an easier way to do what I am trying to accomplish:
  • I have a product in the store that I will run Google Ads on and I want to pass a query parameter in the URL so that I know the traffic came from Google Ads, ie: http://mysite/shop/test-product?source=google
  • If the traffic was not referred by Google Ads (ill check the source=google), then I want to product to display differently. Different images, different title, different description, different price, etc.
  • Public visitors to the store would never be able to find this 'special' product, unless they had ?source=google in the URL query parameters.

Gagik
Posts: 38
Joined: 05 Dec 2023, 06:58

Re: Intercepting Frontend Product Controller with Product Decorator

Post by Gagik » 25 Feb 2024, 10:47

You have configured your configurations incorrectly.

Try this.

Code: Select all

	'product' => [
                'decorators' => [
                    'local' => ['Specialvariant']
                ]
         ]

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

Re: Intercepting Frontend Product Controller with Product Decorator

Post by aimeos » 25 Feb 2024, 11:31

BrianMecler wrote: 24 Feb 2024, 22:04
I have a product in the store that I will run Google Ads on and I want to pass a query parameter in the URL so that I know the traffic came from Google Ads, ie: http://mysite/shop/test-product?source=google[/list]

If the traffic was not referred by Google Ads (ill check the source=google), then I want to product to display differently. Different images, different title, different description, different price, etc.[/list]

Public visitors to the store would never be able to find this 'special' product, unless they had ?source=google in the URL query parameters.
If the product is so different, why don't you create new product that only contains the relevant information and add it to a hidden category so it's not visible without knowing the exact URL?
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

BrianMecler
Posts: 41
Joined: 03 Jun 2023, 17:30

Re: Intercepting Frontend Product Controller with Product Decorator

Post by BrianMecler » 25 Feb 2024, 21:45

I want to use the URL of this product in Google Ads, but I dont want the product to show up in search engines. I believe that Google will try to index the product since I used it in an Ad. So my idea would be to masquerade the product if someone found it organically via a search engine.

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

Re: Intercepting Frontend Product Controller with Product Decorator

Post by aimeos » 27 Feb 2024, 10:32

You can prevent URLs from being indexed in the robots.txt. This is much easier than implementing it the way you outlined.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

BrianMecler
Posts: 41
Joined: 03 Jun 2023, 17:30

Re: Intercepting Frontend Product Controller with Product Decorator

Post by BrianMecler » 27 Feb 2024, 22:11

Thank you Aimeos,

I could go that route, but I would need to prevent indexing by using a specific meta tag on that product. Using robots.txt will prevent crawling, but not indexing (ie: the case of an external link).

I see some options on the product to add meta keywords and description, but no custom tags.

I see the following text here about metatags:
https://aimeos.org/docs/2023.x/config/c ... /#metatags
This setting enables you to suppress these tags in the page header and maybe add your own to the page manually.
Maybe adding my own sounds interesting, do you have any insight on this?

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

Re: Intercepting Frontend Product Controller with Product Decorator

Post by aimeos » 01 Mar 2024, 13:17

BrianMecler wrote: 27 Feb 2024, 22:11 Maybe adding my own sounds interesting, do you have any insight on this?
This disables adding meta tags for the product to the pages completely and you would have to add it otherwise, e.g. in the Blade template, so this is not what you want.
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply