/** * @param Cart $cart * @return array */ public static function orderShippingCharge(Cart $cart) { $shippingCharge = null; $suppliers = []; if (count($cart->getItems()) == 0) { return $shippingCharge; } foreach ($cart->getItems() as $cartItem) { /* @var $cartItem CartItem */ $product = $cartItem->getProduct(); if (count($product->getShipping())) { foreach ($product->getShipping() as $sh) { /* @var $sh ProductShippingOption */ if ($sh->getMatch($cart->getCountryId(), $cart->getShippingType())) { if ($sh->getSupplierId()) { $suppliers[$sh->getSupplierId()] = $sh->getOrderPrice(); } else { $suppliers[0] = $sh->getOrderPrice(); } } } } } $shippingCharge = false; foreach ($suppliers as $supplierId => $value) { $shippingCharge += $value; } // return null if no shipping charge applies return $shippingCharge !== false ? $shippingCharge : NULL; }
/** * @param bool $priceIncludesTax * @param int $cartItemQuantity * @return Cart */ protected function makeCart2($priceIncludesTax = true, $cartItemQuantity = 1) { $params = ['priceIncludesTax' => $priceIncludesTax, 'chargeTaxOnShipping' => true, 'defaultTaxRate' => 20]; $cart = new Cart($params); $variationParams = ['id' => 55, 'name' => 'Test Three', 'price' => 2500, 'sku' => '123x']; $product = new Product(['id' => 1, 'productName' => '', 'productType' => '', 'shortDescription' => '', 'longDescription' => '', 'shipping' => [new ProductShippingOption(300, 15, 'Standard Shipping', 2, [79])], 'tax' => [new ProductTaxOption(20, [79], true)], 'variations' => [new ProductVariation($variationParams)]]); $cartItem = new CartItem(['productVariationId' => 55, 'quantity' => $cartItemQuantity, 'product' => $product, 'basePrice' => 2500, 'taxRate' => 20, 'shippingType' => 'Standard Shipping', 'countryId' => 79, 'priceIncludesTax' => $priceIncludesTax]); $cart->add($cartItem); $variationParams['name'] = 'Test Two'; $variationParams['id'] = 56; $variationParams['price'] = 1500; $product = new Product(['id' => 2, 'productName' => '', 'productType' => '', 'shortDescription' => '', 'longDescription' => '', 'shipping' => [new ProductShippingOption(200, 25, 'Standard Shipping', 1, [79])], 'tax' => [new ProductTaxOption(20, [79], true)], 'variations' => [new ProductVariation($variationParams)]]); $cartItem = new CartItem(['productVariationId' => 56, 'quantity' => $cartItemQuantity, 'product' => $product, 'basePrice' => 1500, 'taxRate' => 20, 'shippingType' => 'Standard Shipping', 'countryId' => 79, 'priceIncludesTax' => $priceIncludesTax]); $cart->add($cartItem); $variationParams['name'] = 'Test One'; $variationParams['id'] = 57; $variationParams['price'] = 1200; $product = new Product(['id' => 3, 'productName' => '', 'productType' => '', 'shortDescription' => '', 'longDescription' => '', 'shipping' => [new ProductShippingOption(100, 50, 'Standard Shipping', null, [79])], 'tax' => [new ProductTaxOption(20, [79], true)], 'variations' => [new ProductVariation($variationParams)]]); $cartItem = new CartItem(['productVariationId' => 57, 'quantity' => $cartItemQuantity, 'product' => $product, 'basePrice' => 1200, 'taxRate' => 20, 'shippingType' => 'Standard Shipping', 'countryId' => 79, 'priceIncludesTax' => $priceIncludesTax]); $cart->add($cartItem); return $cart; }
/** * @param Cart $cart * @return Cart * @throws FailResponseException */ public function save($cart) { $items = $cart->getItems(); $itemIds = []; foreach ($items as $cartItem) { /* @var $cartItem CartItem */ for ($i = 1; $i <= $cartItem->getQuantity(); $i++) { $itemIds[] = $cartItem->getVariationId(); } } Session::put('shoppingCart', $itemIds); Session::save(); if ($token = CustomerHelper::checkLoggedinCustomer()) { try { $data = ['token' => $token, 'ids' => $itemIds]; VendirunApi::makeRequest('cart/update', $data)->getData(); } catch (FailResponseException $e) { // API fail, maybe not logged in properly or token expired // doesn't matter, we just can't save the cart details to Vendirun } } return $this->find(); }
/** * @param $items * @param null $countryId * @param string $shippingType * @return Cart */ public function makeFromIds($items, $countryId = null, $shippingType = '') { $clientInfo = Config::get('clientInfo'); $priceIncludesTax = $clientInfo->business_settings->tax->price_includes_tax; $taxes = ProductTaxOptionFactory::makeFromApi($clientInfo->business_settings->tax->country_tax_rates); $defaultTaxRate = TaxCalculator::calculateProductTaxRate($taxes, $countryId, $clientInfo->business_settings->tax->default_tax_rate); $params = ['ids' => $items, 'priceIncludesTax' => $priceIncludesTax, 'chargeTaxOnShipping' => $clientInfo->business_settings->tax->charge_tax_on_shipping, 'defaultTaxRate' => $defaultTaxRate, 'countryId' => $countryId, 'shippingType' => $shippingType ? $shippingType : null]; $cart = new Cart($params); $cartItemFactory = new CartItemFactory($this->cartRepository, $cart); foreach ($cartItemFactory->makeFromIds($items, $priceIncludesTax, $cart->getDefaultTaxRate()) as $item) { $cart->add($item); } $cart->checkIdList(); // check for free shipping if (isset($clientInfo->business_settings->free_shipping) && $clientInfo->business_settings->free_shipping->free_shipping_enabled) { $cart->updateForFreeShipping($this->cartValuesTransformer, $clientInfo->business_settings->free_shipping->free_shipping_minimum_order, $clientInfo->business_settings->free_shipping->free_shipping_countries); } return $cart; }
/** * @param Cart $cart * @param Customer $customer * @param CartValuesTransformer $cartValuesTransformer * @param CartItemValuesTransformer $cartItemValuesTransformer * @param array $params * @return Order */ public function fromCart(Cart $cart, Customer $customer, CartValuesTransformer $cartValuesTransformer, CartItemValuesTransformer $cartItemValuesTransformer, $params) { if (isset($params['shippingaddressId']) && $params['shippingaddressId'] > 0) { $shippingAddress = $customer->getAddressFromAddressId($params['shippingaddressId']); } else { $data = ['address1' => $params['shippingaddress1'], 'address2' => $params['shippingaddress2'], 'address3' => $params['shippingaddress3'], 'city' => $params['shippingcity'], 'state' => $params['shippingstate'], 'postcode' => $params['shippingpostcode'], 'countryId' => $params['shippingcountryId']]; $shippingAddress = new Address($data); } if (isset($params['billingAddressSameAsShipping']) && $params['billingAddressSameAsShipping']) { $billingAddress = $shippingAddress; } else { if (isset($params['billingaddressId']) && $params['billingaddressId'] > 0) { $billingAddress = $customer->getAddressFromAddressId($params['billingaddressId']); } else { $data = ['address1' => $params['billingaddress1'], 'address2' => $params['billingaddress2'], 'address3' => $params['billingaddress3'], 'city' => $params['billingcity'], 'state' => $params['billingstate'], 'postcode' => $params['billingpostcode'], 'countryId' => $params['billingcountryId']]; $billingAddress = new Address($data); } } if (isset($params['billingAddressSameAsShipping'])) { $data['billing_address_same_as_shipping'] = true; } $items = []; foreach ($cart->getItems() as $item) { /* @var $item CartItem */ $sku = $item->getSku(); $cartItemValues = $item->getValues($cartItemValuesTransformer); $items[] = new OrderItem(NULL, $item->getVariationId(), $item->getTaxRate(), $cartItemValues['totalBeforeTax'], $item->getQuantity(), $item->getProductName(), $sku, 0, 0); } // add order item for shipping $cartValues = $cart->getValues($cartValuesTransformer); if ($cartValues['shipping'] > 0) { $items[] = new OrderItem(NULL, NULL, $cart->getDefaultTaxRate(), $cartValues['shipping'] - $cartValues['shippingTax'], 1, $cart->getShippingType(), 'SHIPPING', 1, 0); } $order = new Order($customer, $billingAddress, $shippingAddress, $items, $cart->getShippingType()); return $order; }
/** * @param Cart $cart */ public function updateShippingAndTax(Cart $cart) { $this->countryId = $cart->getCountryId(); $this->shippingType = $cart->getShippingType(); }