Example #1
0
 /**
  * Displays the product data box, tabbed, with several panels covering price, stock etc
  *
  * @since    1.0
  */
 public function box()
 {
     $post = $this->wp->getGlobalPost();
     $coupon = $this->couponService->findForPost($post);
     $methods = array();
     foreach ($this->paymentService->getAvailable() as $method) {
         /** @var $method Method */
         $methods[$method->getId()] = $method->getName();
     }
     Render::output('admin/coupon/box', array('coupon' => $coupon, 'types' => $this->couponService->getTypes(), 'paymentMethods' => $methods));
 }
Example #2
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;
 }
Example #3
0
 public function displayColumn($column)
 {
     $post = $this->wp->getGlobalPost();
     if ($post === null) {
         return;
     }
     /** @var \Jigoshop\Entity\Coupon $coupon */
     $coupon = $this->couponService->find($post->ID);
     switch ($column) {
         case 'code':
             echo $coupon->getCode();
             break;
         case 'type':
             echo $this->couponService->getType($coupon);
             break;
         case 'amount':
             echo ProductHelper::formatNumericPrice($coupon->getAmount());
             break;
         case 'usage_limit':
             echo $coupon->getUsageLimit();
             break;
         case 'usage':
             echo $coupon->getUsage();
             break;
         case 'from':
             $from = $coupon->getFrom();
             if ($from) {
                 echo Formatter::date($from->getTimestamp());
             }
             break;
         case 'to':
             $to = $coupon->getTo();
             if ($to) {
                 echo Formatter::date($to->getTimestamp());
             }
             break;
         case 'is_individual':
             echo sprintf('<span class="glyphicon %s" aria-hidden="true"></span> <span class="sr-only">%s</span>', $coupon->isIndividualUse() ? 'glyphicon-ok' : 'glyphicon-remove', $coupon->isIndividualUse() ? __('Yes', 'jigoshop') : __('No', 'jigoshop'));
             break;
     }
 }
Example #4
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);
 }