Skip to content

Commit

Permalink
feat: add digital stamp weight range (#801)
Browse files Browse the repository at this point in the history
  • Loading branch information
wthijmen authored Dec 21, 2023
1 parent dd98a32 commit 696b771
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
3 changes: 1 addition & 2 deletions Model/Source/DigitalStampWeightOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public function toOptionArray()
['value' => 0, 'label' => __('No standard weight')],
['value' => 20, 'label' => __('0 - 20 gram')],
['value' => 50, 'label' => __('20 - 50 gram')],
['value' => 100, 'label' => __('50 - 100 gram')],
['value' => 350, 'label' => __('100 - 350 gram')],
['value' => 200, 'label' => __('50 - 350 gram')],
['value' => 2000, 'label' => __('350 - 2000 gram')]
];

Expand Down
72 changes: 72 additions & 0 deletions Setup/Migrations/ReplaceDpzRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\Magento\Setup\Migrations;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Setup\SchemaSetupInterface;
use MyParcelNL\Magento\Setup\QueryBuilder;

class ReplaceDpzRange
{
/** @var string $pathDigitalStampDefault */
private $pathDigitalStampDefault = 'myparcelnl_magento_postnl_settings/digital_stamp/default_weight';

/** @var \MyParcelNL\Magento\Setup\QueryBuilder $queryBuilder */
private $queryBuilder;

/** @var \Magento\Framework\Setup\SchemaSetupInterface $setup */
private $setup;

/** @var array $replaceValues */
private $replaceValues = [
'100',
'350',
];

/** @var $newValue */
private $newValue = '200';

public function __construct(
QueryBuilder $queryBuilder,
SchemaSetupInterface $setup
) {
$this->queryBuilder = $queryBuilder;
$this->setup = $setup;
}

/**
* @return object
*/
public function resourceConnection(): object
{
$objectManager = ObjectManager::getInstance();

return $objectManager->get('Magento\Framework\App\ResourceConnection')
->getConnection();
}

/**
* @throws \Zend_Db_Exception
*/
public function updateRangeValue(): void
{
$connection = $this->resourceConnection();

/** @var $query */
$query = $this->queryBuilder
->select('path', 'value')
->from($this->setup->getTable('core_config_data'))
->where(sprintf('path = \'%s\'', $this->pathDigitalStampDefault));

$result = $connection->fetchRow($query);
if ($result && in_array($result['value'], $this->replaceValues, true)) {
$query = $this->queryBuilder
->update($this->setup->getTable('core_config_data'))
->set('value', $this->newValue)
->where(sprintf('path = \'%s\'', $this->pathDigitalStampDefault));
$connection->query($query);
}
}
}
15 changes: 14 additions & 1 deletion Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
use MyParcelNL\Magento\Setup\Migrations\ReplaceDpzRange;
use MyParcelNL\Magento\Setup\Migrations\ReplaceFitInMailbox;
use MyParcelNL\Magento\Setup\Migrations\ReplaceDisableCheckout;
use MyParcelNL\Magento\Model\Source\FitInMailboxOptions;
Expand Down Expand Up @@ -83,22 +84,30 @@ class UpgradeData implements UpgradeDataInterface
*/
private $replaceDisableCheckout;

/**
* @var \MyParcelNL\Magento\Setup\Migrations\ReplaceDpzRange
*/
private $replaceDpzRange;

/**
* @param \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
* @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
* @param \MyParcelNL\Magento\Setup\Migrations\ReplaceFitInMailbox $replaceFitInMailbox
* @param \MyParcelNL\Magento\Setup\Migrations\ReplaceDisableCheckout $replaceDisableCheckout
* @param \MyParcelNL\Magento\Setup\Migrations\ReplaceDpzRange $replaceDpzRange
*/
public function __construct(
\Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory,
EavSetupFactory $eavSetupFactory,
ReplaceFitInMailbox $replaceFitInMailbox,
ReplaceDisableCheckout $replaceDisableCheckout
ReplaceDisableCheckout $replaceDisableCheckout,
ReplaceDpzRange $replaceDpzRange
) {
$this->categorySetupFactory = $categorySetupFactory;
$this->eavSetupFactory = $eavSetupFactory;
$this->replaceFitInMailbox = $replaceFitInMailbox;
$this->replaceDisableCheckout = $replaceDisableCheckout;
$this->replaceDpzRange = $replaceDpzRange;
}

/**
Expand Down Expand Up @@ -711,6 +720,10 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
);
}

if (version_compare($context->getVersion(), '4.12.0', '<')) {
$this->replaceDpzRange->updateRangeValue();
}

$setup->endSetup();
}
}

0 comments on commit 696b771

Please sign in to comment.