protected function setUp() { $this->_block = new Mage_Catalog_Block_Product_AbstractTestAbstract(); $this->_product = new Mage_Catalog_Model_Product(); $this->_product->load(1); $this->_product->addData(array('image' => '/m/a/magento_image.jpg', 'small_image' => '/m/a/magento_image.jpg', 'thumbnail' => '/m/a/magento_image.jpg')); $this->_block->setProduct($this->_product); }
protected function setUp() { if (!self::$_isStubClass) { $this->getMockForAbstractClass('Mage_Catalog_Block_Product_Abstract', array(), self::STUB_CLASS, false); self::$_isStubClass = true; } $this->_block = Mage::app()->getLayout()->createBlock(self::STUB_CLASS); $this->_product = Mage::getModel('Mage_Catalog_Model_Product'); $this->_product->load(1); $this->_product->addData(array('image' => '/m/a/magento_image.jpg', 'small_image' => '/m/a/magento_image.jpg', 'thumbnail' => '/m/a/magento_image.jpg')); $this->_block->setProduct($this->_product); }
public static function setUpBeforeClass() { // image fixtures self::$_fixtureMediaDir = Mage::getSingleton('Mage_Catalog_Model_Product_Media_Config')->getBaseMediaPath(); mkdir(self::$_fixtureMediaDir . '/m/a', 0777, true); $fixtureDir = dirname(dirname(__FILE__)) . '/_files'; copy("{$fixtureDir}/magento_image.jpg", self::$_fixtureMediaDir . '/m/a/magento_image.jpg'); copy("{$fixtureDir}/magento_small_image.jpg", self::$_fixtureMediaDir . '/m/a/magento_small_image.jpg'); copy("{$fixtureDir}/magento_thumbnail.jpg", self::$_fixtureMediaDir . '/m/a/magento_thumbnail.jpg'); // watermark fixture mkdir(self::$_fixtureMediaDir . '/watermark/stores/' . Mage::app()->getStore()->getId(), 0777, true); copy("{$fixtureDir}/watermark.jpg", self::$_fixtureMediaDir . '/watermark/stores/' . Mage::app()->getStore()->getId() . '/watermark.jpg'); // sample product with images self::$_product = new Mage_Catalog_Model_Product(); self::$_product->addData(array('image' => '/m/a/magento_image.jpg', 'small_image' => '/m/a/magento_small_image.jpg', 'thumbnail' => '/m/a/magento_thumbnail.jpg')); // sample image cached URL $helper = new Mage_Catalog_Helper_Image(); self::$_sampleCachedUrl = (string) $helper->init(self::$_product, 'image'); }
/** * extract the giftcard tender code from the DOMNOdeList object * then get a configuration array of eb2c key tender type map to Magento giftcard type * check if the extracted giftcard tender code is a key the array of configuration of tender type map * then proceed to add lifetime, is redeemable, email template flag to true in the given product object * and then return the value from get giftcard type call * if the key is not found in the map simply return null * @param DOMNodeList $nodes * @param Mage_Catalog_Model_Product $product * @return int|null integer value a valid tender type was extracted null tender type is not configure */ public function extractGiftcardTenderValue(DOMNodeList $nodes, Mage_Catalog_Model_Product $product) { $value = Mage::helper('eb2ccore')->extractNodeVal($nodes); $mapData = $this->_getGiftCardMap(); if (isset($mapData[$value])) { $product->addData(array('use_config_lifetime' => true, 'use_config_is_redeemable' => true, 'use_config_email_template' => true)); return $this->_getGiftCardType($mapData[$value]); } return null; }
public function createProduct(&$item, $asid) { $logFileName = Mage::getSingleton('core/session')->getEmailID(); $attributeSetModel = Mage::getModel('eav/entity_attribute_set'); $attributeSetModel->load($asid); if (count($attributeSetModel->getData()) > 0) { $p_status = (string) $item->isActive == 'Y' ? 1 : 2; $p_taxclass = (string) $item->isTaxable == 'Y' ? 2 : 0; $product = new Mage_Catalog_Model_Product(); $product->setTypeId('simple'); $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); //New and created data code start $format = 'Y-m-d H:i:s'; $catalogNewproductDays = Mage::getStoreConfig('catalog/newproduct/days', Mage::app()->getStore()); if (!empty($catalogNewproductDays) && $catalogNewproductDays >= 0) { $currenDateTime = date("Y-m-d H:i:s", Mage::getModel('core/date')->timestamp(time())); $new_from_date = date($format, strtotime('1 days' . $currenDateTime)); $new_to_date = date($format, strtotime($catalogNewproductDays . ' days' . $new_from_date)); $product->setNewsFromDate($new_from_date); $product->setNewsToDate($new_to_date); } if ($product->getCreatedTime == NULL || $product->getUpdateTime() == NULL) { $product->setCreatedTime($currenDateTime)->setUpdateTime($currenDateTime); } //New and created data code end $product->setSku((string) $item->id); //Product custom id $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())); //Default website (main website) ?? To Do : make it dynamic $product->setStoreIDs(array($this->_store_id)); // Default store id . $inventory = $item->inventory; $manageItem = (string) $inventory->manageStock; $manageItem = strtoupper($manageItem); if ($manageItem == 'Y' && strtoupper($inventory->allowBackorders) == 'Y') { $product->setStockData(array('is_in_stock' => 1, 'qty' => $inventory->atp, 'manage_stock' => 1, 'use_config_backorders' => 0, 'backorders' => 1)); } elseif ($manageItem == 'Y') { $product->setStockData(array('is_in_stock' => 1, 'qty' => $inventory->atp, 'use_config_backorders' => 0, 'manage_stock' => 1)); } elseif ($manageItem == 'N') { $product->setStockData(array('use_config_backorders' => 0, 'manage_stock' => 0)); } $product->setAttributeSetId($asid); $product->setData('name', (string) $item->name); $product->setPrice((double) $item->price); $splAmt = (array) $item->specialPrice->amount; if (isset($item->specialPrice->amount) && $item->specialPrice->amount != NULL) { if (!empty($splAmt)) { $product->setSpecialPrice((double) $item->specialPrice->amount); } //special price in form 11.22 } $fromDate = (array) $item->specialPrice->fromDateTime; if (isset($item->specialPrice->fromDateTime) && $item->specialPrice->fromDateTime != NULL) { if (!empty($fromDate)) { $product->setSpecialFromDate(Mage::helper('customimport')->getCurrentLocaleDateTime($item->specialPrice->fromDateTime)); } //special price from (MM-DD-YYYY) } $toDate = (array) $item->specialPrice->toDateTime; if (isset($item->specialPrice->toDateTime) && $item->specialPrice->toDateTime != NULL) { if (!empty($toDate)) { $product->setSpecialToDate(Mage::helper('customimport')->getCurrentLocaleDateTime($item->specialPrice->toDateTime)); } //special price to (MM-DD-YYYY) } $product->setWeight((double) $item->weight); $product->setStatus($p_status); $product->setTaxClassId($p_taxclass); $product->setDescription((string) $item->longDescription); $product->setShortDescription((string) $item->shortDescription); $product->setMetaTitle((string) $item->pageTitle); $product->setMetaKeyword((string) $item->metaKeywords); $product->setMetaDescription((string) $item->metaDescription); $product->setExternalImage((string) $item->originalImageUrl); $product->setExternalSmallImage((string) $item->largeImageUrl); $product->setExternalThumbnail((string) $item->smallImageUrl); $attributeValues = $item->attributeValues; $attributeOcuurance = array(); //stores no. of occurance for all attributes $configAttributeValue = array(); // will use to take value of attributes that ocuures once $multiple_values = array(); // stores an array of available values $i = 1; $model = Mage::getModel('catalog/resource_eav_attribute'); foreach ($attributeValues->attribute as $attr) { $loadedattr = $model->loadByCode('catalog_product', (string) $attr->id); $attr_type = $loadedattr->getFrontendInput(); if (array_key_exists((string) $attr->id, $attributeOcuurance)) { $multiple_values[(string) $attr->id][] = (string) $attr->valueDefId; $attributeOcuurance[(string) $attr->id] = (int) $attributeOcuurance[(string) $attr->id] + 1; if ($attr_type == 'text' || $attr_type == 'textarea') { $multiple_values[(string) $attr->id][] = (string) $attr->value; } } else { $multiple_values[(string) $attr->id][] = (string) $attr->valueDefId; $attributeOcuurance[(string) $attr->id] = $i; if ($attr_type == 'text' || $attr_type == 'textarea') { $multiple_values[(string) $attr->id][] = (string) $attr->value; } } } foreach ($multiple_values as $attribute_code => $attribute_values) { $loadedattr = $model->loadByCode('catalog_product', $attribute_code); $attr_id = $loadedattr->getAttributeId(); // attribute id of magento if (!$attr_id) { $this->customHelper->reportError($this->customHelper->__('Attribute %s is not available in magento. Hence skipping product # %s', $attribute_code, $item->id)); return; } else { $attr_type = $loadedattr->getFrontendInput(); if ($attr_type == 'select' && count($attribute_values) == 1) { $mapObj = Mage::getModel('customimport/customimport'); $option_id = $mapObj->isOptionExistsInAttribute($attribute_values[0], $attr_id); if ($option_id) { $product->setData($attribute_code, $option_id); } else { $this->customHelper->reportError($this->customHelper->__('Attribute %s has an undefined option value %s. Hence skipping product # %s', $attribute_code, $attribute_values[0], $item->id)); return; } } elseif ($attr_type == 'select' && count($attribute_values) > 1) { //multiple values for attribute which is not multiselect $this->customHelper->reportError($this->customHelper->__('Attribute %s can not have multiple values. Hence skipping product # %s', $attribute_code, $item->id)); return; } elseif ($attr_type == 'multiselect') { $multivalues = array(); foreach ($attribute_values as $value) { $mapObj = Mage::getModel('customimport/customimport'); $option_id = $mapObj->isOptionExistsInAttribute($value, $attr_id); if ($option_id) { $multivalues[] = $option_id; } else { $this->customHelper->reportError($this->customHelper->__('Attribute %s has an undefined option value %s. Hence skipping product id %s', $attribute_code, $value, $item->id)); return; } } $product->addData(array($attribute_code => $multivalues)); } elseif ($attr_type == 'text' || $attr_type == 'textarea') { $product->setData($attribute_code, $attribute_values[1]); } elseif ($attr_type == 'boolean') { $optVal = Mage::getSingleton('customimport/customimport')->getOptVal($attribute_values[0]); if (strtolower($optVal->getValue()) == 'y' || strtolower($optVal->getValue()) == 'yes') { $attOptVal = 1; } else { $attOptVal = 0; } $product->setData($attribute_code, $attOptVal); } } } try { Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $productId = $product->save()->getId(); if ($manageItem == 'N' || $manageItem == 'n') { $product->setStockData(array('use_config_backorders' => 0, 'is_in_stock' => 1, 'manage_stock' => 0)); //code for instock while update product $stockItem = Mage::getModel('cataloginventory/stock_item'); $stockItem->assignProduct($product); $stockItem->setData('use_config_manage_stock', 0); $stockItem->setData('manage_stock', 0); $stockItem->save(); $stockStatus = Mage::getModel('cataloginventory/stock_status'); $stockStatus->assignProduct($product); $stockStatus->saveProductStatus($product->getId(), 1); } if ($productId) { $this->_created_num++; unset($product); unset($multiple_values); unset($attributeOcuurance); return $productId; } else { $this->customHelper->reportError($this->customHelper->__('Skipped product due to some error while saving # %s', $item->id)); } } catch (Mage_Eav_Model_Entity_Attribute_Exception $e) { $this->customHelper->reportError($e->getMessage()); $this->customHelper->reportError($e->getAttributeCode()); $this->customHelper->sendLogEmail($this->logPath); } } else { $this->customHelper->reportError($this->customHelper->__('Attribute set ID # %s is missing. Hence skipped product # %s', $asid, $item->id)); } }
/** * Add permission index to product model * * @param Mage_Catalog_Model_Product $product * @param int $customerGroupId * @return Enterprise_CatalogPermissions_Model_Resource_Permission_Index */ public function addIndexToProduct($product, $customerGroupId) { $adapter = $this->_getReadAdapter(); $select = $adapter->select()->from(array('cat_index' => $this->getTable('catalog/category_product_index')), array())->joinLeft(array('perm' => $this->getTable('permission_index')), 'perm.category_id=cat_index.category_id AND perm.customer_group_id=:customer_group_id AND perm.website_id=:website_id', $this->_getPermColumns())->where('product_id = :product_id')->where('customer_group_id = :customer_group_id OR customer_group_id IS NULL')->where('store_id = :store_id')->where('cat_index.is_parent=1')->group('cat_index.product_id'); $bind = array(':product_id' => $product->getId(), ':customer_group_id' => $customerGroupId, ':store_id' => $product->getStoreId(), ':website_id' => Mage::app()->getStore($product->getStoreId())->getWebsiteId()); if ($product->getCategory()) { $select->where('perm.category_id = :category_id'); $bind[':category_id'] = $product->getCategory()->getId(); } $permission = $adapter->fetchRow($select, $bind); if ($permission) { $product->addData($permission); } return $this; }
/** * Fill a product model with dummy data so that it can be saved and edited later. * @see http://www.magentocommerce.com/boards/viewthread/289906/ * @param Mage_Catalog_Model_Product $prod product model to be autofilled * @param string $sku the new product's sku * @param array $additionalData optional * @return Mage_Catalog_Model_Product */ protected function applyDummyData(Mage_Catalog_Model_Product $prod, $sku, array $additionalData = []) { $prodData = array_merge($this->getProdTplt(), $additionalData); $name = isset($prodData['name']) ? $prodData['name'] : null; $prodData['name'] = $name ?: "Incomplete Product: {$sku}"; $prodData['sku'] = $prodData['url_key'] = $sku; return $prod->addData($prodData); }
/** * Add permission index to product model * * @param Mage_Catalog_Model_Product $product * @param int $customerGroupId * @return Enterprise_CatalogPermissions_Model_Mysql4_Permission_Index */ public function addIndexToProduct($product, $customerGroupId) { $select = $this->_getReadAdapter()->select()->from($this->getTable('permission_index_product'), array('grant_catalog_category_view', 'grant_catalog_product_price', 'grant_checkout_items'))->where('product_id = ?', $product->getId())->where('customer_group_id = ?', $customerGroupId)->where('store_id = ?', $product->getStoreId()); if ($product->getCategory()) { $select->where('category_id = ?', $product->getCategory()->getId()); } else { $select->where('category_id IS NULL'); } $permission = $this->_getReadAdapter()->fetchRow($select); if ($permission) { $product->addData($permission); } return $this; }
/** * Set additional data before product saved * * @param Mage_Catalog_Model_Product $product * @param array $productData * @return object */ protected function _prepareDataForSave($product, $productData) { $product->addData($productData); if (isset($productData['website_ids']) && is_array($productData['website_ids'])) { $product->setWebsiteIds($productData['website_ids']); } if (isset($productData['websites']) && is_array($productData['websites'])) { foreach ($productData['websites'] as &$website) { if (is_string($website)) { try { $website = Mage::app()->getWebsite($website)->getId(); } catch (Exception $e) { } } } $product->setWebsiteIds($productData['websites']); } if (Mage::app()->isSingleStoreMode()) { $product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId())); } if (isset($productData['stock_data']) && is_array($productData['stock_data'])) { $product->setStockData($productData['stock_data']); } if (isset($productData['tier_price']) && is_array($productData['tier_price'])) { $tierPrices = Mage::getModel('catalog/product_attribute_tierprice_api')->prepareTierPrices($product, $productData['tier_price']); $product->setData(Mage_Catalog_Model_Product_Attribute_Tierprice_Api::ATTRIBUTE_CODE, $tierPrices); } }