Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to extend the CartItem class #124

Open
rucky96 opened this issue Apr 15, 2021 · 8 comments
Open

Possibility to extend the CartItem class #124

rucky96 opened this issue Apr 15, 2021 · 8 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rucky96
Copy link

rucky96 commented Apr 15, 2021

Hi,

The package seems very powerful to me, congrats.

I understand that you cannot satisfy everyone by adding new functions, each e-shop is different and many require almost unique solutions. I mean, an example is a function to calculate the price of the cart + shipping. Each one needs to do it in a different way. I managed to extend the Cart class to add it but I also have other needs such as doing calculations on each product which are not included in the original package. This requires extending the CartItem class and it is not that simple, at least I have not succeeded (it is also true that I am quite noob). Maybe in the same way one can customize the default calculator?

That is why I think it would be interesting to open the possibility of extending the classes in a simple way, to avoid rewriting the package and adapting it to each e-shop. Actually, in my case it would be necessary to include just two functions, the package is very complete.

@johnthart
Copy link

johnthart commented Apr 16, 2021

You could add a custom Macro within a service provider, since the Macro trait was added to the Cart class.

This is what I use for example:

        Cart::macro('trackedShipping', function () {
            if ($this->count() >= 3) {
                return true;
            }

            // more code ...

            return false;
        });

Hope this helps

@rucky96
Copy link
Author

rucky96 commented Apr 16, 2021

Hi @johnthart,

Yes, you are right. But this has the same effect than using an extended class for Cart. What I see complicated is modifying the CartItem class.

@johnthart
Copy link

Hi @johnthart,

Yes, you are right. But this has the same effect than using an extended class for Cart. What I see complicated is modifying the CartItem class.

Sorry, you're right. Didn't read far enough.

@rucky96
Copy link
Author

rucky96 commented Apr 16, 2021

Hi @johnthart ,

No problem. But I think, if possible in a next expansion, it would be fine to change the variable $discount and $taxRate to protected. So we could use functions like total() in an extended Cart class. I would also appreciate if extending the CartItem class were possible.

@rucky96 rucky96 closed this as completed Apr 16, 2021
@bumbummen99
Copy link
Owner

Why the close? I think this is a valid issue :) If you can, please reopen so we can keep track of it 👍

@bumbummen99 bumbummen99 added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Apr 16, 2021
@rucky96 rucky96 reopened this Apr 17, 2021
@rucky96
Copy link
Author

rucky96 commented Apr 17, 2021

OK! @bumbummen99 😊

@rucky96
Copy link
Author

rucky96 commented Apr 24, 2021

I have successfully extended the Cart class by adding a new service provider and binding the default class to my custom class:

  1. In config/app.php:
//[...]

    'providers' => [
        //[...]

        App\Providers\CartServiceProvider::class,
      ],

//[...]
  1. In app/Providers/CartServiceProvider.php
<?php

namespace App\Providers;

use App\Services\Cart\Cart;
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;

class CartServiceProvider extends ShoppingcartServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        parent::register();

        $this->app->bind('cart', function($app){
            $session = $app['session'];
            $events = $app['events'];
           return new Cart($session, $events);
        });

    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
  1. Finally, the file app/Services/Cart/Cart.php:
<?php

namespace App\Services\Cart;

use Gloudemans\Shoppingcart\Cart as DefaultCart;

class Cart extends DefaultCart
{
    //
}

@rucky96
Copy link
Author

rucky96 commented Apr 24, 2021

To extend class CartItem I have repeated the same process but the result has not been satisfactory. I have not gotten Laravel to read the new class

  1. I have added the binding in app/Providers/CartServiceProvider.php
<?php

namespace App\Providers;

use App\Services\Cart\Cart;
use App\Services\Cart\CartItem;    //<-- I have added this
use Gloudemans\Shoppingcart\CartItem as CartItemDefault;    //<-- I have added this
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;

class CartServiceProvider extends ShoppingcartServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        parent::register();

        $this->app->bind('cart', function($app){
            $session = $app['session'];
            $events = $app['events'];
           return new Cart($session, $events);
        });

        $this->app->singleton(CartItemDefault::class, CartItem::class);    //<-- I have added this

    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}


  1. The file app/Services/Cart/CartItem.php:
<?php

namespace App\Services\Cart;

use Gloudemans\Shoppingcart\CartItem as DefaultCartItem;

class CartItem extends DefaultCartItem
{
    //
}

This is not working for me. Does anyone figure out why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants