-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
[Core][Shipping] Fix shipping method choice type #5216
Changes from 3 commits
838ad29
0126812
64c3676
d0ee908
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,6 +121,8 @@ | |
<parameter key="sylius.integer_distributor.class">Sylius\Component\Core\Distributor\IntegerDistributor</parameter> | ||
<parameter key="sylius.proportional_integer_distributor.class">Sylius\Component\Core\Distributor\ProportionalIntegerDistributor</parameter> | ||
|
||
<parameter key="sylius.shipping_methods_resolver.by_zones_and_channel.class">Sylius\Component\Core\Resolver\ShippingMethodsByZonesAndChannelResolver</parameter> | ||
|
||
<!-- Data fetchers --> | ||
<parameter key="sylius.form.type.data_fetcher.number_of_orders.class">Sylius\Bundle\CoreBundle\Form\Type\DataFetcher\NumberOfOrdersType</parameter> | ||
<parameter key="sylius.form.type.data_fetcher.sales_total.class">Sylius\Bundle\CoreBundle\Form\Type\DataFetcher\SalesTotalType</parameter> | ||
|
@@ -622,6 +624,12 @@ | |
<argument type="service" id="sylius.sql_logger.purger" /> | ||
</service> | ||
|
||
<service id="sylius.shipping_methods_resolver.by_zones_and_channel" class="%sylius.shipping_methods_resolver.by_zones_and_channel.class%"> | ||
<argument type="service" id="sylius.repository.shipping_method" /> | ||
<argument type="service" id="sylius.zone_matcher" /> | ||
<tag name="sylius.shipping_method_resolver" type="by_zones_and_channel" label="By zones and channel" priority="1" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Label should be a translation key: |
||
</service> | ||
|
||
<!-- Data fetchers --> | ||
<service id="sylius.form.type.data_fetcher.user_registration" class="%sylius.form.type.data_fetcher.user_registration.class%"> | ||
<tag name="form.type" alias="sylius_data_fetcher_user_registration" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> | ||
*/ | ||
class RegisterShippingMethodsResolverPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasDefinition('sylius.registry.shipping_methods_resolver')) { | ||
return; | ||
} | ||
|
||
$registry = $container->findDefinition('sylius.registry.shipping_methods_resolver'); | ||
$resolvers = []; | ||
|
||
foreach ($container->findTaggedServiceIds('sylius.shipping_method_resolver') as $id => $attributes) { | ||
if (!isset($attributes[0]['type']) || !isset($attributes[0]['label'])) { | ||
throw new \InvalidArgumentException('Tagged shipping methods resolvers need to have `type` and `label` attributes.'); | ||
} | ||
|
||
$priority = isset($attributes[0]['priority']) ? (int) $attributes[0]['priority'] : 0; | ||
|
||
$resolvers[$attributes[0]['type']] = $attributes[0]['label']; | ||
|
||
$registry->addMethodCall('register', [new Reference($id), $priority]); | ||
} | ||
|
||
$container->setParameter('sylius.shipping_method_resolvers', $resolvers); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
namespace Sylius\Bundle\ShippingBundle\Form\Type; | ||
|
||
use Sylius\Component\Registry\PrioritizedServiceRegistryInterface; | ||
use Sylius\Component\Registry\ServiceRegistryInterface; | ||
use Sylius\Component\Resource\Repository\RepositoryInterface; | ||
use Sylius\Component\Shipping\Model\ShippingMethodInterface; | ||
|
@@ -35,11 +36,9 @@ | |
class ShippingMethodChoiceType extends AbstractType | ||
{ | ||
/** | ||
* Supported methods resolver. | ||
* | ||
* @var MethodsResolverInterface | ||
* @var PrioritizedServiceRegistryInterface | ||
*/ | ||
protected $resolver; | ||
protected $resolversRegistry; | ||
|
||
/** | ||
* @var ServiceRegistryInterface | ||
|
@@ -52,16 +51,16 @@ class ShippingMethodChoiceType extends AbstractType | |
protected $repository; | ||
|
||
/** | ||
* @param MethodsResolverInterface $resolver | ||
* @param ServiceRegistryInterface $calculators | ||
* @param RepositoryInterface $repository | ||
* @param PrioritizedServiceRegistryInterface $resolversRegistry | ||
* @param ServiceRegistryInterface $calculators | ||
* @param RepositoryInterface $repository | ||
*/ | ||
public function __construct( | ||
MethodsResolverInterface $resolver, | ||
PrioritizedServiceRegistryInterface $resolversRegistry, | ||
ServiceRegistryInterface $calculators, | ||
RepositoryInterface $repository | ||
) { | ||
$this->resolver = $resolver; | ||
$this->resolversRegistry = $resolversRegistry; | ||
$this->calculators = $calculators; | ||
$this->repository = $repository; | ||
} | ||
|
@@ -82,10 +81,16 @@ public function buildForm(FormBuilderInterface $builder, array $options) | |
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$choiceList = function (Options $options) { | ||
$methods = $methods = $this->repository->findAll(); | ||
|
||
if (isset($options['subject'])) { | ||
$methods = $this->resolver->getSupportedMethods($options['subject'], $options['criteria']); | ||
} else { | ||
$methods = $this->repository->findBy($options['criteria']); | ||
/** @var MethodsResolverInterface $resolver */ | ||
foreach ($this->resolversRegistry->all() as $resolver) { | ||
if ($resolver->supports($options['subject'])) { | ||
$methods = $resolver->getSupportedMethods($options['subject']); | ||
break; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this logic to the "main" methods resolver and inject it here. It will support every shipment and use exactly this logic to get the right methods. (CompositeMethodsResolver as name, wdyt?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure 👍 Less logic in form type is always a good choice 😄 |
||
} | ||
|
||
return new ObjectChoiceList($methods, null, [], null, 'id'); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,9 @@ | |
|
||
<parameter key="sylius.registry.shipping_rule_checker.class">Sylius\Component\Registry\ServiceRegistry</parameter> | ||
|
||
<parameter key="sylius.registry.shipping_methods_resolver.class">Sylius\Component\Registry\PrioritizedServiceRegistry</parameter> | ||
<parameter key="sylius.shipping_methods_resolver.interface">Sylius\Component\Shipping\Resolver\MethodsResolverInterface</parameter> | ||
|
||
<parameter key="sylius.shipping_eligibility_checker.class">Sylius\Component\Shipping\Checker\ShippingMethodEligibilityChecker</parameter> | ||
<parameter key="sylius.shipping_rule_checker.unit_count.class">Sylius\Component\Shipping\Checker\UnitCountRuleChecker</parameter> | ||
|
||
|
@@ -67,12 +70,18 @@ | |
<argument>shipping rule checker</argument> | ||
</service> | ||
|
||
<service id="sylius.registry.shipping_methods_resolver" class="%sylius.registry.shipping_methods_resolver.class%"> | ||
<argument>%sylius.shipping_methods_resolver.interface%</argument> | ||
<argument>Shipping methods resolver</argument> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to service implementation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine for me, but then let's lowercase the first letter 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @michalmarcinkowski This is for exception message, so the first uppercase letter is ok. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 I recall that PR :) |
||
</service> | ||
|
||
<service id="sylius.shipping_methods_resolver" class="%sylius.shipping_methods_resolver.class%"> | ||
<argument type="service" id="sylius.repository.shipping_method" /> | ||
<argument type="service" id="sylius.shipping_eligibility_checker" /> | ||
<tag name="sylius.shipping_method_resolver" type="default" label="Default" /> | ||
</service> | ||
<service id="sylius.form.type.shipping_method_choice" class="%sylius.form.type.shipping_method_choice.class%"> | ||
<argument type="service" id="sylius.shipping_methods_resolver" /> | ||
<argument type="service" id="sylius.registry.shipping_methods_resolver" /> | ||
<argument type="service" id="sylius.registry.shipping_calculator" /> | ||
<argument type="service" id="sylius.repository.shipping_method" /> | ||
<tag name="form.type" alias="sylius_shipping_method_choice" /> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle; | ||
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterCalculatorsPass; | ||
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterRuleCheckersPass; | ||
use Sylius\Bundle\ShippingBundle\DependencyInjection\Compiler\RegisterShippingMethodsResolverPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
|
@@ -47,6 +48,7 @@ public function build(ContainerBuilder $container) | |
|
||
$container->addCompilerPass(new RegisterCalculatorsPass()); | ||
$container->addCompilerPass(new RegisterRuleCheckersPass()); | ||
$container->addCompilerPass(new RegisterShippingMethodsResolverPass()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually use plural form here: Resolvers. |
||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
if
should be before$this->shippingMethodRepository->add($shippingMethod);
. Right now the relation is not flushed in this step.