import images via configurable csv - magento

I want to import Configurable Product along with their associative products and images.
I imported the products it associative products are coming in configurable product but images are not uploaded for product.
can anyone help me out??

First save your product with all data without images, then load your product by sku
$image = 'abc.jpg';
$importDir = Mage::getBaseDir('media') . DS . 'import/'; //put your images in this folder
$imgpath = $importDir.$image;
$ConfigproductImg = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //load product by sku
$simpleproductImg->setMediaGallery(array('images' => array(), 'values' => array()));//
$simpleproductImg->addImageToMediaGallery($imgpath, array('image','thumbnail','small_image'), false, false); // set images to media gallery
$simpleproductImg->save(); //save product

Related

how to assign random product positions in Magento category?

I have the following issue in Magento 1.9: there are categories with a lot of products in them and the default sort order is by position and all product position in Magento backend is 1. So what happens is that when you open a category on the frontend a lot of similar products show first followed by another set of similar type of products. I want to randomize their positions somehow (by a script or anything else) so that different kind of products show mixed together.
For example I have categories wine,
champagne and whisky and products
in them and I also have category birthday products that includes products from these categories. And when the category is opened on the frontend first a lot of whisky products
are shown then a lot of wine ... etc I want them mixed. Thanks in advance for
any help!
You can do as below :
First get the category :
$category = Mage::getModel('catalog/category')
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->load($categoryId);
Then its product positions :
$products = $category->getProductsPosition();
This will be an array organized like this :
product_id_1 => position_1
product_id_1 => position_2
So foreach one of those products just set a random position (here between 0 and 9999) :
foreach($products as $productId => $position ){
$products[$productId] = '' . rand(0,9999);
}
And finally save :
$category->setPostedProducts($products);
$category->save();
Here is below a script that you could put in /shell magento directory :
<?php
require_once './abstract.php';
class RandomCategoryOrder extends Mage_Shell_Abstract {
private $_categoryId = 188;
public function run(){
$category = Mage::getModel('catalog/category')
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->load($this->_categoryId);
$products = $category->getProductsPosition();
foreach($products as $productId => $position ){
$products[$productId] = '' . rand(0,9999);
}
$category->setPostedProducts($products);
try{
$category->save();
}catch(Exception $e){
echo $e->getMessage();
}
}
}
$randowCategoryOrder = new RandomCategoryOrder();
$randowCategoryOrder->run();

Show Product's Custom Attributes to Homepage or static page

I want to echo the custom attributes to any other page, except Product Page or Category Listing
One way of getting custom attribute in nearly any page can be done as following (in PHP):
$product_id = $this->getProduct()->getId(); // note that this changes depending on how you load your product
$storeId = Mage::app()->getStore()->getStoreId();
$value = Mage::getResourceModel('catalog/product')->getAttributeRawValue($product_id, 'your_power_attribute_id', $storeId);
echo $value;

Magento: how to use catalogsearch/query model to find products without images?

This is my code:
$catalogSearchModel = Mage::getModel('catalogsearch/query')->setQueryText($q);
$catalogSearchModelCollection = $catalogSearchModel->getResultCollection();
$catalogSearchModelCollection->getSelect()->limit(5);
$results = $catalogSearchModelCollection->getData();
What should I add to this to only find products with images?
Use following code for product which have images.
$catalogSearchModel = Mage::getModel('catalogsearch/query')->setQueryText($q);
$catalogSearchModelCollection = $catalogSearchModel->getResultCollection();
$catalogSearchModelCollection->addAttributeToFilter('image', array('neq' => 'no_selection');
$catalogSearchModelCollection->getSelect()->limit(5);
$results = $catalogSearchModelCollection->getData();

Magento - Load products by ajax in current template design

I am building a magento extension in which I need to show all products in tree order on left bar and then click on each category will load products of category by sending ajax request. To display category tree I used a block.
I am new to magento so I thought to send ajax request to my controller, get all products data as JSON and create HTML on front end to display products.
I took the HTML of base theme with 2 column left bar of products listing and used in ajax method. I used like
function loadCategoryProducts(categoryId) {
jQuery.ajax({
url: '/myModule/index/loadcategoryproduct',
type: 'post',
data: {categoryId: categoryId},
success: function(products) {
products = jQuery.parseJSON(products);
if (products != '') {
var html = '';
html += '<div class="page-title category-title">';
// blah blah blah blah blah to draw html from ajax request
}
html += '</ul>';
html += '</div>' // end of category-products product
jQuery('.col-main').html(html);
}
}
});
}
And my controller code
public function loadcategoryproductAction() {
$id = $this->getRequest()->getParam('categoryId');
if ($id) {
$_category = Mage::getModel('catalog/category')->load($id);
$product = Mage::getModel('catalog/product');
//load the category's products as a collection
$_productCollection = $product->getCollection()
->addAttributeToSelect('*')
->addCategoryFilter($_category)
->load();
// build an array for conversion
$json_products = array();
foreach ($_productCollection as $_product) {
$_product->getData();
$json_products[] = array(
'name' => $_product->getName(),
'url' => $_product->getProductUrl(),
'description' => nl2br($_product->getShortDescription()),
'price' => $_product->getFormatedPrice(),
'image' => $_product->getImageUrl());
}
$data = json_encode($json_products);
echo $data;
die;
}
}
It worked great but I know it is just a static thing as I am using HTML and CSS of base theme. It will only work in this theme. What If the theme of the magento store is changed?
So what I am looking is how to display products in current active theme? I just send ajax request to my controller, get products and these product must be rendered in current theme layout? There must be a way to do this that I still couldn't be able to find. I googled but all in vain. I don't like to use any built in extension for it. I want my extension to do it all. Please guide.
Ok, I have figured out the solution. You just need to get the category and set in registry as current category. Then you need to get current active template. For products listing Magento always check list.phtml file of current active theme. So here are the steps
Get Category
Register your category as current category.
Get current template list.phtml file and pass your category to that file
Leave the rest to Magento, It can handle it because It is Magento :-)
Here is the code that will replace my controller code
$id = $this->getRequest()->getParam('categoryId');
$category = Mage::getModel('catalog/category') ->setStoreId(Mage::app()->getStore()->getId()) ->load($id);
Mage::unregister('current_category');
Mage::register('current_category', $category);
echo $this->getLayout() ->createBlock('catalog/product_list') ->setTemplate('catalog/product/list.phtml') ->setCategoryId($id) ->toHtml();
die;
This will return the product HTML, and I will just need to put the HTML in success function of my ajax request.
success : function(products) {
jQuery('col-main').html(products)
}
And that is it. :-)

Adding magento product from frontend with attributes

How to add product from frontend with custom attributes ?
I have this code form another stack question
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
echo time();
// Build the product
$product->setAttributeSetId(9);// #4 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setSku(time());
$product->setWeight(4.0000);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);//4
print_r(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setPrice(39.99);// # Set some price
$product->setTaxClassId(0);// # default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
$product->setCategoryIds(array(27));// # some cat id's,
$product->setWebsiteIDs(array(1));// # Website id, 1 is default
//Default Magento attribute
$product->setCreatedAt(strtotime('now'));
//print_r($product);
try {
$product->save();
echo "Product Created";
}
catch (Exception $ex) {
//Handle the error
echo "Product Creation Failed";
}
But i have custom attributes also , and how to add them from that code.
For each attribute you have you need to call:
$product->setData('attribute_code_here', 'Value here');
[Edit]
For yes/no attributes do it like this:
$product->setData('attribute_code_here', 1); //1 for Yes, 0 for No
For multiple selects
$product->setData('attribute_code_here', "4,6,12"); //the ids of the values concatenated by comma.
First add a product with all attributes per hand into your Magento, so that you can figure out how Magento uses them. Load that Product and print_r all variables, then use them to save a new product.
$_product = Mage::getModel('catalog/product')->load('PRODUCT ID');
Zend_Debug::dump($_product);
Get All the Attributes you need to save a new Product and do that:
$_product = Mage::getModel('catalog/product');
$_product->setYourAttribute('...');
$_product->save();

Resources