/** * Validates whether * * @param OrderInterface $cart */ public function validate(OrderInterface $cart) { $customer = $cart->getCustomer(); $billingErrors = $this->validateAddress($customer->getBillingAddress()); if ($customer->getBillingAddress()->getEmail() == null) { $billingErrors[] = __('Email address is empty.', 'jigoshop'); } if ($customer->getBillingAddress()->getPhone() == null) { $billingErrors[] = __('Phone is empty.', 'jigoshop'); } if (!Validation::isEmail($customer->getBillingAddress()->getEmail())) { $billingErrors[] = __('Email address is invalid.', 'jigoshop'); } $shippingErrors = $this->validateAddress($customer->getShippingAddress()); $billingErrors = $this->wp->applyFilters('jigoshop\\service\\cart\\billing_address_validation', $billingErrors, $customer->getBillingAddress()); $shippingErrors = $this->wp->applyFilters('jigoshop\\service\\cart\\shipping_address_validation', $shippingErrors, $customer->getShippingAddress()); $error = ''; if (!empty($billingErrors)) { $error .= $this->prepareAddressError(__('Billing address is not valid.', 'jigoshop'), $billingErrors); } if (!empty($shippingErrors)) { $error .= $this->prepareAddressError(__('Shipping address is not valid.', 'jigoshop'), $shippingErrors); } if (!empty($error)) { throw new Exception($error); } }
/** * @param OrderInterface $order Order to calculate shipping for. * * @return float Calculated value of shipping for the order. */ public function calculate(OrderInterface $order) { if ($this->options['type'] == 'per_item') { $quantity = array_sum(array_map(function ($item) { return $item->getQuantity(); }, $order->getItems())); return (double) ($this->options['cost'] * $quantity + $this->options['fee']); } return (double) ($this->options['cost'] + $this->options['fee']); }
/** * @param $taxClass string Tax class to get label for. * @param OrderInterface $order Order to calculate taxes for. * * @return string Tax class rate * @throws Exception When tax class is not found. */ public function getRate($taxClass, $order) { if (!in_array($taxClass, $this->taxClasses)) { if (WP_DEBUG) { throw new Exception(sprintf(__('No tax class: %s', 'jigoshop'), $taxClass)); } return $taxClass; } $definitions = $order->getTaxDefinitions(); if (!isset($definitions[$taxClass])) { $definitions[$taxClass] = $this->getDefinition($taxClass, $order->getCustomer()->getTaxAddress()); } return $definitions[$taxClass]['rate']; }
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); }