/** * @depends testAssignProduct */ public function testRemoveProduct() { $this->assertNotEmpty($this->_model->assignedProducts(6)); $this->assertTrue($this->_model->removeProduct(6, 1)); $this->assertEmpty($this->_model->assignedProducts(6)); }
public function associateProductToCategory($productDetail, $catId) { $cat_api = new Mage_Catalog_Model_Category_Api(); $newProduct = Mage::getModel('catalog/product')->loadByAttribute('sku', (string) $productDetail->id); if ($newProduct) { $category = Mage::getModel('catalog/category')->setStoreId($this->_store_id)->load($catId); $positions = $category->getProductsPosition(); $productDetail->position; if ((string) $productDetail->isActive == 'Y') { // assignproduct is default function of magento, to assign product in a category $cat_api->assignProduct($catId, $newProduct->getId(), (double) $productDetail->position); } else { if (isset($positions[$newProduct->getId()])) { // removeProduct is default function of magento, to remove product from a category $cat_api->removeProduct($catId, $newProduct->getId()); } } } else { $this->customHelper->reportError($this->customHelper->__('Product does not exists %s', $productDetail->id)); } }
/** * Save product (import) * * @param array $importData * @throws Mage_Core_Exception * @return bool */ public function saveRow(array $importData) { $cronName = Mage::app()->getRequest()->getParam('cronname'); $use_file = false; $new_arrival_contentfeed = false; $filepath = Mage::getBaseDir('var') . '/content_feed_errorlog.csv'; if (!isset($cronName)) { $server = $_SERVER['argv'][1]; $server = explode("=", $server); if (isset($server[1])) { $cronName = $server[1]; } } Mage::getSingleton('core/session', array('name' => 'adminhtml')); //verify if the user is logged in to the backend if (Mage::getSingleton('admin/session')->isLoggedIn()) { //check whether it is an inventory run if (!isset($cronName)) { $use_file = true; $new_arrival_contentfeed = true; } } if ($use_file) { $handle = fopen($filepath, "a"); } $importData = array_map('trim', $importData); $product = $this->getProductModel()->reset(); if (empty($importData['store'])) { if (!is_null($this->getBatchParams('store'))) { $store = $this->getStoreById($this->getBatchParams('store')); } else { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'store'); Mage::throwException($message); } } else { $store = $this->getStoreByCode($importData['store']); } if ($store === false) { $message = Mage::helper('catalog')->__('Skipping import row, store "%s" field does not exist.', $importData['store']); Mage::throwException($message); } $_skuforuse = ''; if (empty($importData['sku'])) { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', 'sku'); if ($use_file) { fputcsv($handle, array('', '', '', '', 'SKU not defined')); fclose($handle); } Mage::throwException($message); } else { $_skuforuse = $importData['sku']; } $product->setStoreId($store->getId()); $productId = $product->getIdBySku($importData['sku']); if ($productId) { $product->load($productId); } else { $productTypes = $this->getProductTypes(); $productAttributeSets = $this->getProductAttributeSets(); /** * Check product define type */ if (empty($importData['type']) || !isset($productTypes[strtolower($importData['type'])])) { $value = isset($importData['type']) ? $importData['type'] : ''; $message = Mage::helper('catalog')->__('Skipping import row, following SKU: "%s" is not present in Magento.', $_skuforuse); if ($use_file) { fputcsv($handle, array($_skuforuse, '', '', '', $message)); fclose($handle); } Mage::throwException($message); } $product->setTypeId($productTypes[strtolower($importData['type'])]); /** * Check product define attribute set */ if (empty($importData['attribute_set']) || !isset($productAttributeSets[$importData['attribute_set']])) { $value = isset($importData['attribute_set']) ? $importData['attribute_set'] : ''; $message = Mage::helper('catalog')->__('Skip import row, the value "%s" is invalid for field "%s"', $value, 'attribute_set'); Mage::throwException($message); } $product->setAttributeSetId($productAttributeSets[$importData['attribute_set']]); foreach ($this->_requiredFields as $field) { $attribute = $this->getAttribute($field); if (!isset($importData[$field]) && $attribute && $attribute->getIsRequired()) { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" for new products is not defined.', $field); Mage::throwException($message); } if (isset($importData[$field]) && $attribute && $attribute->getIsRequired() && trim($importData[$field]) == '') { $message = Mage::helper('catalog')->__('Skipping import row, value for required field "%s" for new products is not available.', $field); Mage::throwException($message); } } } if (!isset($cronName)) { $_feilds_left_blank = ''; $_Images_not_exist = ''; $_productType = $product->getTypeId(); foreach ($this->_contentRequiredFields as $field) { if ($field == 'color_swatch_image') { if ($_productType == 'simple') { if (isset($importData[$field])) { if (@trim($importData[$field]) == "") { $_feilds_left_blank .= $field . ','; } } } } elseif ($field == 'gallery') { if (isset($importData[$field])) { if (@trim($importData[$field]) == "") { $_feilds_left_blank .= $field . ','; } else { $galleryDataCheck = explode('|', $importData["gallery"]); foreach ($galleryDataCheck as $gallery_img) { if (!file_exists(Mage::getBaseDir('media') . DS . 'import' . $gallery_img)) { $_Images_not_exist .= $gallery_img . ','; } } } } } else { if (isset($importData[$field])) { if (@trim($importData[$field]) == "") { $_feilds_left_blank .= $field . ','; } } } } if ($_feilds_left_blank != '') { $_feilds_left_blank = substr($_feilds_left_blank, 0, strlen($_feilds_left_blank) - 1); if ($_Images_not_exist != '') { $_Images_not_exist = substr($_Images_not_exist, 0, strlen($_Images_not_exist) - 1); $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" are not defined , Following images are not found on server "%s" for following SKU "%s".', $_feilds_left_blank, $_Images_not_exist, $_skuforuse); } else { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" are not defined for following SKU "%s".', $_feilds_left_blank, $_skuforuse); } $_stockckeck = $product->getStockItem()->getIsInStock(); $_productType = $product->getTypeId(); if ($_productType == 'simple') { $parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); $parent_collection = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('entity_id', array('in' => $parent_ids))->addAttributeToSelect('sku'); $parentSkus = ''; $parent_skus = $parent_collection->getColumnValues('sku'); if (is_array($parent_skus) && !empty($parent_skus)) { foreach ($parent_skus as $skus) { $parentSkus .= $skus . ','; } } if ($parentSkus != '') { $parentSkus = substr($parentSkus, 0, strlen($parentSkus) - 1); } if ($_stockckeck == 1) { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'In Stock', $message)); } } else { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'Out Of Stock', $message)); } } } else { if ($_stockckeck == 1) { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'In Stock', $message)); } } else { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'Out Of Stock', $message)); } } } if ($use_file) { fclose($handle); } Mage::throwException($message); } else { if ($_Images_not_exist != '') { $_Images_not_exist = substr($_Images_not_exist, 0, strlen($_Images_not_exist) - 1); $message = Mage::helper('catalog')->__('Following images are not found on server "%s" for following SKU "%s".', $_Images_not_exist, $_skuforuse); $_stockckeck = $product->getStockItem()->getIsInStock(); $_productType = $product->getTypeId(); if ($_productType == 'simple') { $parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); $parent_collection = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('entity_id', array('in' => $parent_ids))->addAttributeToSelect('sku'); $parentSkus = ''; $parent_skus = $parent_collection->getColumnValues('sku'); if (is_array($parent_skus) && !empty($parent_skus)) { foreach ($parent_skus as $skus) { $parentSkus .= $skus . ','; } } if ($parentSkus != '') { $parentSkus = substr($parentSkus, 0, strlen($parentSkus) - 1); } if ($_stockckeck == 1) { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'In Stock', $message)); } } else { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'Out Of Stock', $message)); } } } else { if ($_stockckeck == 1) { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'In Stock', $message)); } } else { if ($use_file) { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'Out Of Stock', $message)); } } } if ($use_file) { fclose($handle); } //Mage::throwException($message); } } } elseif ($cronName == 'product_inventory') { foreach ($this->_inventoryRequiredFields as $field) { if (!isset($importData[$field])) { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', $field); Mage::throwException($message); } if (@trim($importData[$field]) == "") { $message = Mage::helper('catalog')->__('Skipping import row, value for required field "%s" is not available.', $field); Mage::throwException($message); } } } elseif ($cronName == 'price_update') { foreach ($this->_priceRequiredFields as $field) { if (!isset($importData[$field])) { $message = Mage::helper('catalog')->__('Skipping import row, required field "%s" is not defined.', $field); Mage::throwException($message); } if (@trim($importData[$field]) == "") { $message = Mage::helper('catalog')->__('Skipping import row, value for required field "%s" is not available.', $field); Mage::throwException($message); } } } $this->setProductTypeInstance($product); /* * New arrival CR Start */ if ($cronName == 'product_inventory') { $qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty(); if ($importData['qty'] > $qtyStock) { //$config_days = Mage::getStoreConfig('inventory/date_setting/old_after'); //value //$news_from_date = date('Y-m-d H:i:s', time()); //$news_to_date = date('Y-m-d H:i:s', mktime('23', '59', '59', date('m'), date('d') + $config_days, date('Y'))); $objConfigurableProduct = Mage::getModel('catalog/product_type_configurable'); $arrConfigurableProductIds = $objConfigurableProduct->getParentIdsByChild($product->getId()); foreach ($arrConfigurableProductIds as $configurableProductId) { $config_product = Mage::getModel('catalog/product')->load($configurableProductId); if ($config_product) { $categoryIds = $config_product->getCategoryIds(); foreach ($categoryIds as $categoryId) { $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryId); if ($category->getLevel() == '2') { $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('parent_id', $categoryId)->addAttributeToFilter('url_key', 'new-arrivals')->load(); $nacat_arr = $collection->getData(); $categoryIds[] = $nacat_arr['0']['entity_id']; } } $category_ids = implode(',', $categoryIds); $config_product->setCategoryIds($category_ids); //$config_product->setNewsFromDate($news_from_date); //$config_product->setNewsToDate($news_to_date); $config_product->save(); unset($config_product); } } } } /* * New arrival CR End */ if (isset($importData['category_ids'])) { $product->setCategoryIds($importData['category_ids']); } foreach ($this->_ignoreFields as $field) { if (isset($importData[$field])) { unset($importData[$field]); } } if ($store->getId() != 0) { $websiteIds = $product->getWebsiteIds(); if (!is_array($websiteIds)) { $websiteIds = array(); } if (!in_array($store->getWebsiteId(), $websiteIds)) { $websiteIds[] = $store->getWebsiteId(); } $product->setWebsiteIds($websiteIds); } if (isset($importData['websites'])) { $websiteIds = $product->getWebsiteIds(); if (!is_array($websiteIds) || !$store->getId()) { $websiteIds = array(); } $websiteCodes = explode(',', $importData['websites']); foreach ($websiteCodes as $websiteCode) { try { $website = Mage::app()->getWebsite(trim($websiteCode)); if (!in_array($website->getId(), $websiteIds)) { $websiteIds[] = $website->getId(); } } catch (Exception $e) { } } $product->setWebsiteIds($websiteIds); unset($websiteIds); } foreach ($importData as $field => $value) { if (in_array($field, $this->_inventoryFields)) { continue; } if (in_array($field, $this->_imageFields)) { continue; } if (is_null($value)) { continue; } $attribute = $this->getAttribute($field); if (!$attribute) { continue; } $isArray = false; $setValue = $value; if ($attribute->getFrontendInput() == 'multiselect') { $value = explode(self::MULTI_DELIMITER, $value); $isArray = true; $setValue = array(); } if ($value && $attribute->getBackendType() == 'decimal') { $setValue = $this->getNumber($value); } if ($attribute->usesSource()) { $options = $attribute->getSource()->getAllOptions(false); if ($isArray) { foreach ($options as $item) { if (in_array(strtolower($item['label']), array_map('strtolower', $value))) { $setValue[] = $item['value']; } } } else { $setValue = false; foreach ($options as $item) { if (strtolower($item['label']) == strtolower($value)) { $setValue = $item['value']; } } } } $product->setData($field, $setValue); } if (!$product->getVisibility()) { $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); } $stockData = array(); $inventoryFields = isset($this->_inventoryFieldsProductTypes[$product->getTypeId()]) ? $this->_inventoryFieldsProductTypes[$product->getTypeId()] : array(); foreach ($inventoryFields as $field) { if (isset($importData[$field])) { if (in_array($field, $this->_toNumber)) { $stockData[$field] = $this->getNumber($importData[$field]); } else { $stockData[$field] = $importData[$field]; } } } $product->setStockData($stockData); $imageData = array(); foreach ($this->_imageFields as $field) { if (!empty($importData[$field]) && $importData[$field] != 'no_selection') { if (!isset($imageData[$importData[$field]])) { $imageData[$importData[$field]] = array(); } $imageData[$importData[$field]][] = $field; } } foreach ($imageData as $file => $fields) { try { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $file, $fields, false); } catch (Exception $e) { } } if (!empty($importData['gallery'])) { $galleryData = explode('|', $importData["gallery"]); $attributes = $product->getTypeInstance()->getSetAttributes(); $gallery = $attributes['media_gallery']; $galleryRemoveData = $product->getMediaGallery(); foreach ($galleryRemoveData['images'] as $image) { $gallery->getBackend()->removeImage($product, $image['file']); } foreach ($galleryData as $gallery_img) { try { $media_flag = 0; if ($gallery_img == $importData["image"]) { $media_flag = 1; } if ($media_flag) { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, array('image', 'small_image', 'thumbnail'), false, false); } else { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $gallery_img, null, false, false); } } catch (Exception $e) { } } } // check for attribute set any other than Default $productId = $product->getIdBySku($importData['sku']); $new = true; // fix for duplicating attributes error if ($productId) { $product->load($productId); $new = false; // fix for duplicating attributes error } if (trim($importData[' product_type_id']) == 'configurable') { $product->setCanSaveConfigurableAttributes(true); $configAttributeCodes = $this->userCSVDataAsArray($importData['config_attributes']); $usingAttributeIds = array(); foreach ($configAttributeCodes as $attributeCode) { $attribute = $product->getResource()->getAttribute($attributeCode); if ($attribute) { if ($product->getTypeInstance()->canUseAttribute($attribute)) { if ($new) { // fix for duplicating attributes error $usingAttributeIds[] = $attribute->getAttributeId(); } } } } if (!empty($usingAttributeIds)) { $product->getTypeInstance()->setUsedProductAttributeIds($usingAttributeIds); $product->setConfigurableAttributesData($product->getTypeInstance()->getConfigurableAttributesAsArray()); $product->setCanSaveConfigurableAttributes(true); $product->setCanSaveCustomOptions(true); } if (isset($importData['associated'])) { $product->setConfigurableProductsData($this->skusToIds($importData['associated'], $product)); } } // color swatch images if (!empty($importData['color_swatch_image']) && file_exists(Mage::getBaseDir('media') . DS . 'import' . $importData['color_swatch_image'])) { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $importData['color_swatch_image'], array('color_swatch_image'), false, true); } // hover images if (!empty($importData['hover_image']) && file_exists(Mage::getBaseDir('media') . DS . 'import' . $importData['hover_image'])) { $product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import' . $importData['hover_image'], array('hover_image'), false, true); } /* Content feed now being used to run-add products in new arrival */ // first check is made to check if status feild is coming in import data to see its a content feed or not. if (isset($importData['status'])) { //second check is that to see if it is run by admin if ($new_arrival_contentfeed) { if ($product && $product->getTypeId() == "configurable") { $categoryIds = $product->getCategoryIds(); $news_from_date = date('Y-m-d H:i:s', time()); foreach ($categoryIds as $categoryId) { $category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryId); if ($category->getLevel() == '2') { $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('parent_id', $categoryId)->addAttributeToFilter('url_key', 'new-arrivals')->load(); $nacat_arr = $collection->getData(); $categoryIds[] = $nacat_arr['0']['entity_id']; } } if (isset($importData['category_ids'])) { $category_ids_sheet = explode(",", $importData['category_ids']); $merged_arrays = array_unique(array_merge($category_ids_sheet, $categoryIds)); $category_ids = implode(',', $merged_arrays); } else { $category_ids = implode(',', $categoryIds); } $product->setCategoryIds($category_ids); $product->setNewsFromDate($news_from_date); //$config_product->setNewsToDate($news_to_date); //$config_product->save(); //unset($config_product); } } } /* Content feed now being used to run-add products in new arrival */ $product->setIsMassupdate(true); $product->setExcludeUrlRewrite(true); $product->save(); if ($use_file) { try { $_stockckeck = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock(); } catch (Exception $e) { $_stockckeck = 0; } $_productType = $product->getTypeId(); if ($_productType == 'simple') { $parent_ids = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product->getId()); $parent_collection = Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('entity_id', array('in' => $parent_ids))->addAttributeToSelect('sku'); $parentSkus = ''; $parent_skus = $parent_collection->getColumnValues('sku'); if (is_array($parent_skus) && !empty($parent_skus)) { foreach ($parent_skus as $skus) { $parentSkus .= $skus . ','; } } if ($parentSkus != '') { $parentSkus = substr($parentSkus, 0, strlen($parentSkus) - 1); } if ($_stockckeck == 1) { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'In Stock', 'No Error Occured')); } else { fputcsv($handle, array($_skuforuse, $_productType, $parentSkus, 'Out Of Stock', 'No Error Occured')); } } else { if ($_stockckeck == 1) { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'In Stock', 'No Error Occured')); } else { fputcsv($handle, array($_skuforuse, $_productType, $_skuforuse, 'Out Of Stock', 'No Error Occured')); } } fclose($handle); } // Store affected products ids $this->_addAffectedEntityIds($product->getId()); /* Section added to position products in various categories programatically */ //First check added to whether the product is configurable or not $_type = $product->getTypeId(); if ($_type == "configurable") { // Second check is made to see whether position has been specified in sheet for change or not if (isset($importData['position']) && $importData['position'] == "Yes") { // Now we find the categories that product he is assigned to $categoryIds = $product->getCategoryIds(); $cat_api = new Mage_Catalog_Model_Category_Api(); $_write_connection = Mage::getSingleton('core/resource')->getConnection('core_write'); $_read_connection = Mage::getSingleton('core/resource')->getConnection('core_read'); foreach ($categoryIds as $categoryId) { $_query_read = 'SELECT MAX(`position`) AS position FROM `catalog_category_product` WHERE `category_id=`' . $categoryId; //$category = Mage::getModel('catalog/category')->load($categoryId); $_get_values_in_DB = $_read_connection->fetchAll($_query_read); if (count($_get_values_in_DB) > 0) { $position = $_get_values_in_DB['position']; $cat_api->assignProduct($categoryId, $product->getId(), $position + 1); } } unset($cat_api); } } /* Section added to position products in various categories programatically */ // Save custom options prices for configurable product if ($_type == "configurable") { $attribute_price_rows = array(); if (isset($importData['config_attribute_values']) && trim($importData['config_attribute_values']) != '') { $attribute_price_rows = array(); $main_attributes_sets = explode('|', $importData['config_attribute_values']); $output1 = print_r($main_attributes_sets, true); file_put_contents('/tmp/output.txt', 'Output1' . $output1, FILE_APPEND); if (!empty($main_attributes_sets)) { foreach ($main_attributes_sets as $main_attributes_set) { $sub_attributes_sets = explode(':', $main_attributes_set); if (!empty($sub_attributes_sets) && count($sub_attributes_sets) == 4) { if ($attribute_id = $this->getProductAttributeId(strtolower(trim($sub_attributes_sets[0])))) { if ($attribute_option_id = $this->getProductOptionIdFromAttributeId($attribute_id, trim($sub_attributes_sets[1]))) { $is_percent = strtolower(trim($sub_attributes_sets[3])) == 'fixed' ? 0 : strtolower(trim($sub_attributes_sets[3])) == 'percentage' ? 1 : 0; $attribute_price_rows[$attribute_id][] = array('attribute_id' => $attribute_id, 'value_index' => $attribute_option_id, 'pricing_value' => trim($sub_attributes_sets[2]), 'is_percent' => $is_percent); } // END if($attribute_option_id = $this->getProductOptionIdFromAttributeId($attribute_id, trim($sub_attributes_sets[1]))) } // END if($attribute_id = $this->getProductAttributeId(strtolower(trim($sub_attributes_sets[0])))) } // END if(!empty($sub_attributes_sets)) } // END foreach ($main_attributes_sets as $main_attributes_set) } // END if(!empty($main_attributes_sets)) } // END if(isset($importData['config_attribute_values']) && trim($importData['config_attribute_values']) != '') if (!empty($attribute_price_rows)) { //print_r($attribute_price_rows);die; foreach ($attribute_price_rows as $attribute_id => $attribute_price_row) { $read = Mage::getSingleton('core/resource')->getConnection('core_read'); $product_super_attribute_id = $read->fetchOne("SELECT `product_super_attribute_id` FROM catalog_product_super_attribute WHERE product_id = " . $product->getEntityId() . " AND attribute_id = " . $attribute_id); $insert = Mage::getSingleton('core/resource')->getConnection('core_write'); if ($product_super_attribute_id) { $insert->query("DELETE FROM catalog_product_super_attribute_pricing WHERE `product_super_attribute_id` = {$product_super_attribute_id}"); if (!empty($attribute_price_row) && count($attribute_price_row) > 0) { foreach ($attribute_price_row as $row) { $insert->query("INSERT INTO `catalog_product_super_attribute_pricing` (`value_id` ,`product_super_attribute_id` ,`value_index` ,`is_percent` ,`pricing_value` ,`website_id`) VALUES ( NULL , '{$product_super_attribute_id}', '" . $row['value_index'] . "', '" . $row['is_percent'] . "', '" . $row['pricing_value'] . "', '0')"); } } } // END if($product_super_attribute_id) } // END foreach ($attribute_price_rows as $attribute_id=>$attribute_price_row) } // END if(!empty($attribute_price_rows)) } return true; }