Crinsane / LaravelShoppingcart

A simple shopping cart implementation for Laravel
MIT License
3.67k stars 1.73k forks source link

Buyable interface #189

Closed matbeard closed 8 years ago

matbeard commented 8 years ago

Great bit of work with LaravelShoppingcart! I'm interested in implementing the Buyable interface on my product Model. Can you please provide some pointers on how to do this?

Thanks

mateusjatenee commented 8 years ago

class Product implements Buyable

Crinsane commented 8 years ago

See above

matbeard commented 8 years ago

Err. Thanks, but anyone asking this question is unlikely to know how to deal with half an answer.

I've added class Product implements Buyable to my model, and now I get an error saying: Class App\Product contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableIdentifier, Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableDescription, Gloudemans\Shoppingcart\Contracts\Buyable::getBuyablePrice)

I suspect it's something simple, but I'm pretty new to Laravel and haven't come across anything like this before.

EDIT: It's okay. I figured it out. Anyone else wanting to know how it's done, just add the following to your product model (using whatever fields are appropriate for your model):

    public function getBuyableIdentifier(){
        return $this->id;
    }

    public function getBuyableDescription(){
        return $this->name;
    }

    public function getBuyablePrice(){
        return $this->price;
    }
Crinsane commented 8 years ago

@matbeard Sorry if it came across as a harsh answer. Just thought that would be enough information. In the end, if you're using Laravel and doing OO programming, it seems to me it's pretty trivial how to "implement an interface".

But again, didn't mean to sound harsh, and glad you figured it out! Good luck with your website!

hemant6488 commented 7 years ago

To add on to what @matbeard mentioned, It now throws an error saying: Declaration of App\Book::getBuyableDescription() must be compatible with Gloudemans\Shoppingcart\Contracts\Buyable::getBuyableDescription($options = NULL)

to fix it, add $options = null inside the method parenthesis:

public function getBuyableIdentifier($options = null){ return $this->id; }

t-prod commented 6 years ago

Also the full declaration in your Model is :

use Gloudemans\Shoppingcart\Contracts\Buyable;

class Product extends Model implements Buyable
{ ...
rakibul100636 commented 6 years ago
use Illuminate\Support\Facades\Redirect;

class CartController extends Controller
{

   public function add_to_cart_function(Request $request){

     $qty=$request->qty;
     $product_id=$request->product_id;
//     print_r($product_id);
//     print_r($qty);
//     exit();
      $product_detail_info=DB::table('tbl_product')
              ->where('id',$product_id)
              ->first();
//      echo '<pre>';
//      print_r($product_info);
//      exit();
    Cart::add(['id' => $product_detail_info->product_id, 'name' => $product_detail_info->product_name, 'qty' => $qty, 'price' => $product_detail_info->product_price, 'options' => ['image' => $product_detail_info->image_name]]);
   }

}

//////--------"Trying to get property of non-object" i am using laravel version 5.5