From 2d749ab50e92b0200de1df38af41495da074ed39 Mon Sep 17 00:00:00 2001 From: Bob Wezelman Date: Mon, 22 Jul 2024 11:06:49 +0200 Subject: [PATCH] V2 Cross-sells (#20) --- resources/js/components.js | 2 ++ resources/js/components/CrossSells.vue | 23 +++++++++++++++++++ routes/api.php | 5 ++++ ...utomaticRelatedProductsServiceProvider.php | 12 +++++++--- .../Controllers/AmastyCrossSellController.php | 19 +++++++++++++++ src/Models/AmastyCrossSells.php | 13 +++++++++++ src/Models/Scopes/CrossselProductsScope.php | 17 ++++++-------- 7 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 resources/js/components/CrossSells.vue create mode 100644 routes/api.php create mode 100644 src/Http/Controllers/AmastyCrossSellController.php create mode 100644 src/Models/AmastyCrossSells.php diff --git a/resources/js/components.js b/resources/js/components.js index 4630f3c..2ce9034 100644 --- a/resources/js/components.js +++ b/resources/js/components.js @@ -1,4 +1,6 @@ import 'Vendor/rapidez/core/resources/js/vue' import amastybundles from './components/amastybundles.vue' +import CrossSells from './components/CrossSells.vue' Vue.component('amastybundles', amastybundles) +Vue.component('amasty-cross-sells', CrossSells) diff --git a/resources/js/components/CrossSells.vue b/resources/js/components/CrossSells.vue new file mode 100644 index 0000000..33872a4 --- /dev/null +++ b/resources/js/components/CrossSells.vue @@ -0,0 +1,23 @@ + diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 0000000..f3f65b6 --- /dev/null +++ b/routes/api.php @@ -0,0 +1,5 @@ +addScopes() - ->registerViews(); + ->registerViews() + ->bootRoutes(); + } + + public function bootRoutes(): self + { + $this->loadRoutesFrom(__DIR__ . '/../routes/api.php'); + + return $this; } public function addScopes(): self { Eventy::addFilter('productpage.scopes', fn ($scopes) => array_merge($scopes ?: [], [RelatedProductsScope::class])); - Eventy::addFilter('quote.scopes', fn ($scopes) => array_merge($scopes ?: [], [CrossselProductsScope::class])); Eventy::addFilter('product.casts', fn ($casts) => array_merge($casts ?: [], [ 'amasty_related_ids' => CommaSeparatedToIntegerArray::class, ])); diff --git a/src/Http/Controllers/AmastyCrossSellController.php b/src/Http/Controllers/AmastyCrossSellController.php new file mode 100644 index 0000000..52b1e2e --- /dev/null +++ b/src/Http/Controllers/AmastyCrossSellController.php @@ -0,0 +1,19 @@ +ids) + ->pluck('amasty_related_ids') + ->first(); + + return explode(',', $crossSellIds) ?? null; + } +} diff --git a/src/Models/AmastyCrossSells.php b/src/Models/AmastyCrossSells.php new file mode 100644 index 0000000..86d4f39 --- /dev/null +++ b/src/Models/AmastyCrossSells.php @@ -0,0 +1,13 @@ +selectRaw('GROUP_CONCAT(DISTINCT related.entity_id) as amasty_crosssell_ids') - ->leftJoin('amasty_mostviewed_product_index as mainrule', function ($join) { - $join->on('mainrule.entity_id', '=', 'quote_item.product_id') - ->where('mainrule.relation', 'where_show') - ->where('mainrule.position', 'cart_into_crosssel') - ->where('mainrule.store_id', config('rapidez.store')); - }) - ->leftJoin('amasty_mostviewed_product_index as related', function ($join) { - $join->on('related.rule_id', '=', 'mainrule.rule_id') + $builder->selectRaw('GROUP_CONCAT(DISTINCT related.entity_id) as amasty_related_ids') + ->leftJoin('amasty_mostviewed_product_index as related', function ($join) use ($model) { + $join->on('related.rule_id', '=', $model->qualifyColumn('rule_id')) ->where('related.relation', 'what_show') ->where('related.position', 'cart_into_crosssel') ->where('related.store_id', config('rapidez.store')); - }); + }) + ->where($model->qualifyColumn('relation'), 'where_show') + ->where($model->qualifyColumn('position'), 'cart_into_crosssel') + ->where($model->qualifyColumn('store_id'), config('rapidez.store')); } }