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

101 lines
2.8 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';
protected $fillable = ['customer_id', 'session_id', 'channel_id', 'coupon_code', 'is_gift', 'items_count', 'items_qty', 'exchange_rate', 'global_currency_code', 'base_currency_code', 'channel_currency_code', 'cart_currency_code', 'grand_total', 'base_grand_total', 'sub_total', 'base_sub_total', 'sub_total_with_discount', 'base_sub_total_with_discount', 'checkout_method', 'is_guest', 'is_active', 'customer_first_name', 'conversion_time'];
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
}