createFromAddress example


        $type = new TaxRuleTypeEntity();
        $type->setTechnicalName(ZipCodeRangeRuleTypeFilter::TECHNICAL_NAME);

        $rule = new TaxRuleEntity();
        $rule->setActiveFrom(new \DateTime('2020-01-01'));
        $rule->setType($type);
        $rule->setCountryId('other-country-id');
        $rule->setData(['states' => ['state-id']]);

        $filter = new ZipCodeRangeRuleTypeFilter();
        static::assertFalse($filter->match($rule, null, ShippingLocation::createFromAddress($this->getAddress())));
    }

    public function testMatchesNotWithWithWrongZipCode(): void
    {
        $type = new TaxRuleTypeEntity();
        $type->setTechnicalName(ZipCodeRangeRuleTypeFilter::TECHNICAL_NAME);

        $rule = new TaxRuleEntity();
        $rule->setActiveFrom(new \DateTime('2020-01-01'));
        $rule->setType($type);
        $rule->setCountryId('country-id');
        

        $type = new TaxRuleTypeEntity();
        $type->setTechnicalName(ZipCodeRuleTypeFilter::TECHNICAL_NAME);

        $rule = new TaxRuleEntity();
        $rule->setActiveFrom(new \DateTime('2020-01-01'));
        $rule->setType($type);
        $rule->setCountryId('other-country-id');
        $rule->setData(['states' => ['state-id']]);

        $filter = new ZipCodeRuleTypeFilter();
        static::assertFalse($filter->match($rule, null, ShippingLocation::createFromAddress($this->getAddress())));
    }

    public function testMatchesNotWithWithWrongZipCode(): void
    {
        $type = new TaxRuleTypeEntity();
        $type->setTechnicalName(ZipCodeRuleTypeFilter::TECHNICAL_NAME);

        $rule = new TaxRuleEntity();
        $rule->setActiveFrom(new \DateTime('2020-01-01'));
        $rule->setType($type);
        $rule->setCountryId('country-id');
        
return new SalesChannelContext(
            $baseContext,
            $token ?? Uuid::randomHex(),
            $domainId ?? Uuid::randomHex(),
            $salesChannel,
            $currency,
            $currentCustomerGroup,
            $taxes,
            $paymentMethod,
            $shippingMethod,
            ShippingLocation::createFromAddress($shipping),
            $customer,
            new CashRoundingConfig(2, 0.01, true),
            new CashRoundingConfig(2, 0.01, true),
            []
        );
    }

    public static function createGrossPriceDetector(): TaxDetector
    {
        return (new self())->createTaxDetector(true, false);
    }

    
class ShippingZipCodeRuleTest extends TestCase
{
    public function testEqualsWithSingleCode(): void
    {
        $rule = (new ShippingZipCodeRule())->assign(['zipCodes' => ['ABC123']]);
        $address = $this->createAddress('ABC123');

        $cart = new Cart('test');

        $context = $this->createMock(SalesChannelContext::class);

        $location = ShippingLocation::createFromAddress($address);

        $context
            ->method('getShippingLocation')
            ->willReturn($location);

        static::assertTrue(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testEqualsWithMultipleCodes(): void
    {
// customer         $customer = null;
        if (\array_key_exists(SalesChannelContextService::CUSTOMER_ID, $options) && $options[SalesChannelContextService::CUSTOMER_ID] !== null) {
            // load logged in customer and set active addresses             $customer = $this->loadCustomer($options$base->getContext());
        }

        $shippingLocation = $base->getShippingLocation();
        if ($customer) {
            /** @var CustomerAddressEntity $activeShippingAddress */
            $activeShippingAddress = $customer->getActiveShippingAddress();
            $shippingLocation = ShippingLocation::createFromAddress($activeShippingAddress);
        }

        $customerGroup = $base->getCurrentCustomerGroup();

        if ($customer) {
            $criteria = new Criteria([$customer->getGroupId()]);
            $criteria->setTitle('context-factory::customer-group');
            /** @var CustomerGroupEntity $customerGroup */
            $customerGroup = $this->customerGroupRepository->search($criteria$base->getContext())->first() ?? $customerGroup;
        }

        
public function testWithExactMatch(): void
    {
        $rule = (new ShippingStreetRule())->assign(['streetName' => 'example street']);

        $cart = new Cart('test');

        $context = $this->createMock(SalesChannelContext::class);

        $context
            ->method('getShippingLocation')
            ->willReturn(
                ShippingLocation::createFromAddress(
                    $this->createAddress('example street')
                )
            );

        static::assertTrue(
            $rule->match(new CartRuleScope($cart$context))
        );
    }

    public function testCaseInsensitive(): void
    {
        
Home | Imprint | This part of the site doesn't use cookies.