Example #1
0
 /**
  * Validate and sanitize input values.
  *
  * @param array $settings Input fields.
  *
  * @return array Sanitized and validated output.
  * @throws ValidationException When some items are not valid.
  */
 public function validate($settings)
 {
     $settings['enabled'] = $settings['enabled'] == 'on';
     $settings['calculator'] = $settings['calculator'] == 'on';
     $settings['only_to_billing'] = $settings['only_to_billing'] == 'on';
     $settings['always_show_shipping'] = $settings['always_show_shipping'] == 'on';
     foreach ($this->shippingService->getAvailable() as $method) {
         /** @var $method Method */
         $settings[$method->getId()] = $method->validateOptions($settings[$method->getId()]);
     }
     return $settings;
 }
Example #2
0
 /**
  * Abstraction for cart update response.
  *
  * Prepares and returns response array for cart update requests.
  *
  * @param CartEntity $cart Current cart.
  *
  * @return array
  */
 private function getAjaxCartResponse(CartEntity $cart)
 {
     $tax = array();
     foreach ($cart->getCombinedTax() as $class => $value) {
         $tax[$class] = array('label' => Tax::getLabel($class, $cart), 'value' => ProductHelper::formatPrice($value));
     }
     $shipping = array();
     $shippingHtml = array();
     foreach ($this->shippingService->getAvailable() as $method) {
         /** @var $method Method */
         if ($method instanceof MultipleMethod) {
             /** @var $method MultipleMethod */
             foreach ($method->getRates($cart) as $rate) {
                 /** @var $rate Rate */
                 $shipping[$method->getId() . '-' . $rate->getId()] = $method->isEnabled() ? $rate->calculate($cart) : -1;
                 if ($method->isEnabled()) {
                     $shippingHtml[$method->getId() . '-' . $rate->getId()] = array('price' => ProductHelper::formatPrice($rate->calculate($cart)), 'html' => Render::get('shop/cart/shipping/rate', array('method' => $method, 'rate' => $rate, 'cart' => $cart)));
                 }
             }
         } else {
             $shipping[$method->getId()] = $method->isEnabled() ? $method->calculate($cart) : -1;
             if ($method->isEnabled()) {
                 $shippingHtml[$method->getId()] = array('price' => ProductHelper::formatPrice($method->calculate($cart)), 'html' => Render::get('shop/cart/shipping/method', array('method' => $method, 'cart' => $cart)));
             }
         }
     }
     $response = array('success' => true, 'shipping' => $shipping, 'subtotal' => $cart->getSubtotal(), 'product_subtotal' => $cart->getProductSubtotal(), 'tax' => $cart->getCombinedTax(), 'total' => $cart->getTotal(), 'html' => array('shipping' => $shippingHtml, 'subtotal' => ProductHelper::formatPrice($cart->getSubtotal()), 'product_subtotal' => ProductHelper::formatPrice($cart->getProductSubtotal()), 'tax' => $tax, 'total' => ProductHelper::formatPrice($cart->getTotal())));
     return $response;
 }
Example #3
0
 /**
  * @param $order OrderInterface Order to get values from.
  *
  * @return array Ajax response array.
  */
 private function getAjaxResponse($order)
 {
     $tax = $order->getTax();
     $shippingTax = $order->getShippingTax();
     foreach ($order->getTax() as $class => $value) {
         if (isset($shippingTax[$class])) {
             $tax[$class] = $value + $shippingTax[$class];
         }
     }
     $shipping = array();
     $shippingHtml = array();
     foreach ($this->shippingService->getAvailable() as $method) {
         /** @var $method Shipping\Method */
         if ($method instanceof Shipping\MultipleMethod) {
             /** @var $method Shipping\MultipleMethod */
             foreach ($method->getRates($order) as $rate) {
                 /** @var $rate Shipping\Rate */
                 $shipping[$method->getId() . '-' . $rate->getId()] = $method->isEnabled() ? $rate->calculate($order) : -1;
                 if ($method->isEnabled()) {
                     $shippingHtml[$method->getId() . '-' . $rate->getId()] = array('price' => ProductHelper::formatPrice($rate->calculate($order)), 'html' => Render::get('admin/order/totals/shipping/rate', array('method' => $method, 'rate' => $rate, 'order' => $order)));
                 }
             }
         } else {
             $shipping[$method->getId()] = $method->isEnabled() ? $method->calculate($order) : -1;
             if ($method->isEnabled()) {
                 $shippingHtml[$method->getId()] = array('price' => ProductHelper::formatPrice($method->calculate($order)), 'html' => Render::get('admin/order/totals/shipping/method', array('method' => $method, 'order' => $order)));
             }
         }
     }
     return array('success' => true, 'shipping' => $shipping, 'product_subtotal' => $order->getProductSubtotal(), 'subtotal' => $order->getSubtotal(), 'total' => $order->getTotal(), 'tax' => $tax, 'html' => array('shipping' => $shippingHtml, 'product_subtotal' => ProductHelper::formatPrice($order->getProductSubtotal()), 'subtotal' => ProductHelper::formatPrice($order->getSubtotal()), 'total' => ProductHelper::formatPrice($order->getTotal()), 'tax' => $this->getTaxes($order)));
 }
Example #4
0
 /**
  * Abstraction for cart update response.
  * Prepares and returns response array for cart update requests.
  *
  * @param \Jigoshop\Entity\Cart $cart Current cart.
  *
  * @return array
  */
 private function getAjaxCartResponse(\Jigoshop\Entity\Cart $cart)
 {
     $tax = array();
     foreach ($cart->getCombinedTax() as $class => $value) {
         $tax[$class] = array('label' => Tax::getLabel($class, $cart), 'value' => Product::formatPrice($value));
     }
     $shipping = array();
     $shippingHtml = array();
     foreach ($this->shippingService->getAvailable() as $method) {
         /** @var $method Method */
         if ($method instanceof MultipleMethod) {
             /** @var $method MultipleMethod */
             foreach ($method->getRates($cart) as $rate) {
                 /** @var $rate Rate */
                 $shipping[$method->getId() . '-' . $rate->getId()] = $method->isEnabled() ? $rate->calculate($cart) : -1;
                 if ($method->isEnabled()) {
                     $shippingHtml[$method->getId() . '-' . $rate->getId()] = array('price' => Product::formatPrice($rate->calculate($cart)), 'html' => Render::get('shop/cart/shipping/rate', array('method' => $method, 'rate' => $rate, 'cart' => $cart)));
                 }
             }
         } else {
             $shipping[$method->getId()] = $method->isEnabled() ? $method->calculate($cart) : -1;
             if ($method->isEnabled()) {
                 $shippingHtml[$method->getId()] = array('price' => Product::formatPrice($cart->getShippingPrice()), 'html' => Render::get('shop/cart/shipping/method', array('method' => $method, 'cart' => $cart)));
             }
         }
     }
     $shippingMethod = $cart->getShippingMethod();
     if ($shippingMethod) {
         try {
             $cart->setShippingMethod($shippingMethod);
         } catch (Exception $e) {
             $cart->removeShippingMethod();
         }
     }
     $productSubtotal = $this->options->get('tax.price_tax') == 'with_tax' ? $cart->getProductSubtotal() + $cart->getTotalTax() : $cart->getProductSubtotal();
     $coupons = join(',', array_map(function ($coupon) {
         /** @var $coupon Coupon */
         return $coupon->getCode();
     }, $cart->getCoupons()));
     $response = array('success' => true, 'shipping' => $shipping, 'subtotal' => $cart->getSubtotal(), 'product_subtotal' => $productSubtotal, 'discount' => $cart->getDiscount(), 'coupons' => $coupons, 'tax' => $cart->getCombinedTax(), 'total' => $cart->getTotal(), 'html' => array('shipping' => $shippingHtml, 'discount' => Product::formatPrice($cart->getDiscount()), 'subtotal' => Product::formatPrice($cart->getSubtotal()), 'product_subtotal' => Product::formatPrice($productSubtotal), 'tax' => $tax, 'total' => Product::formatPrice($cart->getTotal())));
     return $response;
 }