public function action() { if (isset($_POST['action']) && $_POST['action'] == 'save_address') { $customer = $this->customerService->getCurrent(); switch ($this->wp->getQueryParameter('edit-address')) { case 'shipping': $address = $customer->getShippingAddress(); break; case 'billing': default: $address = $customer->getBillingAddress(); break; } $errors = array(); if ($address instanceof CompanyAddress) { $address->setCompany(trim(htmlspecialchars(strip_tags($_POST['address']['company'])))); $address->setVatNumber(trim(htmlspecialchars(strip_tags($_POST['address']['euvatno'])))); } $address->setPhone(trim(htmlspecialchars(strip_tags($_POST['address']['phone'])))); $address->setFirstName(trim(htmlspecialchars(strip_tags($_POST['address']['first_name'])))); $address->setLastName(trim(htmlspecialchars(strip_tags($_POST['address']['last_name'])))); $address->setAddress(trim(htmlspecialchars(strip_tags($_POST['address']['address'])))); $address->setCity(trim(htmlspecialchars(strip_tags($_POST['address']['city'])))); $postcode = trim(htmlspecialchars(strip_tags($_POST['address']['postcode']))); if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($postcode, $address->getCountry())) { $errors[] = __('Postcode is not valid!', 'jigoshop'); } else { $address->setPostcode($postcode); } $country = trim(htmlspecialchars(strip_tags($_POST['address']['country']))); if (!Country::exists($country)) { $errors[] = sprintf(__('Country "%s" does not exists.', 'jigoshop'), $country); } else { $address->setCountry($country); } $state = trim(htmlspecialchars(strip_tags($_POST['address']['state']))); if (Country::hasStates($address->getCountry()) && !Country::hasState($address->getCountry(), $state)) { $errors[] = sprintf(__('Country "%s" does not have state "%s".', 'jigoshop'), Country::getName($address->getCountry()), $state); } else { $address->setState($state); } $email = trim(htmlspecialchars(strip_tags($_POST['address']['email']))); if (!Validation::isEmail($email)) { $errors[] = __('Invalid email address', 'jigoshop'); } else { $address->setEmail($email); } if (!empty($errors)) { $this->messages->addError(join('<br/>', $errors), false); } else { $this->customerService->save($customer); $this->messages->addNotice(__('Address saved.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT)); } } }
/** * Ajax action for changing postcode. */ public function ajaxChangePostcode() { $customer = $this->customerService->getCurrent(); if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($_POST['value'], $customer->getShippingAddress()->getCountry())) { echo json_encode(array('success' => false, 'error' => __('Postcode is not valid!', 'jigoshop'))); exit; } if ($customer->hasMatchingAddresses()) { $customer->getBillingAddress()->setPostcode($_POST['value']); } $customer->getShippingAddress()->setPostcode($_POST['value']); $this->customerService->save($customer); $cart = $this->cartService->getCurrent(); $cart->setCustomer($customer); $this->cartService->save($cart); $response = $this->getAjaxLocationResponse($customer, $cart); echo json_encode($response); exit; }
/** * Saves product to database. * * @param EntityInterface $object Customer to save. * @throws Exception */ public function save(EntityInterface $object) { $this->customers[$object->getId()] = $object; $this->service->save($object); }
/** * Executes actions associated with selected page. */ public function action() { $cart = $this->cartService->getCurrent(); if ($cart->isEmpty()) { $this->messages->addWarning(__('Your cart is empty, please add products before proceeding.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::SHOP)); } if (!$this->isAllowedToEnterCheckout()) { $this->messages->addError(__('You need to log in before processing to checkout.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::CART)); } if (isset($_POST['action']) && $_POST['action'] == 'purchase') { try { $allowRegistration = $this->options->get('shopping.allow_registration'); if ($allowRegistration && !$this->wp->isUserLoggedIn()) { $this->createUserAccount(); } if (!$this->isAllowedToCheckout($cart)) { if ($allowRegistration) { throw new Exception(__('You need either to log in or create account to purchase.', 'jigoshop')); } throw new Exception(__('You need to log in before purchasing.', 'jigoshop')); } if ($this->options->get('advanced.pages.terms') > 0 && (!isset($_POST['terms']) || $_POST['terms'] != 'on')) { throw new Exception(__('You need to accept terms & conditions!', 'jigoshop')); } $this->cartService->validate($cart); $this->customerService->save($cart->getCustomer()); if (!Country::isAllowed($cart->getCustomer()->getBillingAddress()->getCountry())) { $locations = array_map(function ($location) { return Country::getName($location); }, $this->options->get('shopping.selling_locations')); throw new Exception(sprintf(__('This location is not supported, we sell only to %s.'), join(', ', $locations))); } $shipping = $cart->getShippingMethod(); if ($this->isShippingRequired($cart) && (!$shipping || !$shipping->isEnabled())) { throw new Exception(__('Shipping is required for this order. Please select shipping method.', 'jigoshop')); } $payment = $cart->getPaymentMethod(); $isPaymentRequired = $this->isPaymentRequired($cart); $this->wp->doAction('jigoshop\\checkout\\payment', $payment); if ($isPaymentRequired && (!$payment || !$payment->isEnabled())) { throw new Exception(__('Payment is required for this order. Please select payment method.', 'jigoshop')); } $order = $this->orderService->createFromCart($cart); /** @var Order $order */ $order = $this->wp->applyFilters('jigoshop\\checkout\\order', $order); $this->orderService->save($order); $this->cartService->remove($cart); $url = ''; if ($isPaymentRequired) { $url = $payment->process($order); } else { $order->setStatus(\Jigoshop\Helper\Order::getStatusAfterCompletePayment($order)); $this->orderService->save($order); } // Redirect to thank you page if (empty($url)) { $url = $this->wp->getPermalink($this->wp->applyFilters('jigoshop\\checkout\\redirect_page_id', $this->options->getPageId(Pages::THANK_YOU))); $url = $this->wp->getHelpers()->addQueryArg(array('order' => $order->getId(), 'key' => $order->getKey()), $url); } $this->wp->wpRedirect($url); exit; } catch (Exception $e) { $this->messages->addError($e->getMessage()); } } }