Get attributes by attribute group Magento - magento

I have product:
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
I have name (not ID) of attribute group.
I need to list all attributes names and values in this attribute group (does not matter on attribute set).
How can i get attributes and values from attribute group, if i know only product and a attribute group name?

$setId = $_product->getAttributeSetId(); // Attribute set Id
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($setId)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
if($group->getAttributeGroupName() == 'Somename'){ // set name
//$groupName = $group->getAttributeGroupName();
//$groupId = $group->getAttributeGroupId();
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
->setAttributeGroupFilter($group->getId())
->addVisibleFilter()
->checkConfigurableProducts()
->load();
if ($attributes->getSize() > 0) {
foreach ($attributes->getItems() as $attribute) {
/* #var $child Mage_Eav_Model_Entity_Attribute */
$attributeCodes[] = $attribute->getAttributeCode();
}
}
}
}
print_r($attributeCodes);

Related

Magento Show custom attribute dropdown option for a simple product in cart page

how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options

How can i update my products?

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;

Magento - Accessing a customer's wishlist

I would like to be able to load a customer's wishlist and return the product id's of the products within the list
I am using:
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
The problem is that the arrays in the Item Collection are protected and I can't find any methods to extract the data.
Is there another way to do this?
You're very close to your target.
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
if (count($wishListItemCollection)) {
$arrProductIds = array();
foreach ($wishListItemCollection as $item) {
/* #var $product Mage_Catalog_Model_Product */
$product = $item->getProduct();
$arrProductIds[] = $product->getId();
}
}
The array variable $arrProductIds will now contain the list of all Product IDs that have been wishlisted by that specific Customer.
Your code is correct. There may be customer is not loaded. here is code.
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}
In any template, using magento 1.8, this works
Total: <?php echo $this->helper('wishlist')->getItemCount() ?>
// Items
$this->helper('wishlist')->getWishlist()->getItemCollection();
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
foreach ($wishListItemCollection as $item)
{
//do your thing e.g. echo $item->getName();
}
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; } } ?>

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
}

How to get all active attributes of products in Magento?

I am working in a new "Filter by Product" module in Magento, i have a situation where i should retrieve all attributes and their values. I Googled this and found the below code
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->load();
// ->addFieldToFilter('attribute_code', 'color')
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$attr = $attribute->getSource()->getAllOptions(true);
foreach ($attr as $att) {
echo " Label : ".$att['label']." Value : ".$att['value']."";
}
but this retrieves only the label and value of last attribute from list of all available attributes.
how to i get all the attributes? what am i doing wrong in this code?
Thanks,
Balan
Try this:
$attributes = Mage::getSingleton('eav/config')
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection();
// Localize attribute label (if you need it)
$attributes->addStoreLabel(Mage::app()->getStore()->getId());
// Loop over all attributes
foreach ($attributes as $attr) {
/* #var $attr Mage_Eav_Model_Entity_Attribute */
// get the store label value
$label = $attr->getStoreLabel() ? $attr->getStoreLabel() : $attr->getFrontendLabel();
echo "Attribute: {$label}\n";
// If it is an attribute with predefined values
if ($attr->usesSource()) {
// Get all option values ans labels
$options = $attr->getSource()->getAllOptions();
// Output all option labels and values
foreach ($options as $option)
{
echo " {$option['label']} (Value {$option['value']})\n";
}
}
else
{
// Just for clarification of the debug code
echo " No select or multiselect attribute\n";
}
}
This is the first solution:
$products = Mage::getModel('catalog/product')->getCollection();
foreach($this->products as $product) {
$prod = Mage::getModel('catalog/product')->load($product->getId());
}
If you are using using the data from outside magento, you can use a class I made:
http://blog.alexparadise.com/magento-dbeav-class/
It's beta but that should work in your case.
Or get the concept of the EAV table... and make your own SQL query.

Resources