简体   繁体   中英

Extending Vendor Class in Laravel

I am using a cart system (\\Gloudemans\\Shoppingcart) but I want to override the default total() method:

namespace Gloudemans\Shoppingcart;
/**
     * Get the total price of the items in the cart.
     *
     * @param int    $decimals
     * @param string $decimalPoint
     * @param string $thousandSeperator
     * @return string
     */
    public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
    {
        $content = $this->getContent();

        $total = $content->reduce(function ($total, CartItem $cartItem) {
            return $total + ($cartItem->qty * $cartItem->priceTax);
        }, 0);

        return $this->numberFormat($total, $decimals, $decimalPoint, $thousandSeperator);
    }

My extension:

namespace App\Http\Controllers;

use Gloudemans\Shoppingcart\Facades\Cart;
use \Illuminate\Http\Request;

class CartController extends Cart
{
    function __construct() {

    }

    public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null) {
        $content = $this->getContent();

        $total = $content->reduce(function ($total, CartItem $cartItem) {
            return $total + ($cartItem->qty * $cartItem->priceTax);
        }, 0);
        $currency = new Currency();
        $currency_id = $currency->where('code', session()->get('currency'))->first()->id;
        $rate = CurrencyRate::where('currency_id', $currency_id)->latest()->first()->rate;

        return $this->numberFormat($total*$rate, $decimals, $decimalPoint, $thousandSeperator);
    }
}

However I am not sure how to access it, this is how I access the default method which works:

...
<span id="cart_total" class="btn btn-default"><i class="fa fa-shopping-cart" aria-hidden="true"></i>
                               {{$symbol}} {{\Gloudemans\Shoppingcart\Facades\Cart::total()}}</span>
...

The facade above is:

namespace Gloudemans\Shoppingcart\Facades;

use Illuminate\Support\Facades\Facade;

class Cart extends Facade {
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'cart';
    }
}

Obviously the following doesn't work:

{{\App\Http\Controllers\CartController::total()}}

Here is a link to the cart system I am using:

https://github.com/Crinsane/LaravelShoppingcart

You have to have a dedicated class that overrides the default functionality.

class myCart extends Cart {

    public function total($decimals = null ....

}

now if you can create new instance of the myCart and use it to calculate total

var obj = new myCart();
obj->total();

If needed you can create the fascade for myCart class and use it.

Doing the following in a facade:

Facade::something();

is a shortcut for

app()->make(Facade::getFacadeAccessor())->something();

You therefore need to extend class returned by making a "cart" and override the total method in there.

Eg assume :

class Cart { 
     //...
     public function total() {}
}

class MyCart extends Cart {
     //...
     //Your total:
     public function total() {}
}

Note: The first cart here is not the facade but the actual class that the facade returns.

Then you need to rebind it. To do this you need to make sure you have a service provider that runs after it's bound the first time and then:

 public function handle() {
        $this->app->bind("cart", function ($app) { return new MyCart(); });
 }

There shouldn't be any need to override the facade.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM