Core Currency Method Improved
This commit is contained in:
parent
0ec071674a
commit
889a574bb8
|
|
@ -84,7 +84,14 @@ class Core
|
|||
*
|
||||
* @var \Webkul\Core\Models\Channel
|
||||
*/
|
||||
private static $channel;
|
||||
protected static $channel;
|
||||
|
||||
/**
|
||||
* Currency.
|
||||
*
|
||||
* @var \Webkul\Core\Models\Currency
|
||||
*/
|
||||
protected static $currency;
|
||||
|
||||
/**
|
||||
* Register your core config keys here which you don't want to
|
||||
|
|
@ -397,6 +404,29 @@ class Core
|
|||
return request()->get('customer_group');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set currency.
|
||||
*
|
||||
* @param string $currencyCode
|
||||
* @return void
|
||||
*/
|
||||
public function setCurrency($currencyCode)
|
||||
{
|
||||
$currency = $this->currencyRepository->findOneByField('code', $currencyCode);
|
||||
|
||||
self::$currency = $currency ?: $this->getChannelBaseCurrency();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currency.
|
||||
*
|
||||
* @return \Webkul\Core\Models\Currency
|
||||
*/
|
||||
public function getCurrency()
|
||||
{
|
||||
return self::$currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all currencies.
|
||||
*
|
||||
|
|
@ -498,13 +528,7 @@ class Core
|
|||
return $currency;
|
||||
}
|
||||
|
||||
if ($currencyCode = session()->get('currency')) {
|
||||
if ($currency = $this->currencyRepository->findOneByField('code', $currencyCode)) {
|
||||
return $currency;
|
||||
}
|
||||
}
|
||||
|
||||
return $currency = $this->getChannelBaseCurrency();
|
||||
return $currency = $this->getCurrency();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -8,36 +8,43 @@ use Webkul\Core\Repositories\CurrencyRepository;
|
|||
class Currency
|
||||
{
|
||||
/**
|
||||
* @var CurrencyRepository
|
||||
* Currency repository instance.
|
||||
*
|
||||
* @var \Webkul\Core\Repositories\CurrencyRepository
|
||||
*/
|
||||
protected $currency;
|
||||
protected $currencyRepository;
|
||||
|
||||
/**
|
||||
* @param \Webkul\Core\Repositories\CurrencyRepository $locale
|
||||
* Create a currency repository instance.
|
||||
*
|
||||
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(CurrencyRepository $currency)
|
||||
public function __construct(CurrencyRepository $currencyRepository)
|
||||
{
|
||||
$this->currency = $currency;
|
||||
$this->currencyRepository = $currencyRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$currencyCode = request()->get('currency');
|
||||
if ($currencyCode = request()->get('currency')) {
|
||||
if ($this->currencyRepository->findOneByField('code', $currencyCode)) {
|
||||
core()->setCurrency($currencyCode);
|
||||
|
||||
if ($currency = $currencyCode) {
|
||||
if ($this->currency->findOneByField('code', $currency)) {
|
||||
session()->put('currency', $currency);
|
||||
session()->put('currency', $currencyCode);
|
||||
}
|
||||
} else {
|
||||
if (! session()->get('currency')) {
|
||||
session()->put('currency', core()->getChannelBaseCurrencyCode());
|
||||
if ($currencyCode = session()->get('currency')) {
|
||||
core()->setCurrency($currencyCode);
|
||||
} else {
|
||||
core()->setCurrency(core()->getChannelBaseCurrencyCode());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -45,4 +52,4 @@ class Currency
|
|||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,44 +2,44 @@
|
|||
|
||||
namespace Webkul\Shop\Http\Middleware;
|
||||
|
||||
use Webkul\Core\Repositories\LocaleRepository;
|
||||
use Closure;
|
||||
use Webkul\Core\Repositories\LocaleRepository;
|
||||
|
||||
class Locale
|
||||
{
|
||||
/**
|
||||
* @var LocaleRepository
|
||||
* Locale repository instance.
|
||||
*
|
||||
* @var \Webkul\Core\Repositories\LocaleRepository
|
||||
*/
|
||||
protected $locale;
|
||||
protected $localeRepository;
|
||||
|
||||
/**
|
||||
* @param \Webkul\Core\Repositories\LocaleRepository $locale
|
||||
* @param \Webkul\Core\Repositories\LocaleRepository $localeRepository
|
||||
*/
|
||||
public function __construct(LocaleRepository $locale)
|
||||
public function __construct(LocaleRepository $localeRepository)
|
||||
{
|
||||
$this->locale = $locale;
|
||||
$this->localeRepository = $localeRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$locale = core()->getRequestedLocaleCode('locale', false);
|
||||
if ($localeCode = core()->getRequestedLocaleCode('locale', false)) {
|
||||
if ($this->localeRepository->findOneByField('code', $localeCode)) {
|
||||
app()->setLocale($localeCode);
|
||||
|
||||
if ($locale) {
|
||||
if ($this->locale->findOneByField('code', $locale)) {
|
||||
app()->setLocale($locale);
|
||||
|
||||
session()->put('locale', $locale);
|
||||
session()->put('locale', $localeCode);
|
||||
}
|
||||
} else {
|
||||
if ($locale = session()->get('locale')) {
|
||||
app()->setLocale($locale);
|
||||
if ($localeCode = session()->get('locale')) {
|
||||
app()->setLocale($localeCode);
|
||||
} else {
|
||||
app()->setLocale(core()->getDefaultChannel()->default_locale->code);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue