How can i update my products? - magento

I want to update from my script the meta title, the meta description and the meta keywords for all of my products.
The meta title = the name of the product
The meta description = the short description of the product
The meta keywords is the meta keywords i've put from the category of the product.
I can loop and get the informations i want to make the update (except the meta keywords of the description) and after how i can make the update inside the loop ?
Thanks
<?php require_once 'app/Mage.php';
umask(0);
set_time_limit(0); // ignore php timeout
ignore_user_abort(true); // keep on going even if user pulls the plug*
while(ob_get_level())ob_end_clean(); // remove output buffers
ob_implicit_flush(true); // output stuff directly
//error_reporting(E_ALL);
/* not Mage::run(); */
Mage::app('default');
// get product collection (All product)
$storeId = Mage::app()->getStore()->getId();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setStoreId($storeId)
->addStoreFilter($storeId);
foreach ($_productCollection as $pro) { // loop each product
$meta_title = $pro['meta_title'];
$meta_description = $pro['meta_description'];
$name = $pro['name'];
$short_description = $pro['short_description'];
// I want to update the meta_title, meta_description, meta_keywords
// meta_description = short_description
// meta_title = name
// meta_keywords = meta keywords from the category of the product
}
?>

You can easy set keyword and description by using standard setters.
Inside foreach loop just use :
foreach ($_productCollection as $pro):
$product = Mage::getModel('catalog/product')->load($pro->getId());
$product->setMetaTitle('Product Title')
->setMetaKeyword('Product keywords')
->setMetaDescription('Product Description')
->save();
endforeach;

Try this,
foreach ($_productCollection as $pro):
$product = Mage::getModel('catalog/product')->load($pro->getId());
$cats = $product->getCategoryIds();
$meta_keys = '';
foreach ($cats as $category_id) {
$_cat = Mage::getModel('catalog/category')->load($category_id) ;
if($_cat->getMetaKeywords())
$meta_keys .= $_cat->getMetaKeywords().', ';
}
$product->setMetaTitle($pro->getName())
->setMetaKeyword($meta_keys)
->setMetaDescription($pro->getShortDescription())
->save();
endforeach;

Related

How can i get a list of the simple products with at least one configuration option?

I need to get with magento a list of all of the simple products with at least one confgurable option.
What is the condition inside the loop ?
<?php require_once 'app/Mage.php';
umask(0);
set_time_limit(0); // ignore php timeout
ignore_user_abort(true); // keep on going even if user pulls the plug*
while(ob_get_level())ob_end_clean(); // remove output buffers
ob_implicit_flush(true); // output stuff directly
//error_reporting(E_ALL);
/* not Mage::run(); */
Mage::app('default');
// get product collection (All product)
$storeId = Mage::app()->getStore()->getId();
$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG);
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('visibility', $visibility)
->setStoreId($storeId)
->addStoreFilter($storeId);
foreach ($_productCollection as $pro):
endforeach;
?>
First I wouldn't use : addAttributeToSelect('*') you are going to slow things down.
$products = Mage::getModel('catalog/products')->getCollection()->addAttributeToFilter('type_id', 'simple')->addAttributeToSelect('name');
foreach ($products as $product) {
echo $product->getName();
}
try that.

Magento - get products by category ids

I want to use url http://mydomain.test/categoru-name.html?cat=9,10,40 . I need to get all products from category category-name and sub-categories with category ids (9,10,40).
How can i do this? Maybe there is such an extension.
Add a php page (categoru-name.php) to Magento root with following code. You need to do few modifications to get it to work.
/** START Include Magento **/
define('MAGENTO_ROOT', getcwd());
$compilerConfig = MAGENTO_ROOT . '/includes/config.php';
if (file_exists($compilerConfig)) {
include $compilerConfig;
}else{exit;}
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
if (file_exists($mageFilename)) {
require_once $mageFilename;
}else{exit;}
Mage::app();
/** END Include Magento **/
// This is not a complete code. You need to fill in.
// Add from query paramter
$categoryIds = array("Insert IDs from URL");
$categoryIdList = array();
array_push($categoryIdList,$categoryIds); //Collect child category IDs
foreach($categoryIds as $Catid){
$cat = Mage::getModel('catalog/category')->load($Catid);
$subCats = $cat->getChildren();
$arrSubCats = explode(',',$subCats);
array_push($categoryIdList,$arrSubCats); //Collect child category IDs
// Go another level down
foreach($arrSubCats as $Catid){
$cat = Mage::getModel('catalog/category')->load($Catid);
$subSubCats = $cat->getChildren();
$arrSubSubCats = explode(',',$subSubCats);
array_push($categoryIdList,$arrSubSubCats); //Collect child category IDs
// Go another level down
// Another foreach loop for $arrSubSubCats
}
}
// Now we have all categoryIds in the tree.
// Get all products.
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIdList))

unable to get product price in custom file magento

Hi i am new to magento and i need to get the products from a particular category
for this i have use
<?php
$id1=4;
$category1 = Mage::getModel('catalog/category')->load($id1);
$collection1 = $category1->getProductCollection();
$collection1->addAttributeToSelect('name');
$collection1->addAttributeToSelect('description');
$collection1->addAttributeToSelect('image');
$collection1->addAttributeToSelect('producturl');
$collection1->addAttributeToSelect('prlce');
$products1 = $collection1->getItems();
$_helper1 = $this->helper('catalog/output'); ?>
<?php foreach ($products1 as $product1){ ?>
<?php echo $this->htmlEscape($product1->getPrice()) ?>
<?php } ?>
in this it is showing the name , image and url but when i am trying to echo price it doenot show anyhing.Please suggest me where i am doing mistake
This is how you do it:
<?php
// Test Params
$cat_id = 4;
$store_id = 1;
// Load Category
$category = Mage::getModel('catalog/category')->load($cat_id);
// Load Category Products
$categoryProducts = $category->getProductCollection();
// Iterate Through Product Collection
foreach ($categoryProducts as $categoryProduct)
{
// Load Product At Specified Store Id
$product = Mage::getModel('catalog/product')
->setStoreId($store_id)
->load($categoryProduct->getId());
// Debug
print_r($product->getData());
// Get Price (e.g.)
echo 'Product Price: '. $product->getPrice()
}
?>

Get Random SKU Product Number

I have the following code:
$sku = $id;
$_product=Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); //Get Product by ID (ASIN)
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
It works by specfing the SKU id, I want to be able to get random SKU product number and use it so
$sku = random product number from database
I used the following script to generate a random sku from the collection. You can use it to suit your requirement also.
//Geting a random sku from collection
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$randomSku = $collection->setPage(1, 1)->getFirstItem()->getSKU();
In your case your code should look like.......
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
$_product = $collection->setPage(1, 1)->getFirstItem()->load();
$qtyStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty(); //if in stock
$_prodcats = $_product->getCategoryIds();
Something like that should work fine ;)
$collection = Mage::getResourceModel(‘catalog/product_collection’);
Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection);
$collection->getSelect()->order(‘rand()’);
$collection->addStoreFilter();
$collection->setPage(1, 1);
$_product = $collection->getFirstItem();
Below code I used in project and its working fine for me :
<?php
// get Random prdocut list
$collection = Mage::getResourceModel(‘catalog/product_collection’);
Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection);
$collection->getSelect()->order(‘rand()’);
$collection->addStoreFilter();
$numProducts = $this->getNumProducts() ? $this->getNumProducts() : 2;
$collection->setPage(1, $numProducts);
foreach($collection as $product){
echo $product->getName();
}
?>

Magento - Product Collection with the current user's Wishlist

Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?
You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}

Resources