lib/composer/OrderBundle/src/VisualMedia/OrderBundle/Twig/ShippingExtension.php line 77

Open in your IDE?
  1. <?php
  2. /**
  3. * ShippingExtension
  4. *
  5. * @author Bertin van den Ham <b.vandenham@visualmedia.nl>
  6. */
  7. namespace VisualMedia\OrderBundle\Twig;
  8. use Twig\Extension\AbstractExtension;
  9. use Twig\TwigFilter;
  10. use Twig\TwigFunction;
  11. use VisualMedia\OrderBundle\Component\Interfaces\ShippingMethodInterface;
  12. use VisualMedia\OrderBundle\Component\ShippingMethod;
  13. use VisualMedia\OrderBundle\Provider\ShippingMethodProvider;
  14. use VisualMedia\OrderBundle\Service\Cart;
  15. use VisualMedia\OrderBundle\ShippingMethod\Delivery;
  16. /**
  17. * ShippingExtension
  18. */
  19. class ShippingExtension extends AbstractExtension
  20. {
  21. /**
  22. * ShippingMethodProvider
  23. *
  24. * @var ShippingMethodProvider
  25. */
  26. protected $shippingMethodProvider;
  27. /**
  28. * Constructor
  29. *
  30. * @param Cart $cart
  31. */
  32. public function __construct(ShippingMethodProvider $shippingMethodProvider)
  33. {
  34. $this->shippingMethodProvider = $shippingMethodProvider;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getFunctions()
  40. {
  41. return [
  42. new TwigFunction('shipping_costs', array($this, 'costs')),
  43. new TwigFunction('shipping_costs_by_shipment', array($this, 'costsByDifferentShippings')),
  44. ];
  45. }
  46. /**
  47. * Cart Amount
  48. *
  49. * @return int
  50. */
  51. public function costs($order): float
  52. {
  53. // Handle shipment costs
  54. if(null !== $shipping = $this->shippingMethodProvider->getEnabled($order->getShippingMethod())) {
  55. $shippingCosts = $shipping->getCosts([
  56. 'order' => $order
  57. ]);
  58. return $shippingCosts;
  59. }
  60. return 0;
  61. }
  62. /**
  63. * @param ShippingMethod $shippingMethod
  64. *
  65. * @return float|int
  66. */
  67. public function costsByDifferentShippings($order, ShippingMethodInterface $shippingMethod = null) {
  68. if($shippingMethod !== null) {
  69. return $shippingMethod->getCosts([
  70. 'order' => $order
  71. ]);
  72. }
  73. return 0;
  74. }
  75. }