Elcodi\Plugin\CustomShippingBundle\EventListener\ShippingCollectEventListener::addCustomShippingMethods PHP Метод

addCustomShippingMethods() публичный Метод

Given a Cart, return a set of Valid ShippingRanges satisfied.
public addCustomShippingMethods ( Elcodi\Component\Shipping\Event\ShippingCollectionEvent $event )
$event Elcodi\Component\Shipping\Event\ShippingCollectionEvent Event
    public function addCustomShippingMethods(ShippingCollectionEvent $event)
    {
        if (!$this->plugin->isEnabled()) {
            return $this;
        }
        $cart = $event->getCart();
        $carrierRanges = $this->shippingRangesProvider->getAllShippingRangesSatisfiedWithCart($cart);
        foreach ($carrierRanges as $carrierRange) {
            $event->addShippingMethod(new ShippingMethod('custom-shipping-method-' . $carrierRange->getId(), $carrierRange->getCarrier()->getName(), $carrierRange->getName(), '', $carrierRange->getPrice()));
        }
    }

Usage Example

 /**
  * Test with matching price and weight
  */
 public function testWithMatchingPriceAndWeight()
 {
     $carrierRepository = $this->getBuiltCarrierRepository(true, 500, 600, 700, 800, 1200, 200);
     $event = $this->getEvent();
     $event->addShippingMethod(Argument::any())->shouldBeCalledTimes(1);
     $event->getCart()->willReturn($this->getCart()->reveal());
     $shippingCollectEventListener = new ShippingCollectEventListener($this->getEnabledPlugin()->reveal(), $carrierRepository->reveal(), $this->getCurrencyConverter(1)->reveal(), $this->getZoneMatcher()->reveal());
     $shippingCollectEventListener->addCustomShippingMethods($event->reveal());
 }
ShippingCollectEventListener