/** * Generates a ShopgateCartItem for the checkCart Response * * @param Mage_Catalog_Model_Product $product * @param boolean $isBuyable * @param int $qtyBuyable * @param int|float $priceExclTax * @param int|float $priceInclTax * @param array $errors * @param bool $stockQuantity * * @return ShopgateCartItem */ public function generateShopgateCartItem($product, $isBuyable = false, $qtyBuyable = 0, $priceInclTax = 0, $priceExclTax = 0, $errors = array(), $stockQuantity = false) { $item = new ShopgateCartItem(); $item->setItemNumber($product->getShopgateItemNumber()); $item->setOptions($product->getShopgateOptions()); $item->setInputs($product->getShopgateInputs()); $item->setAttributes($product->getShhopgateAttributes()); $item->setIsBuyable((int) $isBuyable); $item->setQtyBuyable($qtyBuyable); $item->setStockQuantity($stockQuantity); $item->setUnitAmount($priceExclTax); $item->setUnitAmountWithTax($priceInclTax); foreach ($errors as $error) { $item->setError($error['type']); $item->setErrorText($error['message']); } return $item; }
/** * @param ShopgateCart $cart * @return array */ public function checkStock(ShopgateCart $cart) { $result = array(); foreach ($cart->getItems() as $item) { $cartItem = new ShopgateCartItem(); $cartItem->setItemNumber($item->getItemNumber()); list($productId, $attributeId) = ShopgateHelper::getProductIdentifiers($item); /** @var ProductCore $product */ if (version_compare(_PS_VERSION_, '1.5.2.0', '<')) { $product = new BWProduct($productId, true, $this->getPlugin()->getLanguageId()); } else { $product = new Product($productId, $this->getPlugin()->getLanguageId()); } if (empty($attributeId) && !empty($productId) && $product->hasAttributes()) { $result[] = $cartItem; continue; } $product->loadStockData(); /** * validate attributes */ if ($product->hasAttributes()) { $invalidAttribute = false; $message = ''; if (!$attributeId) { $cartItem->setError(ShopgateLibraryException::UNKNOWN_ERROR_CODE); $cartItem->setErrorText('attributeId required'); $message = 'attributeId required'; $invalidAttribute = true; } else { $validAttributeId = false; if (version_compare(_PS_VERSION_, '1.5.0', '<')) { $attributeIds = BWProduct::getProductAttributesIds($productId); } else { $attributeIds = $product->getProductAttributesIds($productId, true); } foreach ($attributeIds as $attribute) { if ($attributeId == $attribute['id_product_attribute']) { $validAttributeId = true; continue; } } if (!$validAttributeId) { $invalidAttribute = true; $message = 'invalid attributeId'; } } if ($invalidAttribute) { $cartItem->setError(ShopgateLibraryException::UNKNOWN_ERROR_CODE); $cartItem->setErrorText($message); $result[] = $cartItem; continue; } } if ($product->id) { if (version_compare(_PS_VERSION_, '1.5.0', '<')) { $quantity = $product->getStockAvailable(); //getQuantityAvailableByProduct($productId, $attributeId, $this->getPlugin()->getContext()->shop->id); } else { $quantity = StockAvailable::getQuantityAvailableByProduct($productId, $attributeId, $this->getPlugin()->getContext()->shop->id); } $cartItem->setStockQuantity($quantity); $cartItem->setIsBuyable($product->available_for_order && ($attributeId ? Attribute::checkAttributeQty($attributeId, ShopgateItemsCartExportJson::DEFAULT_QTY_TO_CHECK) : $product->checkQty(ShopgateItemsCartExportJson::DEFAULT_QTY_TO_CHECK)) || Product::isAvailableWhenOutOfStock($product->out_of_stock) ? 1 : 0); } else { $cartItem->setError(ShopgateLibraryException::CART_ITEM_PRODUCT_NOT_FOUND); $cartItem->setErrorText(ShopgateLibraryException::getMessageFor($cartItem->getError())); } $result[] = $cartItem; } return $result; }