From fd12783ec063ff48e2b8156015f541624b8b51fb Mon Sep 17 00:00:00 2001 From: Muammer Top Date: Mon, 21 Jun 2021 13:39:15 +0300 Subject: [PATCH] added bestseller twig function --- ...oft.module.advs__add_total-sales_field.php | 26 ++++++++++ .../advs-module/src/Adv/AdvModel.php | 52 +++++++++++++++++-- .../advs-module/src/AdvsModulePlugin.php | 13 +++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 addons/default/visiosoft/advs-module/migrations/2021_06_21_101029_visiosoft.module.advs__add_total-sales_field.php diff --git a/addons/default/visiosoft/advs-module/migrations/2021_06_21_101029_visiosoft.module.advs__add_total-sales_field.php b/addons/default/visiosoft/advs-module/migrations/2021_06_21_101029_visiosoft.module.advs__add_total-sales_field.php new file mode 100644 index 000000000..3012804f1 --- /dev/null +++ b/addons/default/visiosoft/advs-module/migrations/2021_06_21_101029_visiosoft.module.advs__add_total-sales_field.php @@ -0,0 +1,26 @@ + 'advs', + ]; + + protected $fields = [ + 'total_sales' => [ + 'type' => 'anomaly.field_type.integer', + 'config' => [ + 'min' => 0, + 'default_value' => 0, + ], + ], + ]; + + protected $assignments = [ + 'total_sales' + ]; +} diff --git a/addons/default/visiosoft/advs-module/src/Adv/AdvModel.php b/addons/default/visiosoft/advs-module/src/Adv/AdvModel.php index b7227eda5..599ef84d9 100644 --- a/addons/default/visiosoft/advs-module/src/Adv/AdvModel.php +++ b/addons/default/visiosoft/advs-module/src/Adv/AdvModel.php @@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Visiosoft\AdvsModule\Adv\Contract\AdvInterface; use Anomaly\Streams\Platform\Model\Advs\AdvsAdvsEntryModel; +use Visiosoft\AdvsModule\Support\Command\Currency; use Visiosoft\LocationModule\City\CityModel; use Visiosoft\LocationModule\Country\CountryModel; use Visiosoft\CartsModule\Cart\Command\GetCart; @@ -17,6 +18,51 @@ use Visiosoft\LocationModule\Village\Contract\VillageRepositoryInterface; class AdvModel extends AdvsAdvsEntryModel implements AdvInterface { + protected $appends = [ + 'detail_url', + 'currency_price', + 'currency_standard_price', + 'category1', + 'category2', + 'thumbnail', + ]; + + public function getDetailUrlAttribute() + { + return $this->getAdvDetailLinkByModel($this, 'list'); + } + + public function getCurrencyPriceAttribute() + { + return app(Currency::class)->format($this->price, $this->currency); + } + + public function getCurrencyStandardPriceAttribute() + { + return app(Currency::class)->format($this->standard_price, $this->currency); + } + + public function getCategory1Attribute() + { + return $this->hasMany('Visiosoft\CatsModule\Category\CategoryModel', 'id', 'cat1')->first(); + + } + + public function getCategory2Attribute() + { + return $this->hasMany('Visiosoft\CatsModule\Category\CategoryModel', 'id', 'cat1')->first(); + + } + + public function getThumbnailAttribute() + { + if ($this->cover_photo == null) { + return $this->dispatch(new MakeImageInstance('visiosoft.theme.base::images/no-image.png', 'img'))->url(); + } else { + return url($this->cover_photo); + } + } + public function getTransNameAttribute() { if (is_null($this->name)) { @@ -278,12 +324,10 @@ class AdvModel extends AdvsAdvsEntryModel implements AdvInterface public function stockControl($id, $quantity) { - if($adv = $this->getAdv($id)) - { + if ($adv = $this->getAdv($id)) { $stock = $adv->stock; - if($stock and $stock >= $quantity) - { + if ($stock and $stock >= $quantity) { return 1; } } diff --git a/addons/default/visiosoft/advs-module/src/AdvsModulePlugin.php b/addons/default/visiosoft/advs-module/src/AdvsModulePlugin.php index 0a1bcc3e3..91f553b45 100644 --- a/addons/default/visiosoft/advs-module/src/AdvsModulePlugin.php +++ b/addons/default/visiosoft/advs-module/src/AdvsModulePlugin.php @@ -49,6 +49,19 @@ class AdvsModulePlugin extends Plugin return $latestAds; } ), + new \Twig_SimpleFunction( + 'bestsellerAds', + function ($catId = null) { + return AdvModel::query() + ->orderBy('total_sales', 'desc') + ->where(function ($query) use ($catId) { + if ($catId) { + $query->where('cat1', $catId); + } + }) + ->limit(10)->get(); + } + ), new \Twig_SimpleFunction( 'appendRequestURL', function ($request, $url, $new_parameters, $removeParams = []) {