2018-07-27 06:22:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Product\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-08-02 04:19:45 +00:00
|
|
|
use Webkul\Attribute\Models\AttributeFamily;
|
2018-07-27 09:30:48 +00:00
|
|
|
use Webkul\Category\Models\Category;
|
2018-08-02 04:19:45 +00:00
|
|
|
use Webkul\Attribute\Models\Attribute;
|
|
|
|
|
use Webkul\Product\Models\ProductAttributeValue;
|
2018-08-09 04:35:13 +00:00
|
|
|
use Webkul\Product\Models\ProductInventory;
|
|
|
|
|
use Webkul\Product\Models\ProductImage;
|
2018-07-31 07:50:54 +00:00
|
|
|
use Webkul\Inventory\Models\InventorySource;
|
2018-08-21 10:58:55 +00:00
|
|
|
use Webkul\Product\Models\ProductReview;
|
2018-07-27 06:22:12 +00:00
|
|
|
|
|
|
|
|
class Product extends Model
|
|
|
|
|
{
|
2018-08-02 04:19:45 +00:00
|
|
|
protected $fillable = ['type', 'attribute_family_id', 'sku', 'parent_id'];
|
2018-07-27 09:30:48 +00:00
|
|
|
|
2018-08-21 10:58:55 +00:00
|
|
|
protected $with = ['attribute_family', 'attribute_values', 'inventories'];
|
2018-08-01 06:11:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the product attribute family that owns the product.
|
|
|
|
|
*/
|
|
|
|
|
public function attribute_family()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(AttributeFamily::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 04:19:45 +00:00
|
|
|
/**
|
|
|
|
|
* Get the product attribute values that owns the product.
|
|
|
|
|
*/
|
|
|
|
|
public function attribute_values()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ProductAttributeValue::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-08-09 04:35:13 +00:00
|
|
|
* Get the product variants that owns the product.
|
2018-08-02 04:19:45 +00:00
|
|
|
*/
|
2018-08-09 04:35:13 +00:00
|
|
|
public function variants()
|
2018-08-02 04:19:45 +00:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 10:58:55 +00:00
|
|
|
/**
|
|
|
|
|
* Get the product reviews that owns the product.
|
|
|
|
|
*/
|
|
|
|
|
public function reviews()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ProductReview::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* Get the product that owns the product.
|
|
|
|
|
*/
|
|
|
|
|
public function parent()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(self::class);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-27 09:30:48 +00:00
|
|
|
/**
|
|
|
|
|
* The categories that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function categories()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Category::class, 'product_categories');
|
|
|
|
|
}
|
2018-07-31 07:50:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The inventories that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function inventories()
|
|
|
|
|
{
|
2018-08-09 04:35:13 +00:00
|
|
|
return $this->hasMany(ProductInventory::class, 'product_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The inventory sources that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function inventory_sources()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(InventorySource::class, 'product_inventories')->withPivot('id', 'qty');
|
2018-07-31 07:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The super attributes that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function super_attributes()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Attribute::class, 'product_super_attributes');
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
/**
|
|
|
|
|
* The images that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function images()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ProductImage::class, 'product_id');
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 07:50:54 +00:00
|
|
|
/**
|
|
|
|
|
* The related products that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function related_products()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(self::class, 'product_relations');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The up sells that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function up_sells()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(self::class, 'product_up_sells');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The cross sells that belong to the product.
|
|
|
|
|
*/
|
|
|
|
|
public function cross_sells()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(self::class, 'product_cross_sells');
|
|
|
|
|
}
|
2018-08-09 04:35:13 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $key
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isCustomAttribute($attribute)
|
|
|
|
|
{
|
|
|
|
|
return $this->attribute_family->custom_attributes->pluck('code')->contains($attribute);
|
|
|
|
|
}
|
2018-08-21 10:58:55 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getFinalPrice()
|
|
|
|
|
{
|
|
|
|
|
return 0.00;
|
|
|
|
|
}
|
2018-08-09 04:35:13 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an attribute from the model.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getAttribute($key)
|
|
|
|
|
{
|
2018-08-21 10:58:55 +00:00
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
if (!method_exists(self::class, $key) && !isset($this->attributes[$key])) {
|
2018-08-21 10:58:55 +00:00
|
|
|
$this->attributes[$key] = '';
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
if ($this->isCustomAttribute($key)) {
|
|
|
|
|
$attributeModel = $this->attribute_family->custom_attributes()->where('attributes.code', $key)->first();
|
|
|
|
|
|
|
|
|
|
if($attributeModel) {
|
2018-08-21 10:58:55 +00:00
|
|
|
$channel = request()->get('channel') ?: core()->getCurrentChannelCode();
|
|
|
|
|
|
|
|
|
|
$locale = request()->get('locale') ?: app()->getLocale();
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
if($attributeModel->value_per_channel) {
|
|
|
|
|
if($attributeModel->value_per_locale) {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attributeModel->id)->first();
|
|
|
|
|
} else {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attributeModel->id)->first();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if($attributeModel->value_per_locale) {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attributeModel->id)->first();
|
|
|
|
|
} else {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('attribute_id', $attributeModel->id)->first();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 10:58:55 +00:00
|
|
|
$this->attributes[$key] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attributeModel->type]];
|
2018-08-09 04:35:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getAttributeValue($key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parent::getAttribute($key);
|
|
|
|
|
}
|
2018-08-01 06:11:33 +00:00
|
|
|
|
2018-08-02 04:19:45 +00:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function attributesToArray()
|
|
|
|
|
{
|
|
|
|
|
$attributes = parent::attributesToArray();
|
|
|
|
|
|
|
|
|
|
$hiddenAttributes = $this->getHidden();
|
|
|
|
|
|
2018-08-21 10:58:55 +00:00
|
|
|
$channel = request()->get('channel') ?: core()->getCurrentChannelCode();
|
|
|
|
|
|
|
|
|
|
$locale = request()->get('locale') ?: app()->getLocale();
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
foreach ($this->attribute_family->custom_attributes as $attribute) {
|
2018-08-02 04:19:45 +00:00
|
|
|
if (in_array($attribute->code, $hiddenAttributes)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-09 04:35:13 +00:00
|
|
|
if($attribute->value_per_channel) {
|
|
|
|
|
if($attribute->value_per_locale) {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
|
|
|
|
|
} else {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if($attribute->value_per_locale) {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
|
|
|
|
|
} else {
|
|
|
|
|
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-21 10:58:55 +00:00
|
|
|
$attributes[$attribute->code] = $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
|
2018-08-02 04:19:45 +00:00
|
|
|
}
|
2018-08-01 06:11:33 +00:00
|
|
|
|
2018-08-02 04:19:45 +00:00
|
|
|
return $attributes;
|
2018-08-01 06:11:33 +00:00
|
|
|
}
|
2018-07-27 06:22:12 +00:00
|
|
|
}
|