/** * Executes actions associated with selected page. */ public function action() { if (isset($_POST['action']) && $_POST['action'] == 'add-to-cart') { /** @var \Jigoshop\Entity\Product $product */ $product = $this->productService->find($_POST['item']); try { /** @var Item $item */ $item = $this->wp->applyFilters('jigoshop\\cart\\add', null, $product); if ($item === null) { throw new Exception(__('Unable to add product to the cart.', 'jigoshop')); } if (isset($_POST['quantity'])) { $item->setQuantity($_POST['quantity']); } /** @var Cart $cart */ $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; /** @noinspection PhpMissingBreakStatementInspection */ /** @noinspection PhpMissingBreakStatementInspection */ case 'product_list': $url = $this->wp->getPermalink($this->options->getPageId(Pages::SHOP)); case 'product': case 'same_page': default: $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); } } 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())); } } }