Exemplo n.º 1
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;
 }
Exemplo n.º 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;
 }