sarga/packages/Webkul/Product/src/Models/Product.php

354 lines
9.1 KiB
PHP
Raw Normal View History

2018-07-27 06:22:12 +00:00
<?php
namespace Webkul\Product\Models;
use Illuminate\Database\Eloquent\Model;
2019-02-18 07:30:40 +00:00
use Webkul\Attribute\Models\AttributeFamilyProxy;
use Webkul\Category\Models\CategoryProxy;
use Webkul\Attribute\Models\AttributeProxy;
use Webkul\Inventory\Models\InventorySourceProxy;
use Webkul\Product\Contracts\Product as ProductContract;
class Product extends Model implements ProductContract
2018-07-27 06:22:12 +00:00
{
protected $fillable = ['type', 'attribute_family_id', 'sku', 'parent_id'];
2018-07-27 09:30:48 +00:00
2019-06-28 14:18:52 +00:00
protected $typeInstance;
2019-04-16 10:10:58 +00:00
// protected $with = ['attribute_family', 'inventories'];
2018-08-01 06:11:33 +00:00
2018-09-10 09:31:34 +00:00
// protected $table = 'products';
2018-08-01 06:11:33 +00:00
/**
* Get the product attribute family that owns the product.
*/
public function attribute_family()
{
2019-02-18 07:30:40 +00:00
return $this->belongsTo(AttributeFamilyProxy::modelClass());
2018-08-01 06:11:33 +00:00
}
/**
* Get the product attribute values that owns the product.
*/
public function attribute_values()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(ProductAttributeValueProxy::modelClass());
}
/**
2018-08-09 04:35:13 +00:00
* Get the product variants that owns the product.
*/
2018-08-09 04:35:13 +00:00
public function variants()
{
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()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(ProductReviewProxy::modelClass());
2018-08-21 10:58:55 +00:00
}
2018-08-09 04:35:13 +00:00
/**
* Get the product that owns the product.
*/
public function parent()
{
return $this->belongsTo(self::class, 'parent_id');
2018-08-09 04:35:13 +00:00
}
2018-07-27 09:30:48 +00:00
/**
* The categories that belong to the product.
*/
public function categories()
{
2019-02-18 07:30:40 +00:00
return $this->belongsToMany(CategoryProxy::modelClass(), 'product_categories');
2018-07-27 09:30:48 +00:00
}
2018-07-31 07:50:54 +00:00
/**
* The inventories that belong to the product.
*/
public function inventories()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(ProductInventoryProxy::modelClass(), 'product_id');
2018-08-09 04:35:13 +00:00
}
/**
* The ordered inventories that belong to the product.
*/
public function ordered_inventories()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(ProductOrderedInventoryProxy::modelClass(), 'product_id');
}
2018-08-09 04:35:13 +00:00
/**
* The inventory sources that belong to the product.
*/
public function inventory_sources()
{
2019-02-18 07:30:40 +00:00
return $this->belongsToMany(InventorySourceProxy::modelClass(), '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()
{
2019-02-18 07:30:40 +00:00
return $this->belongsToMany(AttributeProxy::modelClass(), 'product_super_attributes');
2018-07-31 07:50:54 +00:00
}
2018-08-09 04:35:13 +00:00
/**
* The images that belong to the product.
*/
public function images()
{
2019-02-18 07:30:40 +00:00
return $this->hasMany(ProductImageProxy::modelClass(), 'product_id');
2018-08-09 04:35:13 +00:00
}
2019-01-04 13:39:27 +00:00
/**
* The images that belong to the product.
*/
public function getBaseImageUrlAttribute()
{
$image = $this->images()->first();
return $image ? $image->url : null;
}
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', 'parent_id', 'child_id')->limit(4);
2018-07-31 07:50:54 +00:00
}
/**
* The up sells that belong to the product.
*/
public function up_sells()
{
return $this->belongsToMany(self::class, 'product_up_sells', 'parent_id', 'child_id')->limit(4);
2018-07-31 07:50:54 +00:00
}
/**
* The cross sells that belong to the product.
*/
public function cross_sells()
{
return $this->belongsToMany(self::class, 'product_cross_sells', 'parent_id', 'child_id')->limit(4);
2018-07-31 07:50:54 +00:00
}
2018-09-05 10:56:42 +00:00
2018-09-10 09:20:08 +00:00
/**
2019-06-28 14:18:52 +00:00
* The images that belong to the product.
2018-09-10 09:20:08 +00:00
*/
2019-06-28 14:18:52 +00:00
public function downloadable_samples()
2018-09-10 09:20:08 +00:00
{
2019-06-28 14:18:52 +00:00
return $this->hasMany(ProductDownloadableSampleProxy::modelClass());
}
2019-06-28 14:18:52 +00:00
/**
* The images that belong to the product.
*/
public function downloadable_links()
{
return $this->hasMany(ProductDownloadableLinkProxy::modelClass());
2019-08-19 09:30:24 +00:00
}
/**
* Get the grouped products that owns the product.
*/
public function grouped_products()
{
return $this->hasMany(ProductGroupedProductProxy::modelClass());
2018-09-10 09:20:08 +00:00
}
/**
* @param integer $qty
*
* @return bool
*/
2019-01-11 09:21:20 +00:00
public function inventory_source_qty($inventorySourceId)
{
return $this->inventories()
2019-01-11 09:21:20 +00:00
->where('inventory_source_id', $inventorySourceId)
->sum('qty');
}
2018-10-05 11:59:43 +00:00
/**
2019-06-28 14:18:52 +00:00
* Retrieve type instance
*
* @return AbstractType
2018-10-05 11:59:43 +00:00
*/
2019-06-28 14:18:52 +00:00
public function getTypeInstance()
2018-10-05 11:59:43 +00:00
{
2019-06-28 14:18:52 +00:00
if ($this->typeInstance)
return $this->typeInstance;
2018-10-05 11:59:43 +00:00
2019-06-28 14:18:52 +00:00
$this->typeInstance = app(config('product_types.' . $this->type . '.class'));
2019-06-28 14:18:52 +00:00
$this->typeInstance->setProduct($this);
2018-10-05 11:59:43 +00:00
2019-06-28 14:18:52 +00:00
return $this->typeInstance;
}
2018-10-05 11:59:43 +00:00
2019-06-28 14:18:52 +00:00
/**
* @param string $key
*
* @return bool
*/
public function isSaleable()
{
return $this->getTypeInstance()->isSaleable();
}
2018-10-05 11:59:43 +00:00
2019-06-28 14:18:52 +00:00
/**
* @return integer
*/
public function totalQuantity()
{
return $this->getTypeInstance()->totalQuantity();
}
/**
* @param integer $qty
*
* @return bool
*/
public function haveSufficientQuantity($qty)
{
2019-06-28 14:18:52 +00:00
if (! $this->getTypeInstance()->isStockable())
return true;
return $this->getTypeInstance()->haveSufficientQuantity($qty);
}
/**
* @return bool
*/
public function isStockable()
{
return $this->getTypeInstance()->isStockable();
}
/**
* Retrieve product attributes
*
* @param Group $group
* @param bool $skipSuperAttribute
* @return Collection
*/
public function getEditableAttributes($group = null, $skipSuperAttribute = true)
{
return $this->getTypeInstance()->getEditableAttributes($group, $skipSuperAttribute);
2018-10-05 11:59:43 +00: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-09-20 10:01:51 +00:00
{
2019-04-16 10:10:58 +00:00
if (! method_exists(self::class, $key) && ! in_array($key, ['parent_id', 'attribute_family_id']) && ! isset($this->attributes[$key])) {
2018-10-12 08:22:06 +00:00
if (isset($this->id)) {
2018-08-22 07:05:17 +00:00
$this->attributes[$key] = '';
2019-04-18 07:22:51 +00:00
$attribute = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getAttributeByCode($key);
2018-08-09 04:35:13 +00:00
2018-10-12 08:22:06 +00:00
$this->attributes[$key] = $this->getCustomAttributeValue($attribute);
2018-08-22 07:05:17 +00:00
return $this->getAttributeValue($key);
2018-08-09 04:35:13 +00:00
}
}
return parent::getAttribute($key);
}
2018-08-01 06:11:33 +00:00
/**
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
$hiddenAttributes = $this->getHidden();
2019-01-15 11:54:41 +00:00
if (isset($this->id)) {
2019-04-18 07:22:51 +00:00
$familyAttributes = core()->getSingletonInstance(\Webkul\Attribute\Repositories\AttributeRepository::class)
->getFamilyAttributes($this->attribute_family);
foreach ($familyAttributes as $attribute) {
2018-08-30 13:22:15 +00:00
if (in_array($attribute->code, $hiddenAttributes)) {
continue;
2018-08-09 04:35:13 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-10 10:26:27 +00:00
$attributes[$attribute->code] = $this->getCustomAttributeValue($attribute);
2018-08-30 13:22:15 +00:00
}
}
2018-08-01 06:11:33 +00:00
return $attributes;
2018-08-01 06:11:33 +00:00
}
2018-08-30 13:22:15 +00:00
2018-10-10 10:26:27 +00:00
/**
* Get an product attribute value.
*
* @return mixed
*/
public function getCustomAttributeValue($attribute)
{
2019-01-15 11:54:41 +00:00
if (! $attribute)
2018-10-10 10:26:27 +00:00
return;
2018-10-12 08:22:06 +00:00
$channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode());
2018-10-10 10:26:27 +00:00
$locale = request()->get('locale') ?: app()->getLocale();
2019-01-15 11:54:41 +00:00
if ($attribute->value_per_channel) {
if ($attribute->value_per_locale) {
2018-10-10 10:26:27 +00:00
$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 {
2019-01-15 11:54:41 +00:00
if ($attribute->value_per_locale) {
2018-10-10 10:26:27 +00:00
$attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first();
} else {
$attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first();
}
}
return $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]];
}
2018-08-30 13:22:15 +00:00
/**
* Overrides the default Eloquent query builder
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newEloquentBuilder($query)
{
return new \Webkul\Product\Database\Eloquent\Builder($query);
}
2019-04-16 11:07:52 +00:00
/**
* Return the product id attribute.
*/
public function getProductIdAttribute()
{
2019-04-18 07:22:51 +00:00
return $this->id;
2019-04-16 11:07:52 +00:00
}
2019-04-18 07:22:51 +00:00
/**
* Return the product attribute.
*/
public function getProductAttribute()
{
return $this;
}
2018-07-27 06:22:12 +00:00
}