/** * Find and fetches saved cart. * If cart is not found - returns new empty one. * * @param $id string Id of cart to fetch. * * @return \Jigoshop\Entity\Cart Prepared cart instance. */ public function get($id) { if (!isset($this->carts[$id])) { $cart = new Cart($this->options->get('tax.classes')); $cart->setCustomer($this->customerService->getCurrent()); $cart->getCustomer()->selectTaxAddress($this->options->get('taxes.shipping') ? 'shipping' : 'billing'); // Fetch data from session if available $cart->setId($id); $state = $this->getStateFromSession($id); if (isset($_POST['jigoshop_order']) && Pages::isCheckout()) { $state = $this->getStateFromCheckout($state); } // TODO: Support for transients? $cart = $this->orderFactory->fill($cart, $state); $this->carts[$id] = $this->wp->applyFilters('jigoshop\\service\\cart\\get', $cart, $state); } return $this->carts[$id]; }
/** * Creates cart from order - useful for cancelling orders. * * @param $cartId string Cart ID to use. * @param $order Order Order to base cart on. * * @return \Jigoshop\Entity\Cart The cart. */ public function createFromOrder($cartId, $order) { $cart = new \Jigoshop\Entity\Cart($this->options->get('tax.classes')); $cart->setId($cartId); $cart->setCustomer($order->getCustomer()); $cart->setCustomerNote($order->getCustomerNote()); $cart->setTaxDefinitions($order->getTaxDefinitions()); foreach ($order->getItems() as $item) { /** @var $item Order\Item */ $item = clone $item; $item->setId(false); $item->setKey(false); $cart->addItem($item); } // foreach ($order->getCoupons() as $coupon) { // $cart->addCoupon() // } $shipping = $order->getShippingMethod(); if ($shipping !== null) { $cart->setShippingMethod($shipping); $cart->setShippingTax($order->getShippingTax()); } $payment = $order->getPaymentMethod(); if ($payment !== null) { $cart->setPaymentMethod($payment); } return $cart; }