sarga/packages/Webkul/Checkout/src/Models/Cart.php

101 lines
2.4 KiB
PHP
Raw Normal View History

2018-09-06 06:20:30 +00:00
<?php
namespace Webkul\Checkout\Models;
2018-09-06 06:20:30 +00:00
2018-09-10 09:31:34 +00:00
use Illuminate\Database\Eloquent\Model;
use Webkul\Product\Models\Product;
use Webkul\Checkout\Models\CartItem;
use Webkul\Checkout\Models\CartAddress;
use Webkul\Checkout\Models\CartPayment;
use Webkul\Checkout\Models\CartShippingRate;
2018-09-10 09:31:34 +00:00
class Cart extends Model
2018-09-06 06:20:30 +00:00
{
protected $table = 'cart';
2018-10-10 10:41:33 +00:00
protected $guarded = ['id', 'created_at', 'updated_at'];
2018-09-06 06:20:30 +00:00
protected $hidden = ['coupon_code'];
2018-09-10 09:31:34 +00:00
2018-10-05 06:18:58 +00:00
protected $with = ['items', 'items.child', 'shipping_address', 'billing_address', 'selected_shipping_rate', 'payment'];
public function items() {
2018-09-27 10:01:25 +00:00
return $this->hasMany(CartItem::class)->whereNull('parent_id');
2018-09-10 09:31:34 +00:00
}
2018-09-20 10:01:51 +00:00
public function all_items() {
return $this->hasMany(CartItem::class);
}
2018-09-20 10:01:51 +00:00
/**
* Get the addresses for the cart.
*/
public function addresses()
{
return $this->hasMany(CartAddress::class);
}
/**
2018-09-26 04:21:14 +00:00
* Get the biling address for the cart.
2018-09-20 10:01:51 +00:00
*/
2018-09-27 07:53:26 +00:00
public function billing_address()
2018-09-20 10:01:51 +00:00
{
2018-09-26 04:21:14 +00:00
return $this->addresses()->where('address_type', 'billing');
2018-09-20 10:01:51 +00:00
}
2018-09-26 04:21:14 +00:00
/**
* Get billing address for the cart.
2018-09-26 04:21:14 +00:00
*/
2018-09-27 07:53:26 +00:00
public function getBillingAddressAttribute()
2018-09-26 04:21:14 +00:00
{
2018-09-27 07:53:26 +00:00
return $this->billing_address()->first();
2018-09-26 04:21:14 +00:00
}
/**
* Get the shipping address for the cart.
*/
public function shipping_address()
{
return $this->addresses()->where('address_type', 'shipping');
}
/**
* Get shipping address for the cart.
2018-09-26 04:21:14 +00:00
*/
public function getShippingAddressAttribute()
{
return $this->shipping_address()->first();
}
/**
* Get the shipping rates for the cart.
*/
public function shipping_rates()
{
return $this->hasManyThrough(CartShippingRate::class, CartAddress::class, 'cart_id', 'cart_address_id');
}
2018-09-26 10:19:18 +00:00
2018-10-05 06:18:58 +00:00
/**
* Get all of the attributes for the attribute groups.
*/
public function selected_shipping_rate()
{
return $this->shipping_rates()->where('method', $this->shipping_method);
}
2018-09-26 10:19:18 +00:00
/**
* Get all of the attributes for the attribute groups.
*/
public function getSelectedShippingRateAttribute()
{
2018-10-05 06:18:58 +00:00
return $this->selected_shipping_rate()->where('method', $this->shipping_method)->first();
2018-09-26 10:19:18 +00:00
}
/**
* Get the payment associated with the cart.
*/
public function payment()
{
return $this->hasOne(CartPayment::class);
}
2018-09-26 04:21:14 +00:00
}