Injecting coupon by url parameters

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

Injecting coupon by url parameters

Post by BrianMecler » 29 Jul 2023, 12:49

Hello,

Laravel Framework 10.13.1, php8.1-fpm, aimeos/aimeos-laravel 2023.04.2

When I setup ad campaigns, im thinking I would create different URL's for advertised discounts, ex:
:
New Fishing Reels! - Free Shipping
https://mysite/shop/new-reels?code=free ... rer=google

10% Off All Plastic Fishing Lures!
https://mysite/shop/plastic-lures?code= ... r=facebook

The discounts would only be available if you came in from the given URL and then the coupon persisted in a cookie for X number of days.

What would be the best approach to accomplish this (if even done by URL parameters?)? If by URL parameters, where would be the best place to intercept/evaluate the request and start processing it to add a coupon?

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

Re: Injecting coupon by url parameters

Post by aimeos » 31 Jul 2023, 09:18

It may be possible to add a coupon code from a link but only in the basket view using the b_coupon parameter:
https://aimeos.org/docs/latest/frontend ... es/#basket

If you want to show another page, you have to create a Laravel controller which intercepts the request first, adds the coupon to the basket and than redirects to the target page.
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: Injecting coupon by url parameters

Post by BrianMecler » 06 Aug 2023, 15:53

Hello,

Could you expand on adding a coupon into the cart from my own controller?
I been looking at example code and documentation, but I'm not fully understanding..

Currently I am only as far as doing the following:

Code: Select all

class CampaignController extends Controller
{
    public function handleOffer(Request $request, $key)
    {
        $context = app('aimeos.context')->get(true);
        $orderManager = \Aimeos\MShop::create( $context, 'order' );
Then I assume from $orderManager I can somehow access a basket object and use the addCoupon() method? But I am having a hard time bridging the gap.

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

Re: Injecting coupon by url parameters

Post by BrianMecler » 06 Aug 2023, 20:47

Hello,

I may, or may not, have got a little further, my code now:

Code: Select all

class CampaignController extends Controller
{
    public function handleOffer(Request $request, $key)
    {
        $context = app('aimeos.context')->get(true);
        $orderManager = \Aimeos\MShop::create( $context, 'order' );
        $basket = $orderManager->getSession();

        $test = $basket->addCoupon('percent');
        
        return redirect()->route('aimeos_shop_detail', [$key,'0']);
Im thinking this would add the default 10% percentage coupon that comes with Aimeos.

I then redirect to the desired product, but I dont see any discounts applied. From what I read, it sounds like I am suppose to use these methods to add a coupon to an item in an order? Do I actually need to add the product to the users basket and then apply the coupon? Or can I have the coupon applied only if they end up adding the item to the basket..

Thanks

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

Re: Injecting coupon by url parameters

Post by aimeos » 08 Aug 2023, 07:29

Use the addCoupon() method of the basket controller instead:
https://github.com/aimeos/ai-controller ... e.php#L118

For Laravel, there's a facade available:

Code: Select all

\Aimeos\Shop\Facades\Basket::addCoupon('percent');
This only adds the coupon code to the basket and it doesn't reduce the price of the product until it's added to the basket.
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: Injecting coupon by url parameters

Post by BrianMecler » 09 Aug 2023, 21:41

Hello,

When I try to use '\Aimeos\Shop\Facades\Basket::addCoupon('percent')' in my controller I get:

'Illegal offset type in isset or empty'

Do I need to somehow instantiate an instance of a basket? or an instance of Aimeos? I don't understand how this controller knows anything about Aimeos in order to interact with the basket.

My controller is only this:

Code: Select all

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CampaignController extends Controller
{
    public function handleOffer(Request $request, $key)
    {
        $test = \Aimeos\Shop\Facades\Basket::addCoupon('percent');
        die();

        return redirect()->route('aimeos_shop_detail', [$key,'0']);

    }
}
Should I be extending from a different Aimeos-specific controller?
Any help is greatly appreciated.

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

Re: Injecting coupon by url parameters

Post by aimeos » 10 Aug 2023, 08:38

Do you have a stack trace of the error?
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: Injecting coupon by url parameters

Post by BrianMecler » 10 Aug 2023, 12:17

Hello,

I have shared the stack trace here:
https://flareapp.io/share/LPdkYdzm

Thank you

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

Re: Injecting coupon by url parameters

Post by aimeos » 12 Aug 2023, 09:50

Seem like the Aimeos facades needs some love ...

Use this instead:

Code: Select all

\Aimeos\Controller\Frontend::create( $context, 'basket' )->addCoupon('percent');
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, Image give us a star

Post Reply