public function action()
 {
     if (isset($_POST['action']) && $_POST['action'] == 'add-to-cart') {
         /** @var Entity $product */
         $product = $this->productService->find($_POST['item']);
         try {
             $item = $this->wp->applyFilters('jigoshop\\cart\\add', null, $product);
             if ($item === null) {
                 throw new Exception(__('Unable to add product to the cart.', 'jigoshop'));
             }
             $cart = $this->cartService->get($this->cartService->getCartIdForCurrentUser());
             $cart->addItem($item);
             $this->cartService->save($cart);
             $url = false;
             $button = '';
             switch ($this->options->get('shopping.redirect_add_to_cart')) {
                 case 'cart':
                     $url = $this->wp->getPermalink($this->options->getPageId(Pages::CART));
                     break;
                 case 'checkout':
                     $url = $this->wp->getPermalink($this->options->getPageId(Pages::CHECKOUT));
                     break;
                 case 'product':
                 default:
                     $url = $this->wp->getPermalink($product->getId());
                 case 'same_page':
                 case 'product_list':
                     $button = sprintf('<a href="%s" class="btn btn-warning pull-right">%s</a>', $this->wp->getPermalink($this->options->getPageId(Pages::CART)), __('View cart', 'jigoshop'));
             }
             $this->messages->addNotice(sprintf(__('%s successfully added to your cart. %s', 'jigoshop'), $product->getName(), $button));
             if ($url !== false) {
                 $this->messages->preserveMessages();
                 $this->wp->wpRedirect($url);
                 exit;
             }
         } catch (NotEnoughStockException $e) {
             if ($e->getStock() == 0) {
                 $message = sprintf(__('Sorry, we do not have "%s" in stock.', 'jigoshop'), $product->getName());
             } else {
                 if ($this->options->get('products.show_stock')) {
                     $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. We only have %d available at this time. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName(), $e->getStock());
                 } else {
                     $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName());
                 }
             }
             $this->messages->addError($message);
         } catch (Exception $e) {
             $this->messages->addError(sprintf(__('A problem ocurred when adding to cart: %s', 'jigoshop'), $e->getMessage()), false);
         }
     }
 }
Beispiel #2
0
 public function action()
 {
     if (isset($_REQUEST['action'])) {
         switch ($_REQUEST['action']) {
             case 'cancel_order':
                 if ($this->wp->getHelpers()->verifyNonce($_REQUEST['nonce'], 'cancel_order')) {
                     /** @var Order $order */
                     $order = $this->orderService->find((int) $_REQUEST['id']);
                     if ($order->getKey() != $_REQUEST['key']) {
                         $this->messages->addError(__('Invalid order key.', 'jigoshop'));
                         return;
                     }
                     if ($order->getStatus() != Status::PENDING) {
                         $this->messages->addError(__('Unable to cancel order.', 'jigoshop'));
                         return;
                     }
                     $order->setStatus(Status::CANCELLED);
                     $cart = $this->cartService->createFromOrder($this->cartService->getCartIdForCurrentUser(), $order);
                     $this->orderService->save($order);
                     $this->cartService->save($cart);
                     $this->messages->addNotice(__('The order has been cancelled', 'jigoshop'));
                 }
                 break;
             case 'update-shipping':
                 $customer = $this->customerService->getCurrent();
                 $this->updateCustomer($customer);
                 break;
             case 'checkout':
                 try {
                     $cart = $this->cartService->getCurrent();
                     // Update quantities
                     $this->updateQuantities($cart);
                     // Update customer (if needed)
                     if ($this->options->get('shipping.calculator')) {
                         $customer = $this->customerService->getCurrent();
                         $this->updateCustomer($customer);
                     }
                     if (isset($_POST['jigoshop_order']['shipping_method'])) {
                         // Select shipping method
                         $method = $this->shippingService->get($_POST['jigoshop_order']['shipping_method']);
                         $cart->setShippingMethod($method);
                     }
                     if ($cart->getShippingMethod() && !$cart->getShippingMethod()->isEnabled()) {
                         $cart->removeShippingMethod();
                         $this->messages->addWarning(__('Previous shipping method is unavailable. Please select different one.', 'jigoshop'));
                     }
                     if ($this->options->get('shopping.validate_zip')) {
                         $address = $cart->getCustomer()->getShippingAddress();
                         if ($address->getPostcode() && !Validation::isPostcode($address->getPostcode(), $address->getCountry())) {
                             throw new Exception(__('Postcode is not valid!', 'jigoshop'));
                         }
                     }
                     do_action('jigoshop\\cart\\before_checkout', $cart);
                     $this->cartService->save($cart);
                     $this->messages->preserveMessages();
                     $this->wp->redirectTo($this->options->getPageId(Pages::CHECKOUT));
                 } catch (Exception $e) {
                     $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage()));
                 }
                 break;
             case 'update-cart':
                 if (isset($_POST['cart']) && is_array($_POST['cart'])) {
                     try {
                         $cart = $this->cartService->getCurrent();
                         $this->updateQuantities($cart);
                         $this->cartService->save($cart);
                         $this->messages->addNotice(__('Successfully updated the cart.', 'jigoshop'));
                     } catch (Exception $e) {
                         $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage()));
                     }
                 }
         }
     }
     if (isset($_GET['action']) && isset($_GET['item']) && $_GET['action'] === 'remove-item' && is_numeric($_GET['item'])) {
         $cart = $this->cartService->getCurrent();
         $cart->removeItem((int) $_GET['item']);
         $this->cartService->save($cart);
         $this->messages->addNotice(__('Successfully removed item from cart.', 'jigoshop'), false);
     }
 }