protected function getCheapestDelivery(Address $address = null, Country $country = null)
    {
        $cart = $this->getCurrentRequest()->getSession()->getSessionCart();

        $deliveryModules = ModuleQuery::create()
            ->filterByActivate(1)
            ->filterByType(BaseModule::DELIVERY_MODULE_TYPE, Criteria::EQUAL)
            ->find()
        ;

        $virtual = $cart->isVirtual();

        /** @var \Thelia\Model\Module $deliveryModule */
        foreach ($deliveryModules as $deliveryModule) {
          $areaDeliveryModule = AreaDeliveryModuleQuery::create()
              ->findByCountryAndModule($country, $deliveryModule, $state);
          if (null === $areaDeliveryModule && false === $virtual) {
              continue;
          }

          $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);

          if (true === $virtual
            && false === $moduleInstance->handleVirtualProductDelivery()
            ) {
            continue;
          }

            try {
                $deliveryPostageEvent = new DeliveryPostageEvent($moduleInstance, $cart, $address, $country, $state);
                $this->dispatcher->dispatch(
                    TheliaEvents::MODULE_DELIVERY_GET_POSTAGE,
                    $deliveryPostageEvent
                );

                if ($deliveryPostageEvent->isValidModule()) {
                    $postage = $deliveryPostageEvent->getPostage();

                    if (null === $this->postage || $this->postage > $postage->getAmount()) {
                        $this->postage = $postage->getAmount();
                        $this->postageTax = $postage->getAmountTax();
                        $this->postageTaxRuleTitle = $postage->getTaxRuleTitle();
                        $this->deliveryId = $deliveryModule->getId();
                    }
                }
            } catch (DeliveryException $ex) {
                // Module is not available
            }
        }
    }
protected function getCheapestDelivery(Address $address = null, Country $country = null)
    {
        $cart = $this->getCurrentRequest()->getSession()->getSessionCart();

        $deliveryModules = ModuleQuery::create()
            ->filterByActivate(1)
            ->filterByType(BaseModule::DELIVERY_MODULE_TYPE, Criteria::EQUAL)
            ->find()
        ;

        $virtual = $cart->isVirtual();

        /** @var \Thelia\Model\Module $deliveryModule */
        foreach ($deliveryModules as $deliveryModule) {
          $areaDeliveryModule = AreaDeliveryModuleQuery::create()
              ->findByCountryAndModule($country, $deliveryModule, $state);
          if (null === $areaDeliveryModule && false === $virtual) {
              continue;
          }

          $moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);

          if (true === $virtual
            && false === $moduleInstance->handleVirtualProductDelivery()
            ) {
            continue;
          }

            try {
                $deliveryPostageEvent = new DeliveryPostageEvent($moduleInstance, $cart, $address, $country, $state);
                $this->dispatcher->dispatch(
                    TheliaEvents::MODULE_DELIVERY_GET_POSTAGE,
                    $deliveryPostageEvent
                );

                if ($deliveryPostageEvent->isValidModule()) {
                    $postage = $deliveryPostageEvent->getPostage();

                    if (null === $this->postage || $this->postage > $postage->getAmount()) {
                        $this->postage = $postage->getAmount();
                        $this->postageTax = $postage->getAmountTax();
                        $this->postageTaxRuleTitle = $postage->getTaxRuleTitle();
                        $this->deliveryId = $deliveryModule->getId();
                    }
                }
            } catch (DeliveryException $ex) {
                // Module is not available
            }
        }
    }
protected function getCheapestDelivery ( Address $ address = null , Country $ country = null ) { $ cart = $ this -> getCurrentRequest ( ) -> getSession ( ) -> getSessionCart ( ) ; $ deliveryModules = ModuleQuery :: create ( ) -> filterByActivate ( 1 ) -> filterByType ( BaseModule :: DELIVERY_MODULE_TYPE , Criteria :: EQUAL ) -> find ( ) ; $ virtual = $ cart -> isVirtual ( ) ; /** @var \Thelia\Model\Module $deliveryModule */ foreach ( $ deliveryModules as $ deliveryModule ) { $ areaDeliveryModule = AreaDeliveryModuleQuery :: create ( ) -> findByCountryAndModule ( $ country , $ deliveryModule , $ state ) ; if ( null === $ areaDeliveryModule && false === $ virtual ) { continue ; } $ moduleInstance = $ deliveryModule -> getDeliveryModuleInstance ( $ this -> container ) ; if ( true === $ virtual && false === $ moduleInstance -> handleVirtualProductDelivery ( ) ) { continue ; } try { $ deliveryPostageEvent = new DeliveryPostageEvent ( $ moduleInstance , $ cart , $ address , $ country , $ state ) ; $ this -> dispatcher -> dispatch ( TheliaEvents :: MODULE_DELIVERY_GET_POSTAGE , $ deliveryPostageEvent ) ; if ( $ deliveryPostageEvent -> isValidModule ( ) ) { $ postage = $ deliveryPostageEvent -> getPostage ( ) ; if ( null === $ this -> postage || $ this -> postage > $ postage -> getAmount ( ) ) { $ this -> postage = $ postage -> getAmount ( ) ; $ this -> postageTax = $ postage -> getAmountTax ( ) ; $ this -> postageTaxRuleTitle = $ postage -> getTaxRuleTitle ( ) ; $ this -> deliveryId = $ deliveryModule -> getId ( ) ; } } } catch ( DeliveryException $ ex ) { // Module is not available } } }
Retrieve the cheapest delivery for country

@param Address $address
@param \Thelia\Model\Country $country
@return DeliveryModuleInterface
Retrieve the cheapest delivery for country