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

132 lines
3.1 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;
2019-02-18 07:30:40 +00:00
use Webkul\Product\Models\ProductProxy;
use Webkul\Checkout\Contracts\Cart as CartContract;
2018-09-10 09:31:34 +00:00
2019-02-18 07:30:40 +00:00
class Cart extends Model implements CartContract
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
2019-08-30 11:39:15 +00:00
protected $with = ['items', 'items.children'];
2018-10-05 06:18:58 +00:00
2018-11-25 13:43:30 +00:00
/**
* To get relevant associated items with the cart instance
*/
public function items() {
2019-02-18 07:30:40 +00:00
return $this->hasMany(CartItemProxy::modelClass())->whereNull('parent_id');
2018-09-10 09:31:34 +00:00
}
2018-09-20 10:01:51 +00:00
2018-11-25 13:43:30 +00:00
/**
* To get all the associated items with the cart instance even the parent and child items of configurable products
*/
public function all_items() {
2019-02-18 07:30:40 +00:00
return $this->hasMany(CartItemProxy::modelClass());
}
2018-09-20 10:01:51 +00:00
/**
* Get the addresses for the cart.
*/
public function addresses()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(CartAddressProxy::modelClass());
2018-09-20 10:01:51 +00:00
}
/**
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()
{
2019-02-18 07:30:40 +00:00
return $this->hasManyThrough(CartShippingRateProxy::modelClass(), CartAddressProxy::modelClass(), 'cart_id', 'cart_address_id');
2018-09-26 04:21:14 +00:00
}
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()
{
2019-02-18 07:30:40 +00:00
return $this->hasOne(CartPaymentProxy::modelClass());
2018-09-26 10:19:18 +00:00
}
2019-06-28 14:18:52 +00:00
/**
* Checks if cart have stockable items
*
* @return boolean
*/
public function haveStockableItems()
{
foreach ($this->items as $item) {
2019-06-28 14:18:52 +00:00
if ($item->product->isStockable())
return true;
}
return false;
}
/**
* Checks if cart have downloadable items
*
* @return boolean
*/
public function haveDownloadableItems()
{
foreach ($this->items as $item) {
if ($item->type == 'downloadable')
return true;
}
return false;
}
2018-09-26 04:21:14 +00:00
}