Skip to content

Commit

Permalink
V2 Cross-sells (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobWez98 authored Jul 22, 2024
1 parent 1ceb5d8 commit 2d749ab
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 13 deletions.
2 changes: 2 additions & 0 deletions resources/js/components.js
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions resources/js/components/CrossSells.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script>
export default {
render() {
return this.$scopedSlots.default(this)
},
data: () => ({
amastyIds: []
}),
async mounted() {
this.amastyIds = await rapidezAPI('post', 'cart/cross-sells', {
ids: this.productIds
})
},
computed: {
productIds() {
return this.$root.cart.items.map((item) => item.product.id)
}
}
}
</script>
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

use Rapidez\AmastyAutomaticRelatedProducts\Http\Controllers\AmastyCrossSellController;

Route::post('/api/cart/cross-sells', AmastyCrossSellController::class);
12 changes: 9 additions & 3 deletions src/AmastyAutomaticRelatedProductsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Rapidez\AmastyAutomaticRelatedProducts;

use Illuminate\Support\ServiceProvider;
use Rapidez\AmastyAutomaticRelatedProducts\Models\Scopes\CrossselProductsScope;
use Rapidez\AmastyAutomaticRelatedProducts\Models\Scopes\RelatedProductsScope;
use Rapidez\Core\Casts\CommaSeparatedToIntegerArray;
use TorMorten\Eventy\Facades\Eventy;
Expand All @@ -14,13 +13,20 @@ public function boot()
{
$this
->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,
]));
Expand Down
19 changes: 19 additions & 0 deletions src/Http/Controllers/AmastyCrossSellController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rapidez\AmastyAutomaticRelatedProducts\Http\Controllers;


use Illuminate\Http\Request;
use Rapidez\AmastyAutomaticRelatedProducts\Models\AmastyCrossSells;

class AmastyCrossSellController
{
public function __invoke(Request $request): array|null
{
$crossSellIds = AmastyCrossSells::whereIn('amasty_mostviewed_product_index.entity_id', $request->ids)
->pluck('amasty_related_ids')
->first();

return explode(',', $crossSellIds) ?? null;
}
}
13 changes: 13 additions & 0 deletions src/Models/AmastyCrossSells.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rapidez\AmastyAutomaticRelatedProducts\Models;

use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Rapidez\AmastyAutomaticRelatedProducts\Models\Scopes\CrossselProductsScope;
use Rapidez\Core\Models\Model;

#[ScopedBy([CrossselProductsScope::class])]
class AmastyCrossSells extends Model
{
protected $table = 'amasty_mostviewed_product_index';
}
17 changes: 7 additions & 10 deletions src/Models/Scopes/CrossselProductsScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ class CrossselProductsScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->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'));
}
}

0 comments on commit 2d749ab

Please sign in to comment.