Exemplo n.º 1
0
 /**
  * Processes updates of coupons and returns updated cart details.
  */
 public function ajaxUpdateDiscounts()
 {
     try {
         $cart = $this->cartService->getCurrent();
         if (isset($_POST['coupons'])) {
             $errors = array();
             $codes = array_filter(explode(',', $_POST['coupons']));
             $cart->removeAllCouponsExcept($codes);
             $coupons = $this->couponService->getByCodes($codes);
             foreach ($coupons as $coupon) {
                 try {
                     $cart->addCoupon($coupon);
                 } catch (Exception $e) {
                     $errors[] = $e->getMessage();
                 }
             }
             if (!empty($errors)) {
                 throw new Exception(join('<br/>', $errors));
             }
         }
         // TODO: Add support for other discounts
         $this->cartService->save($cart);
         $response = $this->getAjaxCartResponse($cart);
     } catch (Exception $e) {
         $response = array('success' => false, 'error' => $e->getMessage());
     }
     echo json_encode($response);
     exit;
 }
Exemplo n.º 2
0
 public function fill(OrderInterface $order, array $data)
 {
     if (!empty($data['customer']) && is_numeric($data['customer'])) {
         $data['customer'] = $this->customerService->find($data['customer']);
     }
     if (isset($data['customer'])) {
         if (!empty($data['customer'])) {
             $data['customer'] = $this->wp->getHelpers()->maybeUnserialize($data['customer']);
         } else {
             $data['customer'] = new CustomerEntity\Guest();
         }
         if (isset($data['billing_address'])) {
             $data['billing_address'] = array_merge(array_flip(array_keys(ProductHelper::getBasicBillingFields())), $data['billing_address']);
             /** @var CustomerEntity $customer */
             $customer = $data['customer'];
             $customer->setBillingAddress($this->createAddress($data['billing_address']));
         }
         if (isset($data['shipping_address'])) {
             $data['shipping_address'] = array_merge(array_flip(array_keys(ProductHelper::getBasicShippingFields())), $data['shipping_address']);
             /** @var CustomerEntity $customer */
             $customer = $data['customer'];
             $customer->setShippingAddress($this->createAddress($data['shipping_address']));
         }
         $order->setCustomer($data['customer']);
         unset($data['customer']);
     }
     /** @var OrderInterface $order */
     $order = $this->wp->applyFilters('jigoshop\\factory\\order\\fetch\\after_customer', $order);
     if (isset($data['items'])) {
         $order->removeItems();
     }
     //We do not want to add coupons and from directly, without validation.
     $coupons = null;
     if (isset($data['coupons'])) {
         $coupons = $data['coupons'];
         unset($data['coupons']);
     }
     if (isset($data['discount'])) {
         unset($data['discount']);
     }
     $order->restoreState($data);
     if ($coupons) {
         $coupons = $this->wp->getHelpers()->maybeUnserialize($coupons);
         if (isset($coupons[0]) && is_array($coupons[0])) {
             $codes = array_map(function ($coupon) {
                 return $coupon['code'];
             }, $coupons);
         } else {
             $codes = $coupons;
         }
         $coupons = $this->couponService->getByCodes($codes);
         foreach ($coupons as $coupon) {
             /** @var Coupon $coupon */
             try {
                 $order->addCoupon($coupon);
             } catch (Exception $e) {
                 $this->messages->addWarning($e->getMessage(), false);
             }
         }
     }
     return $this->wp->applyFilters('jigoshop\\factory\\order\\fill', $order);
 }