/** * Add Products to cart * @param Cart $cart Cart object */ private function addProductsToCart($cart, $codeCountry) { $products = $this->datas->orderLineItems; $country = $this->getCountry($codeCountry); $address = Address::initialize(); $address->id_country = $country->id; if ($products && count($products)) { foreach ($products as $p) { if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::IN_PROGRESS, 'Product : ' . $p->product->code); } $product = PowaTagProductHelper::getProductByCode($p->product->code, $this->context->language->id); if (!Validate::isLoadedObject($product)) { $this->addError(sprintf($this->module->l('This product does not exists : %s'), $p->product->code), PowaTagErrorType::$SKU_NOT_FOUND); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } $variants = $p->product->productVariants; $product_rate = 1 + $product->getTaxesRate($address) / 100; foreach ($variants as $variant) { $variantCurrency = $this->getCurrencyByIsoCode($variant->finalPrice->currency); if (!PowaTagValidate::currencyEnable($variantCurrency)) { $this->addError(sprintf($this->module->l('Currency not found : %s'), $variant->code), PowaTagErrorType::$CURRENCY_NOT_SUPPORTED); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } $variantAmount = $variant->finalPrice->amount; $id_product_attribute = false; $combination = false; if ($id_product_attribute = PowaTagProductAttributeHelper::getCombinationByCode($product->id, $variant->code)) { $combination = new Combination($id_product_attribute); $priceAttribute = $product->getPrice($this->display_taxes, $id_product_attribute); $qtyInStock = PowaTagProductQuantityHelper::getProductQuantity($product, $id_product_attribute); } else { if ($product) { $priceAttribute = $product->getPrice($this->display_taxes); $qtyInStock = PowaTagProductQuantityHelper::getProductQuantity($product); } else { $this->addError(sprintf($this->module->l('This variant does not exist : %s'), $variant->code), PowaTagErrorType::$SKU_NOT_FOUND); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } } if ($qtyInStock == 0) { $this->addError(sprintf($this->module->l('No Stock Available')), PowaTagErrorType::$SKU_OUT_OF_STOCK); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } if ($qtyInStock < $p->quantity) { $this->addError(sprintf($this->module->l('Quantity > Stock Count')), PowaTagErrorType::$INSUFFICIENT_STOCK); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } if ($p->quantity < $product->minimal_quantity || $combination && $combination->minimal_quantity > $p->quantity) { $this->addError(sprintf($this->module->l('Quantity < minimal quantity for product')), PowaTagErrorType::$OTHER_STOCK_ERROR); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::ERROR, 'Product : ' . $this->error['message']); } return false; } $cart->updateQty($p->quantity, $product->id, $id_product_attribute); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Add product to cart', PowaTagLogs::SUCCESS, 'Cart ID : ' . $cart->id . ' - Product ID : ' . $product->id); } break; } } } else { $this->addError($this->module->l('No product found in request'), PowaTagErrorType::$SKU_NOT_FOUND); return false; } // add vouchers if (isset($this->datas->vouchers)) { $this->context->cart = $cart; $vouchers = $this->datas->vouchers; if ($vouchers && count($vouchers)) { foreach ($vouchers as $voucher) { $ci = CartRule::getIdByCode($voucher); if (!$ci) { continue; } $cr = new CartRule($ci); if (!$cr) { continue; } if ($error = $cr->checkValidity($this->context, false, true)) { continue; } $this->context->cart->addCartRule($cr->id); if (PowaTagAPI::apiLog()) { PowaTagLogs::initAPILog('Added voucher', PowaTagLogs::SUCCESS, 'Cart ID : ' . $cart->id . ' - Voucher : ' . $voucher); } } } } return true; }
/** * Calculate total of products without tax * @return float Total of products */ protected function getSubTotal($products, $codeCountry, $check = true) { if (Validate::isInt($codeCountry)) { $country = new Country($codeCountry); } else { if (!$codeCountry instanceof Country) { $country = $this->getCountryByCode($codeCountry); } } $address = Address::initialize(); $address->id_country = $country->id; if ($products && count($products)) { foreach ($products as $p) { $product = PowaTagProductHelper::getProductByCode($p->product->code, $this->context->language->id); if (!Validate::isLoadedObject($product)) { $this->addError(sprintf($this->module->l('This product does not exists : %s'), $p->product->code), PowaTagErrorType::$SKU_NOT_FOUND); return false; } $variants = $p->product->productVariants; $product_rate = 1 + $product->getTaxesRate($address) / 100; foreach ($variants as $variant) { $variantCurrency = $this->getCurrencyByIsoCode($variant->finalPrice->currency); if (!PowaTagValidate::currencyEnable($variantCurrency)) { $this->addError(sprintf($this->module->l('Currency not found : %s'), $variantCurrency), PowaTagErrorType::$CURRENCY_NOT_SUPPORTED); return false; } $variantAmount = $variant->finalPrice->amount; if ($id_product_attribute = PowaTagProductAttributeHelper::getCombinationByCode($product->id, $variant->code)) { $priceAttribute = $product->getPrice(false, $id_product_attribute); $qtyInStock = PowaTagProductQuantityHelper::getProductQuantity($product, $id_product_attribute); } else { if ($product) { $priceAttribute = $product->getPrice(false); $qtyInStock = PowaTagProductQuantityHelper::getProductQuantity($product); } else { $this->addError(sprintf($this->module->l('This variant does not exist : %s'), $variant->code), PowaTagErrorType::$SKU_NOT_FOUND); return false; } } $priceAttributeWt = $priceAttribute * $product_rate; $this->convertToCurrency($variantAmount, $variantCurrency, false); if (version_compare(_PS_VERSION_, 1.6, '<')) { $priceAttribute = Tools::ps_round($priceAttribute, 2); $variantAmount = Tools::ps_round($variantAmount, 2); $priceAttributeWt = Tools::ps_round($priceAttributeWt, 2); } if ($check && $priceAttribute != $variantAmount) { $this->addError(sprintf($this->module->l('Price variant is different with the price shop : %s %s != %s'), $variant->code, $priceAttribute, $variantAmount), PowaTagErrorType::$OTHER_PRODUCT_ERROR); return false; } if ($qtyInStock == 0) { $this->addError(sprintf($this->module->l('No Stock Available'), $variant->code), PowaTagErrorType::$SKU_OUT_OF_STOCK); return false; } if ($qtyInStock < $p->quantity) { $this->addError(sprintf($this->module->l('Quantity > Stock Count'), $variant->code), PowaTagErrorType::$INSUFFICIENT_STOCK); return false; } $totalPriceAttribute = $priceAttribute * $p->quantity; $totalPriceAttributeWt = $priceAttributeWt * $p->quantity; $this->subTotal += $totalPriceAttribute; $this->subTotalWt += $totalPriceAttributeWt; $this->subTax += $totalPriceAttributeWt - $totalPriceAttribute; } } $this->subTax = $this->subTotalWt - $this->subTotal; return true; } else { return false; } }
private function getVariants() { $groups = array(); if ($this->combinations && count($this->combinations)) { foreach ($this->combinations as $combination) { if (!array_key_exists($combination['id_product_attribute'], $groups)) { $groups[$combination['id_product_attribute']] = array('code' => PowatagProductAttributeHelper::getVariantCode($combination), 'numberInStock' => PowaTagProductQuantityHelper::getCombinationQuantity($combination), 'productImages' => $this->getCombinationImages($combination['id_product_attribute']), 'originalPrice' => array('amount' => $this->formatNumber($this->product->getPrice($this->display_taxes, null), 2), 'currency' => $this->context->currency->iso_code), 'finalPrice' => array('amount' => $this->formatNumber($this->product->getPrice($this->display_taxes, $combination['id_product_attribute']), 2), 'currency' => $this->context->currency->iso_code)); } $groups[$combination['id_product_attribute']]['options'][$combination['group_name']] = $combination['attribute_name']; } sort($groups); } else { $variant = array('code' => PowaTagProductHelper::getProductSKU($this->product), 'numberInStock' => PowaTagProductQuantityHelper::getProductQuantity($this->product), 'finalPrice' => array('amount' => $this->formatNumber($this->product->getPrice($this->display_taxes, null), 2), 'currency' => $this->context->currency->iso_code)); $groups = array($variant); } return $groups; }