Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Payment] make payment description translatable #1633

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace CoreShop\Bundle\CoreBundle\Migrations;

use CoreShop\Component\Pimcore\Migration\SharedTranslation;
use Doctrine\DBAL\Schema\Schema;
use Pimcore\Migrations\Migration\AbstractPimcoreMigration;

class Version20210511074115 extends AbstractPimcoreMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
SharedTranslation::add('coreshop.order_payment.total', 'de', 'Zahlung behinhaltet %items% Einträge für Betrag %total%.');
SharedTranslation::add('coreshop.order_payment.total', 'de_CH', 'Zahlung behinhaltet %items% Einträge für Betrag %total%.');
SharedTranslation::add('coreshop.order_payment.total', 'en', 'Payment contains %items% item(s) for a total of %total%.');
SharedTranslation::add('coreshop.order_payment.total', 'it', 'Il pagamento contiene %items% voce/i per un totale di %total%.');
}

/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ private function addPimcoreResourcesSection(ArrayNodeDefinition $node)
->scalarPrototype()->end()
->defaultValue(['@CoreShopOrderBundle/Resources/install/pimcore/grid-config.yml'])
->end()
->arrayNode('translations')
->treatNullLike([])
->scalarPrototype()->end()
->defaultValue(['@CoreShopOrderBundle/Resources/install/pimcore/translations.yml'])
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ services:
- '@coreshop.factory.payment'
- '%coreshop.currency.decimal_factor%'
- '%coreshop.currency.decimal_precision%'
- '@translator'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
translations:
coreshop.order_payment.total:
languages:
en: 'Payment contains %items% item(s) for a total of %total%.'
de: 'Zahlung behinhaltet %items% Einträge für Betrag %total%.'
de_CH: 'Zahlung behinhaltet %items% Einträge für Betrag %total%.'
it: 'Il pagamento contiene %items% voce/i per un totale di %total%.'
43 changes: 33 additions & 10 deletions src/CoreShop/Component/Order/Payment/OrderPaymentProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

namespace CoreShop\Component\Order\Payment;

use CoreShop\Component\Payment\Model\PaymentInterface;
use CoreShop\Component\Order\Model\OrderInterface;
use CoreShop\Component\Order\Model\OrderPaymentInterface;
use CoreShop\Component\Payment\Model\PaymentInterface;
use CoreShop\Component\Payment\Model\PaymentSettingsAwareInterface;
use CoreShop\Component\Resource\Factory\FactoryInterface;
use CoreShop\Component\Resource\TokenGenerator\UniqueTokenGenerator;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Model\Payment;
use Symfony\Component\Translation\TranslatorInterface;

class OrderPaymentProvider implements OrderPaymentProviderInterface
{
Expand All @@ -39,15 +40,27 @@ class OrderPaymentProvider implements OrderPaymentProviderInterface
private $decimalPrecision;

/**
* @param FactoryInterface $paymentFactory
* @param int $decimalFactor
* @param int $decimalPrecision
* @var TranslatorInterface|null
*/
private $translator;

/**
* @param FactoryInterface $paymentFactory
* @param int $decimalFactor
* @param int $decimalPrecision
* @param TranslatorInterface|null $translator
*/
public function __construct(FactoryInterface $paymentFactory, int $decimalFactor, int $decimalPrecision)
public function __construct(
FactoryInterface $paymentFactory,
int $decimalFactor,
int $decimalPrecision,
TranslatorInterface $translator = null
)
{
$this->paymentFactory = $paymentFactory;
$this->decimalFactor = $decimalFactor;
$this->decimalPrecision = $decimalPrecision;
$this->translator = $translator;
}

/**
Expand Down Expand Up @@ -78,11 +91,21 @@ public function provideOrderPayment(OrderInterface $order)
$payment->setOrder($order);
}

$description = sprintf(
'Payment contains %s item(s) for a total of %s.',
count($order->getItems()),
round($order->getTotal() / $this->decimalFactor, $this->decimalPrecision)
);
if (null !== $this->translator) {
$description = $this->translator->trans(
'coreshop.order_payment.total',
[
'%items%' => count($order->getItems()),
'%total%' => round($order->getTotal() / $this->decimalFactor, $this->decimalPrecision),
]
);
} else {
$description = sprintf(
'Payment contains %s item(s) for a total of %s.',
count($order->getItems()),
round($order->getTotal() / $this->decimalFactor, $this->decimalPrecision)
);
}

//payum setters
if ($payment instanceof Payment) {
Expand Down