public function checkProductsAreShippable($products)
 {
     foreach ($products as $p) {
         $carrier_ok = false;
         $product = PowaTagProductHelper::getProductByCode($p->product->code, $this->context->language->id);
         if (!$product) {
             // product not found
             $this->addError($this->module->l('This product does not exists : ') . $p->product->code, PowaTagErrorType::$SKU_NOT_FOUND);
             return;
         }
         $carriers = $product->getCarriers();
         if (count($carriers)) {
             $powatag_carrier = Configuration::get('POWATAG_SHIPPING');
             foreach ($carriers as $carrier) {
                 if ($carrier['id_carrier'] == $powatag_carrier) {
                     $carrier_ok = true;
                     break;
                 }
             }
             if (!$carrier_ok) {
                 $this->addError($this->module->l('Product with id') . ' ' . $product->id . ' ' . $this->module->l('cannot be shipped with the carrier ') . ' ' . $powatag_carrier, PowaTagErrorType::$MERCHANT_WRONG_COUNTRY);
             }
         }
     }
 }
 /**
  * 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;
 }
示例#3
0
 private function generateTag()
 {
     $product = new Product((int) Tools::getValue('id_product'), true, (int) $this->context->language->id);
     if ($product_sku = PowaTagProductHelper::getProductSKU($product)) {
         $lang = Configuration::get('POWATAG_LANG');
         if ($lang == "site") {
             // convert "en-us" to "en_US"
             $lang = $this->context->language->language_code;
             $lang = str_replace("-", "_", $lang);
             $alang = explode("_", $lang);
             if (count($alang) == 2) {
                 $lang = strtolower($alang[0]) . "_" . strtoupper($alang[1]);
             }
         }
         $datas = array('powatagApi' => Configuration::get('POWATAG_API_KEY'), 'productSku' => PowaTagProductHelper::getProductSKU($product), 'powatagGeneratorURL' => Configuration::get('POWATAG_GENERATOR_URL'), 'powatagRedirect' => Configuration::get('POWATAG_REDIRECT'), 'powatagOffer' => Configuration::get('POWATAG_OFFER'), 'powatagLang' => $lang, 'powatagType' => Configuration::get('POWATAG_TYPE'), 'powatagStyle' => Configuration::get('POWATAG_STYLE'), 'powatagColorscheme' => Configuration::get('POWATAG_COLORSCHEME'), 'powatagDisplay' => Configuration::get('POWATAG_DISPLAY'), 'powatagVideo' => Configuration::get('POWATAG_VIDEO') ? "true" : "false", 'powatagDebug' => Configuration::get('POWATAG_DEBUG') ? "true" : "false");
         $this->context->smarty->assign($datas);
         return $this->display(__FILE__, 'product.tpl');
     }
 }
 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;
 }