diff --git a/ps_checkout.php b/ps_checkout.php index 12dd2eea6..6e8f7107c 100755 --- a/ps_checkout.php +++ b/ps_checkout.php @@ -172,7 +172,7 @@ public function install() $this->installConfiguration() && $this->installHooks() && (new PrestaShop\Module\PrestashopCheckout\Database\TableManager())->createTable() && - (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSources() && + (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSourcesOnAllShops() && $this->installTabs() && $this->disableIncompatibleCountries() && $this->disableIncompatibleCurrencies(); @@ -1292,6 +1292,7 @@ public function hookActionObjectShopAddAfter(array $params) Configuration::set($name, $value, (int) $shop->id_shop_group, (int) $shop->id); } + (new PrestaShop\Module\PrestashopCheckout\FundingSource\FundingSourceInstaller())->createFundingSources((int) $shop->id); $this->addCheckboxCarrierRestrictionsForModule([(int) $shop->id]); $this->addCheckboxCountryRestrictionsForModule([(int) $shop->id]); diff --git a/src/FundingSource/FundingSourceConfigurationRepository.php b/src/FundingSource/FundingSourceConfigurationRepository.php index c4936eaa3..ac7d67165 100644 --- a/src/FundingSource/FundingSourceConfigurationRepository.php +++ b/src/FundingSource/FundingSourceConfigurationRepository.php @@ -50,12 +50,13 @@ public function __construct(PrestaShopContext $context) /** * @param string $name + * @param int|null $shopId * * @return array|null */ - public function get($name) + public function get($name, $shopId = null) { - $fundingSources = $this->getAll(); + $fundingSources = $this->getAll($shopId); if (null === $fundingSources) { return null; @@ -71,42 +72,53 @@ public function get($name) } /** + * @param int|null $shopId + * * @return array|null */ - public function getAll() + public function getAll($shopId = null) { - if (null !== $this->fundingSources) { - return $this->fundingSources; + $shopId = (int) ($shopId === null ? $this->context->getShopId() : $shopId); + + if (isset($this->fundingSources[$shopId]) && null !== $this->fundingSources[$shopId]) { + return $this->fundingSources[$shopId]; } $data = $this->db->executeS(' SELECT `name`, `active`, `position` FROM `' . _DB_PREFIX_ . 'pscheckout_funding_source` - WHERE `id_shop` = ' . (int) $this->context->getShopId() + WHERE `id_shop` = ' . (int) $shopId ); if (!empty($data)) { - $this->fundingSources = $data; + $this->fundingSources[$shopId] = $data; } - return $this->fundingSources; + return isset($this->fundingSources[$shopId]) ? $this->fundingSources[$shopId] : null; } /** * @param array $data + * @param int|null $shopId * * @return bool + * + * @throws \PrestaShopDatabaseException */ - public function save($data) + public function save($data, $shopId = null) { - if ($this->get($data['name'])) { + $shopId = (int) ($shopId === null ? $this->context->getShopId() : $shopId); + + $this->fundingSources[$shopId] = null; + + if ($this->get($data['name'], $shopId)) { return (bool) $this->db->update( 'pscheckout_funding_source', [ 'position' => (int) $data['position'], 'active' => (int) $data['isEnabled'], ], - '`name` = "' . pSQL($data['name']) . '" AND `id_shop` = ' . (int) $this->context->getShopId() + '`name` = "' . pSQL($data['name']) . '" AND `id_shop` = ' . (int) $shopId ); } @@ -116,7 +128,7 @@ public function save($data) 'name' => pSQL($data['name']), 'position' => (int) $data['position'], 'active' => (int) $data['isEnabled'], - 'id_shop' => (int) $this->context->getShopId(), + 'id_shop' => (int) $shopId, ] ); } diff --git a/src/FundingSource/FundingSourceInstaller.php b/src/FundingSource/FundingSourceInstaller.php index e764707c1..ddc811491 100644 --- a/src/FundingSource/FundingSourceInstaller.php +++ b/src/FundingSource/FundingSourceInstaller.php @@ -27,9 +27,11 @@ class FundingSourceInstaller /** * Saves Funding Sources for the first time into the database * + * @param int|null $shopId + * * @return bool */ - public function createFundingSources() + public function createFundingSources($shopId = null) { $fundingSourceConfigurationRepository = new FundingSourceConfigurationRepository(new PrestaShopContext()); $fundingSourceCollectionBuilder = new FundingSourceCollectionBuilder( @@ -43,9 +45,20 @@ public function createFundingSources() 'name' => $fundingSourceEntity->getName(), 'position' => $fundingSourceEntity->getPosition(), 'isEnabled' => $fundingSourceEntity->getIsEnabled() ? 1 : 0, - ]); + ], $shopId); } return true; } + + public function createFundingSourcesOnAllShops() + { + $result = true; + + foreach (\Shop::getShops(false, null, true) as $shopId) { + $result &= $this->createFundingSources((int) $shopId); + } + + return $result; + } }