Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for image disabled option #381

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/rapidez/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'option_swatch' => Rapidez\Core\Models\OptionSwatch::class,
'option_value' => Rapidez\Core\Models\OptionValue::class,
'product_image' => Rapidez\Core\Models\ProductImage::class,
'product_image_value' => Rapidez\Core\Models\ProductImageValue::class,
'product_view' => Rapidez\Core\Models\ProductView::class,
'product_option' => Rapidez\Core\Models\ProductOption::class,
'product_option_title' => Rapidez\Core\Models\ProductOptionTitle::class,
Expand Down
21 changes: 21 additions & 0 deletions src/Models/ProductImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@

namespace Rapidez\Core\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;

class ProductImage extends Model
{
protected $table = 'catalog_product_entity_media_gallery';

protected $primaryKey = 'value_id';

protected static function booted(): void
{
static::addGlobalScope(
'enabled',
fn (Builder $query) => $query
->where($query->qualifyColumn('disabled'), 0)
->whereHas(
'productImageValue',
fn ($query) => $query
->where($query->qualifyColumn('disabled'), 0)
)
);
}

public function productImageValue()
{
return $this->belongsTo(config('rapidez.models.product_image_value'), 'value_id', 'value_id');
}
}
24 changes: 24 additions & 0 deletions src/Models/ProductImageValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rapidez\Core\Models;

use Rapidez\Core\Models\Scopes\ForCurrentStoreWithoutLimitScope;

class ProductImageValue extends Model
{
protected $table = 'catalog_product_entity_media_gallery_value';

protected $primaryKey = 'record_id';

public $timestamps = false;

protected static function booted(): void
{
static::addGlobalScope(new ForCurrentStoreWithoutLimitScope('value_id'));
}

public function productImage()
{
return $this->belongsTo(config('rapidez.models.product_image'), 'value_id');
}
}
41 changes: 41 additions & 0 deletions src/Models/Scopes/ForCurrentStoreWithoutLimitScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rapidez\Core\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

/**
* Remove results from the default store when store view specific is found.
*/
class ForCurrentStoreWithoutLimitScope implements Scope
{
public function __construct(public $uniquePerStoreKey, public $storeIdColumn = 'store_id')
{
}

public function apply(Builder $query, Model $model)
{
if (! config('rapidez.store')) {
return $query
->where($query->qualifyColumn($this->storeIdColumn), 0);
}

return $query
// Pre-filter results to be default and current store only.
->whereIn($query->qualifyColumn($this->storeIdColumn), [0, config('rapidez.store')])
// Remove values from the default store where values for the current store exist.
->where(fn ($query) => $query
// Remove values where we already have values in the current store.
->whereNotIn($query->qualifyColumn($this->uniquePerStoreKey), fn ($query) => $query
->select($model->qualifyColumn($this->uniquePerStoreKey))
->from($model->getTable())
->whereColumn($model->qualifyColumn($this->uniquePerStoreKey), $model->qualifyColumn($this->uniquePerStoreKey))
->where($model->qualifyColumn($this->storeIdColumn), config('rapidez.store'))
)
// Unless the value IS the current store.
->orWhere($query->qualifyColumn($this->storeIdColumn), config('rapidez.store'))
);
}
}
Loading