Skip to content

Commit

Permalink
Fix funding source list with multishop
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt75 committed Aug 23, 2024
1 parent c889531 commit 032744d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
3 changes: 2 additions & 1 deletion ps_checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]);

Expand Down
36 changes: 24 additions & 12 deletions src/FundingSource/FundingSourceConfigurationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);
}

Expand All @@ -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,
]
);
}
Expand Down
17 changes: 15 additions & 2 deletions src/FundingSource/FundingSourceInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
}

0 comments on commit 032744d

Please sign in to comment.