Esempio n. 1
0
 public function getItemBaseInfo()
 {
     $helper = Mage::helper('catalog/image');
     $helper->init($this->product, $this->getProductImageType())->keepAspectRatio($this->_keepAspectRatio)->keepFrame($this->_keepFrame);
     $this->item['item_id'] = $this->product->getId();
     $this->item['item_title'] = $this->product->getName();
     $this->item['item_url'] = $this->product->getProductUrl();
     $this->item['cid'] = $this->product->getCategoryIds();
     $this->item['qty'] = $this->product->getStockItem()->getQty();
     $this->item['thumbnail_pic_url'] = (string) $helper->resize($this->thumbnail_with, $this->thumbnail_height);
     $this->item['main_pic_url'] = (string) $helper->resize($this->main_with, $this->main_height);
     $this->item['is_virtual'] = $this->product->isVirtual();
     $this->item['item_status'] = $this->product->isSaleable() ? 'instock' : 'outofstock';
     if (!$this->product->getRatingSummary()) {
         Mage::getModel('review/review')->getEntitySummary($this->product, Mage::app()->getStore()->getId());
     }
     $this->item['allow_add_to_cart'] = !$this->HasOptions();
     $this->item['rating_score'] = round((int) $this->product->getRatingSummary()->getRatingSummary() / 20);
     $this->item['rating_count'] = $this->product->getRatingSummary()->getReviewsCount();
     $this->item['sales_type'] = $this->product->isInStock() ? 'stock' : 'distribute';
     $this->item['qty_min_unit'] = 1;
     $stockItem = $this->product->getStockItem();
     if ($stockItem) {
         if ($stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0) {
             $this->item['qty_min_unit'] = $stockItem->getMinSaleQty();
         }
     }
     $this->item['item_type'] = $this->product->getTypeId();
     return $this->item;
 }
Esempio n. 2
0
 /**
  * Set current attribute to entry (for specified product)
  *
  * @param Mage_Catalog_Model_Product $product
  * @param Varien_Gdata_Gshopping_Entry $entry
  * @return Varien_Gdata_Gshopping_Entry
  */
 public function convertAttribute($product, $entry)
 {
     $quantity = $product->getStockItem()->getQty();
     if ($quantity) {
         $value = $quantity ? max(1, (int) $quantity) : 1;
         $this->_setAttribute($entry, 'quantity', self::ATTRIBUTE_TYPE_INT, $value);
     }
     return $entry;
 }
Esempio n. 3
0
 /**
  * Get MinSaleQty for product
  *
  * @param Mage_Catalog_Model_Product $product
  * @return int|null
  */
 protected function _getMinimalQty($product)
 {
     if ($stockItem = $product->getStockItem()) {
         if ($stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0) {
             return $stockItem->getMinSaleQty() * 1;
         }
     }
     return null;
 }
Esempio n. 4
0
 /**
  * Gets minimal sales quantity
  *
  * @param Mage_Catalog_Model_Product $product
  * @return int|null
  */
 public function getMinimalQty($product)
 {
     $stockItem = $product->getStockItem();
     if ($stockItem) {
         return $stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null;
     }
     return null;
 }
Esempio n. 5
0
 /**
  * Set additional data before product save
  *
  * @param Mage_Catalog_Model_Product $product
  * @param array $productData
  */
 protected function _prepareDataForSave($product, $productData)
 {
     if (isset($productData['stock_data'])) {
         if (!$product->isObjectNew() && !isset($productData['stock_data']['manage_stock'])) {
             $productData['stock_data']['manage_stock'] = $product->getStockItem()->getManageStock();
         }
         $this->_filterStockData($productData['stock_data']);
     } else {
         $productData['stock_data'] = array('use_config_manage_stock' => 1, 'use_config_min_sale_qty' => 1, 'use_config_max_sale_qty' => 1);
     }
     $product->setStockData($productData['stock_data']);
     // save gift options
     $this->_filterConfigValueUsed($productData, array('gift_message_available', 'gift_wrapping_available'));
     if (isset($productData['use_config_gift_message_available'])) {
         $product->setData('use_config_gift_message_available', $productData['use_config_gift_message_available']);
         if (!$productData['use_config_gift_message_available'] && $product->getData('gift_message_available') === null) {
             $product->setData('gift_message_available', (int) Mage::getStoreConfig(Mage_GiftMessage_Helper_Message::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, $product->getStoreId()));
         }
     }
     if (isset($productData['use_config_gift_wrapping_available'])) {
         $product->setData('use_config_gift_wrapping_available', $productData['use_config_gift_wrapping_available']);
         if (!$productData['use_config_gift_wrapping_available'] && $product->getData('gift_wrapping_available') === null) {
             $xmlPathGiftWrappingAvailable = 'sales/gift_options/wrapping_allow_items';
             $product->setData('gift_wrapping_available', (int) Mage::getStoreConfig($xmlPathGiftWrappingAvailable, $product->getStoreId()));
         }
     }
     if (isset($productData['website_ids']) && is_array($productData['website_ids'])) {
         $product->setWebsiteIds($productData['website_ids']);
     }
     // Create Permanent Redirect for old URL key
     if (!$product->isObjectNew() && isset($productData['url_key']) && isset($productData['url_key_create_redirect'])) {
         $product->setData('save_rewrites_history', (bool) $productData['url_key_create_redirect']);
     }
     /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
     foreach ($product->getTypeInstance(true)->getEditableAttributes($product) as $attribute) {
         //Unset data if object attribute has no value in current store
         if (Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID !== (int) $product->getStoreId() && !$product->getExistsStoreValueFlag($attribute->getAttributeCode()) && !$attribute->isScopeGlobal()) {
             $product->setData($attribute->getAttributeCode(), false);
         }
         if ($this->_isAllowedAttribute($attribute)) {
             if (isset($productData[$attribute->getAttributeCode()])) {
                 $product->setData($attribute->getAttributeCode(), $productData[$attribute->getAttributeCode()]);
             }
         }
     }
 }
Esempio n. 6
0
 protected function _getQuantity()
 {
     $stockData = $this->_product->getStockData();
     return (int) $this->_product->getStockItem()->getQty();
     //(int)$stockData['qty'];
 }
Esempio n. 7
0
 protected function _updateProductData($productId, $field, $value)
 {
     $this->_initProduct($productId, $field);
     if ($this->_product) {
         $result = array();
         switch ($field) {
             case 'custom_name':
                 $field = 'name';
                 break;
         }
         $store = $this->_getStore();
         $columnProps = $this->_getColumnProperties();
         $obj = $this->_product;
         if (isset($columnProps[$field])) {
             /* will save value. first need to check where to save (product itself, stock item, etc.)
              * @see Amasty_Pgrid_Helper_Data
              */
             $obj = $this->_getObject($columnProps[$field]);
             if (isset($columnProps[$field]['format'])) {
                 switch ($columnProps[$field]['format']) {
                     case 'numeric':
                         if (false !== strpos($value, '+') || false !== strpos($value, '-')) {
                             if (strpos($value, '+') != strlen($value) - 1 && strpos($value, '-') != strlen($value) - 1) {
                                 $value = preg_replace('@[^0-9\\.+-]@', '', $value);
                                 try {
                                     $toEval = '$value = ' . $value . ';';
                                     eval($toEval);
                                 } catch (Exception $e) {
                                 }
                             }
                         }
                         $symbol = Mage::app()->getLocale()->currency($this->_getStore()->getBaseCurrency()->getCode())->getSymbol();
                         if ($value !== "") {
                             $value = str_replace($symbol, '', $value);
                             $value = Mage::app()->getLocale()->getNumber($value);
                         } else {
                             $value = NULL;
                         }
                         break;
                 }
             }
             if (!empty($value)) {
                 if ('multiselect' == $columnProps[$field]['type']) {
                     $value = explode(',', $value);
                 }
                 if ('price' == $columnProps[$field]['type']) {
                     $value = str_replace('$', '', $value);
                 }
                 if ($columnProps[$field]['type'] == 'date') {
                     $value = Mage::app()->getLocale()->date(strtotime($value), null, null)->getTimestamp();
                     $value = date("Y-m-d", $value);
                 }
             }
             $obj->setData($columnProps[$field]['col'], $value);
             try {
                 if (method_exists($obj, 'validate')) {
                     $obj->validate();
                     // this will validate necessary unique values
                 }
             } catch (Exception $e) {
                 $result = array('error' => 1, 'message' => 'ID ' . $productId . ': ' . $e->getMessage(), "\r\n");
             }
             if (!isset($result['error'])) {
                 if (Mage::getStoreConfig('ampgrid/cond/availability')) {
                     if ('qty' == $columnProps[$field]['col']) {
                         if ($obj->getOrigData('qty') > 0 && $obj->getData('qty') <= 0) {
                             $obj->setData('is_in_stock', 0);
                         }
                         if ($obj->getOrigData('qty') <= 0 && $obj->getData('qty') > 0) {
                             $obj->setData('is_in_stock', 1);
                         }
                     }
                 }
                 if ($columnProps[$field]['col'] != 'visibility') {
                     $obj->setData('visibility', $obj->getOrigData('visibility'));
                 }
                 if ($columnProps[$field]['col'] != 'status') {
                     $obj->setData('status', $obj->getOrigData('status'));
                 }
                 if ($columnProps[$field]['col'] != 'tax_class_id') {
                     $obj->setData('tax_class_id', $obj->getOrigData('tax_class_id'));
                 }
                 $obj->save();
                 $this->_product->setData('updated_at', Mage::getModel('core/date')->timestamp(time()));
                 $this->_product->save();
                 $this->_initProduct($productId, $field);
                 $obj = $this->_getObject($columnProps[$field]);
             }
         }
         $indexer = Mage::getSingleton('index/indexer');
         if ($indexer) {
             $indexer->processEntityAction($this->_product, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);
             $indexer->processEntityAction($this->_product->getStockItem(), Mage_CatalogInventory_Model_Stock_Item::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);
         }
         if (!isset($result['error'])) {
             $outputValue = $obj->getData($columnProps[$field]['col']);
             if (isset($columnProps[$field])) {
                 switch ($columnProps[$field]['type']) {
                     case 'price':
                         if (!empty($outputValue)) {
                             if ($field == "special_price" && ($obj->getTypeID() == "bundle" || $obj->getTypeID() == "grouped")) {
                                 $outputValue = round($outputValue) . "%";
                             } else {
                                 $currencyCode = $store->getBaseCurrency()->getCode();
                                 $outputValue = sprintf("%f", $outputValue);
                                 $outputValue = Mage::app()->getLocale()->currency($currencyCode)->toCurrency($outputValue);
                             }
                         }
                         break;
                     case 'select':
                         if (isset($columnProps[$field]['options'][$outputValue])) {
                             $outputValue = $columnProps[$field]['options'][$outputValue];
                         }
                         break;
                     case 'multiselect':
                         $outputValues = explode(',', $outputValue);
                         if (is_array($outputValues) && !empty($outputValues)) {
                             foreach ($outputValues as &$value) {
                                 if (isset($columnProps[$field]['options'][$value])) {
                                     $value = $columnProps[$field]['options'][$value];
                                 }
                             }
                             $outputValue = implode(', ', $outputValues);
                         }
                         break;
                     case 'date':
                         $outputValue = Mage::helper('core')->formatDate($outputValue, 'medium', false);
                         break;
                     default:
                         $outputValue = htmlentities($outputValue, ENT_QUOTES, "UTF-8");
                         break;
                 }
             }
             $result = array('success' => 1, 'value' => $outputValue);
         }
     } else {
         $result = array('error' => 1, 'message' => $this->__('Unable to load product with ID %d', $productId) . "\r\n");
     }
     return $result;
 }
 /**
  * Determine if the passed in product is a backorderable product.
  *
  * @param  Mage_Catalog_Model_Product
  * @return bool
  */
 protected function isBackorderable(Mage_Catalog_Model_Product $product)
 {
     $stockItem = $product->getStockItem();
     return (int) $stockItem->getBackorders() > Mage_CatalogInventory_Model_Stock::BACKORDERS_NO;
 }
Esempio n. 9
0
 /**
  * fills the item-array available_text field
  * <strong>Note:</strong> the function isAvailable()  on the Product-Object is only available in Magento-Version >= 1.5
  *
  * @param Mage_Catalog_Model_Product $product
  * @param int $storeViewId
  *
  * @return string
  */
 public function getAvailableText($product, $storeViewId)
 {
     $availableText = "";
     $attributeCode = Mage::getStoreConfig(Shopgate_Framework_Model_Config::XML_PATH_SHOPGATE_EXPORT_AVAILABLE_TEXT_ATTRIBUTE_CODE);
     if ($attributeCode) {
         $attributeInputType = $product->getResource()->getAttribute($attributeCode)->getFrontendInput();
         $rawValue = $product->getResource()->getAttributeRawValue($product->getId(), $attributeCode, $storeViewId);
         switch ($attributeInputType) {
             case "select":
                 /** @var Mage_Eav_Model_Entity_Attribute_Option $attr */
                 $attr = Mage::getModel('eav/entity_attribute_option')->getCollection()->setStoreFilter($storeViewId)->join('attribute', 'attribute.attribute_id=main_table.attribute_id', 'attribute_code')->addFieldToFilter('main_table.option_id', array('eq' => $rawValue))->getFirstItem();
                 if ($attr) {
                     $availableText = $attr->getStoreValue() ? $attr->getStoreValue() : $attr->getValue();
                 }
                 break;
             case "date":
                 $availableText = Mage::helper('core')->formatDate($rawValue, 'medium', false);
                 break;
             case "text":
                 $availableText = $rawValue;
                 break;
             default:
         }
     }
     if (!$availableText) {
         /** @var Mage_CatalogInventory_Model_Stock_Item $stockItem */
         $stockItem = $product->getStockItem();
         $isAvailable = $stockItem->checkQty(1) && $product->isSaleable();
         if ($product->isComposite()) {
             $isAvailable = true;
         }
         if ($isAvailable && $stockItem->getIsInStock()) {
             if ($stockItem->getManageStock() && $stockItem->getBackorders() == Mage_CatalogInventory_Model_Stock::BACKORDERS_YES_NOTIFY && $stockItem->getQty() <= 0) {
                 $availableText = Mage::helper('shopgate')->__('Item will be backordered');
             } else {
                 $availableText = Mage::helper('shopgate')->__('In stock');
             }
         } else {
             $availableText = Mage::helper('shopgate')->__('Out of stock');
         }
     }
     return $availableText;
 }
Esempio n. 10
0
 /**
  * Check whether specified product is out of stock
  *
  * @param Mage_Catalog_Model_Product $product
  * @return bool
  */
 protected function _isProductOutOfStock($product)
 {
     if ($product->isComposite()) {
         if (!$product->getStockItem()->getIsInStock()) {
             return true;
         }
         $productsByGroups = $product->getTypeInstance(true)->getProductsToPurchaseByReqGroups($product);
         foreach ($productsByGroups as $productsInGroup) {
             foreach ($productsInGroup as $childProduct) {
                 if ($childProduct->hasStockItem() && $childProduct->getStockItem()->getIsInStock() && !$childProduct->isDisabled()) {
                     return false;
                 }
             }
         }
         return true;
     }
     /** @var $stockItem Mage_CatalogInventory_Model_Stock_Item */
     $stockItem = Mage::getModel('cataloginventory/stock_item');
     $stockItem->loadByProduct($product);
     $stockItem->setProduct($product);
     return !$stockItem->getIsInStock();
 }
Esempio n. 11
0
 /**
  * get product max order qty
  *
  * @param Mage_Catalog_Model_Product $product
  */
 protected function _setMaximumOrderQuantity($product)
 {
     $this->_defaultRow["maximum_order_quantity"] = $product->getStockItem()->getMaxSaleQty();
 }
Esempio n. 12
0
 /**
  * @param $inputs
  *
  * @return array
  */
 protected function _setBundleOptions($inputs = array())
 {
     $bundleOptions = $this->item->getPriceModel()->getOptions($this->item);
     $isGross = Mage::getStoreConfig(self::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $this->_getConfig()->getStoreViewId());
     $stock = parent::getStock();
     $selectionQuantities = array();
     foreach ($bundleOptions as $bundleOption) {
         $optionValues = array();
         /* @var $bundleOption Mage_Bundle_Model_Option */
         if (!is_array($bundleOption->getSelections())) {
             $stock->setIsSaleable(false);
             $bundleOption->setSelections(array());
         }
         $optionPrices = array();
         $cheapestOptionId = null;
         $cheapestPrice = null;
         // fetch id of cheapest option
         foreach ($bundleOption->getSelections() as $selection) {
             $selectionPrice = $this->item->getPriceModel()->getSelectionFinalPrice($this->item, $selection, 1, $selection->getSelectionQty());
             $selectionPrice = Mage::helper('tax')->getPrice($selection, $selectionPrice, $isGross);
             if ($cheapestPrice === null || $cheapestPrice > $selectionPrice) {
                 $cheapestPrice = $selectionPrice;
                 $cheapestOptionId = $selection->getSelectionId();
             }
             $optionPrices[$selection->getSelectionId()] = $selectionPrice;
         }
         foreach ($bundleOption->getSelections() as $selection) {
             $option = new Shopgate_Model_Catalog_Option();
             /** @var $selection Mage_Catalog_Model_Product */
             $selectionId = $selection->getSelectionId();
             $qty = max(1, (int) $selection->getSelectionQty());
             $selectionPrice = $optionPrices[$selectionId];
             $selectionName = $qty > 1 ? $qty . " x " : '';
             $selectionName .= $this->_getMageCoreHelper()->htmlEscape($selection->getName());
             if (!array_key_exists($selectionId, $selectionQuantities)) {
                 $selectionQuantities[$selectionId] = 0;
             }
             if ($this->item->getStockItem()->getManageStock() && $selection->isSaleable() && $this->item->getStockItem()->getQty() > 0) {
                 if ($selectionQuantities[$selectionId] !== null) {
                     $selectionQuantities[$selectionId] += $this->item->getStockItem()->getQty();
                 }
             } else {
                 if (!$this->item->getStockItem()->getManageStock()) {
                     $selectionQuantities[$selectionId] = null;
                 } else {
                     if (!$selection->isSaleable() && $this->item->getStockItem()->getBackorders()) {
                         $selectionQuantities[$selectionId] = null;
                     } else {
                         $selectionQuantities[$selectionId] = 0;
                     }
                 }
             }
             $option->setUid($selection->getSelectionId());
             $option->setLabel($selectionName);
             $option->setSortOrder($selection->getPosition());
             // reset selection price, in this case the bundle parent is already configured
             // with the price of the cheapest bundle configuration
             if ($this->item->getPriceType() == Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC) {
                 if ($cheapestOptionId == $selection->getSelectionId() && $selection->getOption()->getRequired()) {
                     $selectionPrice = 0;
                 } elseif ($selection->getOption()->getRequired()) {
                     $selectionPrice = $selectionPrice - $optionPrices[$cheapestOptionId];
                 }
             }
             $option->setAdditionalPrice($selectionPrice);
             $optionValues[] = $option;
             if ($selectionQuantities[$selectionId] === null) {
                 unset($selectionQuantities[$selectionId]);
             }
         }
         $inputItem = new Shopgate_Model_Catalog_Input();
         $inputItem->setUid($bundleOption->getId());
         $inputItem->setType($this->_mapInputType($bundleOption->getType()));
         $title = $bundleOption->getTitle() ? $bundleOption->getTitle() : $bundleOption->getDefaultTitle();
         $inputItem->setLabel($title);
         $inputItem->setValidation($this->_buildInputValidation($inputItem->getType(), null));
         $inputItem->setRequired($bundleOption->getRequired());
         $inputItem->setSortOrder($bundleOption->getPosition());
         $inputItem->setOptions($optionValues);
         $inputs[] = $inputItem;
     }
     $stockQty = count($selectionQuantities) ? min($selectionQuantities) : 0;
     $stock->setStockQuantity($stockQty);
     if (!count($selectionQuantities)) {
         $stock->setUseStock(false);
     }
     $this->setStock($stock);
     return $inputs;
 }
Esempio n. 13
0
 public function isUsableProduct(Mage_Catalog_Model_Product $product)
 {
     $stockItem = $product->getStockItem();
     if (!isset($stockItem)) {
         return false;
     }
     if ($this->isValidProductType($product) && $stockItem->getIsInStock()) {
         return true;
     }
 }
 /**
  * Update product
  * 
  * @param Mage_Catalog_Model_Product                    $magentoProduct Magento product
  * @param Nicovogelaar_Nedisimport_Model_Entity_Product $product        Product
  * 
  * @return void
  */
 protected function updateProduct(Mage_Catalog_Model_Product $magentoProduct, Nicovogelaar_Nedisimport_Model_Entity_Product $product)
 {
     $categoryIds = $magentoProduct->getCategoryIds();
     $newCategoryIds = $this->createCategories($product->getCategories());
     $categoryIds = array_unique(array_merge($categoryIds, $newCategoryIds));
     $magentoProduct->setUpdatedAt(strtotime('now'))->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)->setSku($product->getSku())->setName($product->getName())->setMsrp($product->getMsrp())->setDescription($product->getDescription())->setShortDescription($product->getShortDescription())->setCategoryIds($categoryIds);
     $stockItem = $magentoProduct->getStockItem();
     if ($stockItem) {
         $stockItem->setQty($product->getStockQty());
         $stockItem->setIsInStock($product->getIsInStock());
     } else {
         $this->createStockItem($magentoProduct, $product);
     }
     $this->save($magentoProduct);
 }