public function action() { if (isset($_POST['action']) && $_POST['action'] == 'add-to-cart') { $post = $this->wp->getGlobalPost(); /** @var \Jigoshop\Entity\Product $product */ $product = $this->productService->findForPost($post); 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())); } } }
/** * Display the widget in the sidebar. * * @param array $args Sidebar arguments. * @param array $instance Instance. */ public function widget($args, $instance) { // Hide widget if page is the cart or checkout if (Pages::isCart() || Pages::isCheckout()) { return; } // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Cart', 'jigoshop'), $instance, $this->id_base); Render::output('widget/cart/widget', array_merge($args, array('title' => $title, 'cart' => self::$cart->getCurrent(), 'cart_url' => get_permalink(self::$options->getPageId(\Jigoshop\Frontend\Pages::CART)), 'checkout_url' => get_permalink(self::$options->getPageId(\Jigoshop\Frontend\Pages::CHECKOUT))))); }
/** * @return bool Whether current method is enabled and able to work. */ public function isEnabled() { $cart = $this->cartService->getCurrent(); $post = $this->wp->getGlobalPost(); if ($post === null || $post->post_type != Types::ORDER) { $customer = $cart->getCustomer(); } else { // TODO: Get rid of this hack for customer fetching $customer = unserialize($this->wp->getPostMeta($post->ID, 'customer', true)); } return $this->options['enabled'] && ($this->options['available_for'] === 'all' || in_array($customer->getShippingAddress()->getCountry(), $this->options['countries'])); }
/** * @return bool Whether current method is enabled and able to work. */ public function isEnabled() { $cart = $this->cartService->getCurrent(); $post = $this->wp->getGlobalPost(); if ($post === null || $post->post_type != Types::ORDER) { $customer = $cart->getCustomer(); } else { // TODO: Get rid of this hack for customer fetching $customer = maybe_unserialize($this->wp->getPostMeta($post->ID, 'customer', true)); } if (empty($customer)) { return $this->options['enabled']; } return $this->options['enabled'] && $customer->getShippingAddress()->getCountry() == $this->baseCountry; }
/** * @return bool Whether current method is enabled and able to work. */ public function isEnabled() { $cart = $this->cartService->getCurrent(); $post = $this->wp->getGlobalPost(); if ($post === null || $post->post_type != Types::ORDER) { $customer = $cart->getCustomer(); } else { // TODO: Get rid of this hack for customer fetching $customer = unserialize($this->wp->getPostMeta($post->ID, 'customer', true)); } $freeShippingCoupon = array_reduce($cart->getCoupons(), function ($value, $coupon) { /** @var $coupon Coupon */ return $value || $coupon->isFreeShipping(); }, false); return $this->options['enabled'] && ($freeShippingCoupon || $cart->getProductSubtotal() >= $this->options['minimum'] && ($this->options['available_for'] === 'all' || in_array($customer->getShippingAddress()->getCountry(), $this->options['countries']))); }
public function render() { $cart = $this->cartService->getCurrent(); $content = $this->wp->getPostField('post_content', $this->options->getPageId(Pages::CART)); $content = do_shortcode($content); $termsUrl = ''; $termsPage = $this->options->get('advanced.pages.terms'); if ($termsPage > 0) { $termsUrl = $this->wp->getPermalink($termsPage); } return Render::get('shop/cart', array('content' => $content, 'cart' => $cart, 'messages' => $this->messages, 'productService' => $this->productService, 'customer' => $this->customerService->getCurrent(), 'shippingMethods' => $this->shippingService->getEnabled(), 'shopUrl' => $this->wp->getPermalink($this->options->getPageId(Pages::SHOP)), 'showWithTax' => $this->options->get('tax.price_tax') == 'with_tax', 'showShippingCalculator' => $this->options->get('shipping.calculator'), 'termsUrl' => $termsUrl)); }
/** * Renders page template. * * @return string Page template. */ public function render() { $content = $this->wp->getPostField('post_content', $this->options->getPageId(Pages::CHECKOUT)); $content = do_shortcode($content); $cart = $this->cartService->getCurrent(); $billingFields = $this->getBillingFields($cart->getCustomer()->getBillingAddress()); $shippingFields = $this->getShippingFields($cart->getCustomer()->getShippingAddress()); $termsUrl = ''; $termsPage = $this->options->get('advanced.pages.terms'); if ($termsPage > 0) { $termsUrl = $this->wp->getPageLink($termsPage); } $verificationMessage = $this->options->get('shopping.enable_verification_message') ? $this->options->get('shopping.verification_message') : ''; return Render::get('shop/checkout', array('cartUrl' => $this->wp->getPermalink($this->options->getPageId(Pages::CART)), 'content' => $content, 'cart' => $cart, 'messages' => $this->messages, 'shippingMethods' => $this->shippingService->getEnabled(), 'paymentMethods' => $this->paymentService->getEnabled(), 'billingFields' => $billingFields, 'shippingFields' => $shippingFields, 'showWithTax' => $this->options->get('tax.price_tax') == 'with_tax', 'showLoginForm' => $this->options->get('shopping.show_login_form') && !$this->wp->isUserLoggedIn(), 'allowRegistration' => $this->options->get('shopping.allow_registration') && !$this->wp->isUserLoggedIn(), 'showRegistrationForm' => $this->options->get('shopping.allow_registration') && !$this->options->get('shopping.guest_purchases') && !$this->wp->isUserLoggedIn(), 'alwaysShowShipping' => $this->options->get('shipping.always_show_shipping'), 'verificationMessage' => $verificationMessage, 'differentShipping' => isset($_POST['jigoshop_order']) ? $_POST['jigoshop_order']['different_shipping_address'] == 'on' : false, 'termsUrl' => $termsUrl, 'defaultGateway' => $this->options->get('payment.default_gateway'))); }
/** * Returns cart ID for current user. * If the user is logged in - returns his ID so his cart will be properly loaded. * Otherwise generates random string based on available user data to preserve it's cart. * * @return string Cart ID for currently logged in user. */ public function getCartIdForCurrentUser() { return $this->service->getCartIdForCurrentUser(); }