Get product atribute value in magento - php

Hi i have got the maximum elements of products using below code but it doesn't show the size attribute of my product, the size attribute is visible on front-end but i cant understand why it is not printing with this code
<?php
ob_start();
session_start();
ini_set('display_errors', 1);
//for order update
include '../../../../app/Mage.php';
Mage::app('default');
echo '<pre>';
if(isset($_REQUEST['productid'])){
$productId = $_REQUEST['productid'];
}else{
$productId = '12402'; // product ID 10 is an actual product, and used here for a test
}
$product = Mage::getModel('catalog/product')->load($productId); //load the product
//$product_id = $product->getId();
//$created_at = $product->getcreated_at();
//$description = $product->getdescription();
//$short_description = $product->getshort_description();
//$sku = $product->getsku();
//$size_fit = $product->getsize_fit();
//$style_ideas = $product->getstyle_ideas();
//$name = $product->getname();
//$price = $product->getprice();
//$stocklevel = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();

If its the size_fit attribute (i'm guessing that because its the only size attempt in your code..) use $product->getSizeFit(). For just size use $product->getSize(). When this is not returning anything, please post the attribute installer if you have one. Mufadall his answer is also correct but judging your code you are just using wrong syntax.
Basicly according to the magic get method the first letter is turned into a capital and all other letters after an underscore.
Ex.: To fetch my_sample_attribute use getMySampleAttribute().
getData('my_sample_attribute') would also be an option but you shouldn't make a habbit of doing that because in some cases, for some attributes getData('attribute') returns a different value then getAttribute()....

$product->getData($attribute_code);
will return you the actual attribute value. For attribute with type dropdown it will return option id
$product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
will return actual value

You can use this:
$product->getData('Your Attribute ID');

Go to Size attribute and check for Drop Down used in product listing if it set to No then set it to Yes, after that you can get your size attribute with other product attribute

You can use the getters, or getData();
The getter is set in magento with the magic __get() method, and you can use it in the following way:
$product->getDescription() // ( to get description attribute)
$product->getShortDescription() // (to get short_description attribute)
So basically you explode the attribute with underscores, and capitalize the words, and you will get what you need to put after "get".
Here is something very useful I use all the time
Zend_Debug::dump($product->getData());
This gets you all the data you have to work with. If you are missing data, it means it's not loaded.
Good luck!

Related

Magento duplicate attriubte set new attributes not showing in front end

Duplicated attriubte set for new product line.
Attributes set show in backend and front end.
Created new attribute and added to attribute set.
New attribute show's in back end but not front end.
If I add the new attribute to the origonal set that was duplicated the new attribute will now show on the front end for the product assoiated with the duplicated attribute group.
So basicly A new attribute added to a duplicate set won't show on the front end until its added to the attribute set that was duplicated.
I've checked to make sure the attribute is visable on front end etc and tried it several times checking the settings.
The goal is to be able to duplicate a attribute set and add new attributes for different product types. Then call the folder by id and display the assoiated attributes.
I've called the attribute group by ID (spec's). This code is working.
<?php
require_once 'app/Mage.php';
Mage::app();
$attributeGroupCollection = Mage::getResourceModel('eav/entity_attribute_group_collection');
$product = $this->getProduct();
foreach ($attributeGroupCollection as $attributeGroup) {
$attributeGroup->getAttributeGroupId();
$attributeSpecs = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter(41);
}
?>
Help is appreciated, thanks
Duplicating an attribute set is duplicating at a time (the moment when you click the button). If you want to add an attribute in both attribute sets, you need to create it in both, for that, you just need to create your attribute and drop it in the section you want to have it linked to (what you call "folder"). If you want to do it programmatically. You need to create an observer that will be fired after the attribute set is saved and that will add the attribute to the duplicated attribute set. I would not advise to do so because it means that you need to enter the ID of the attribute set duplicated and it can be tricky thing when it comes to pushing the development to the production. I am sure there is a workaround for what you want to achieve.
The problem was is magento creates a new id for each new group folder in the new duplicated attribute set (makes sense). I was calling by ID for the group and as a result will only show attributes in the origional folder (weird) even if the product was assoicated with the new attribute set. What I did was get the current attribute set id and then sort out the attribute groups by name so even if the attribute sets are coppied as long as the folde has the name it will display in the custom loop displaying the attributes. Here is my working code:
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
$attributeSetID = $attributeSetModel->getAttributeSetID();
$groups = Mage::getModel('eav/entity_attribute_group')
->getResourceCollection()
->setAttributeSetFilter($attributeSetID)
->setSortOrder()
->load();
$attributeCodes = array();
foreach ($groups as $group) {
echo $groupName = $group->getAttributeGroupName();
$groupId = $group->getAttributeGroupId();
if (strpos($groupName , 'Specifications') !== false) {
echo 'true';
$specificationsNum = $groupId;
};
if (strpos($groupName , 'eatures') !== false) {
echo 'true';
$prodfeaturesNum = $groupId;
};
}
//echo $specifications;
$specifications = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($specificationsNum);
$prodfeatures = Mage::getResourceModel('eav/entity_attribute_collection')
->setAttributeGroupFilter($prodfeatures);

Magento - use attribute value as static block identifier

Hi i am trying to use attribute value as a static block identifier.
i have an attribute called designer. In my tabs on product page i want to display a static block depending on the value of this attribute
echo $this->getLayout()->createBlock('cms/block')->setBlockId('**designer**')->toHtml();
so i would like designer to be substituted with the value for the specific product, so if i have a product where the attribute value is designer-1 i would like it to be
echo $this->getLayout()->createBlock('cms/block')->setBlockId('designer-1')->toHtml();
and so forth
If you are outputting this in your template (while viewing product page ): Try below:
$product = Mage::registry('current_product');
$attributeText = $product->getAttributeText('designer');
//For uniformity of block identifiers, make it all lower case and make sure you are using lower case as block name
$lowerCaseValueForBlockId = strtolower($attributeText);
$cmsBlock = Mage::getModel('cms/block')->load($lowerCaseValueForBlockId);
if ($cmsBlock->getId()) {
echo $this->getLayout()->createBlock('cms/block')->setBlockId($lowerCaseValueForBlockId)->toHtml();
//Or you can use below code
echo $cmsBlock->getContent();
}

Magento set the new attribute value

I have created a new IMEI attribute of type textarea for all products, see from the image. Can anyone point out a function to update the its value. I have the code like the following.
$this belogns to Mage_Sales_Model_Order.
foreach ($this->getAllItems() as $item) {
$item->setImei('123');
$item->save();
echo $item->getImei();
}
I am getting 123 from the last statement but when I am viewing from admin. Its not changing there. Also in which table the attribute and value will be stored, So I can debug from there.
What class is $this->getAllItems() is it Mage_Catalog_Model_Product?
If it not Mage_Catalog_Model_Product then load the product by id and save the product
foreach ($this->getAllItems() as $item) {
$product = Mage::getModel('catalog/product')->load($item->getId() or $item->getProductId())
$product->setImei($product->getImei() . '123');
$product->save();
}
The values of catalog product attributes of type text are stored in the table catalog_product_entity_text. An SQL would be
select * from catalog_product_entity_text where attribute_id = {insert your attribute id} and entity_id = {insert your product id}
The query will return results for every store view in the system.
The reason why you do not see a change to the attribute in the backend is probably because the new value is set for a different website/store than the one loaded in the backend.
You are already using a correct way to set the attribute value assuming $item is of type Mage_Catalog_Model_Product:
$item->setImei('123');
$item->save();
heres how you do it differently: (color = attribute name, red = attribute value id)
starts assuming you already have $product available.
$attr = $product->getResource()->getAttribute('color');
if ($attr->usesSource()) {
$avid = $attr->getSource()->getOptionId('red');
$product->setData('color', $avid);
$product->save();
}

Add attributeSet but based on an existing set - Magento

Ok, its possible to add a new attribute set in magento using something like the following:
$entitySetup = new Mage_Eav_Model_Entity_Setup;
$entitySetup->addAttributeSet('catalog_product', $setName);
But how can i base the set on an existing set like the default. This option is available in the admin section so its possible.
I did this 6 months ago, I do not have the code anymore, but I know you have to use the initFromSkeleton() method on your attribute set. You can search Magento's code for calls to this function, there are very few calls (just one perhaps). It will show you its usage.
EDIT: I remember I had the very same problem you are talking about, and I mailed about it. Here is the usage I was advised:
$attrSet = Mage::getModel('eav/entity_attribute_set');
$attrSet->setAttributeSetName('MyAttributeSet');
$attrSet->setEntityTypeId(4);//You can look into the db what '4' corresponds to, I think it is for products.
$attrSet->initFromSkeleton($attrSetId);
$attrSet->save();
The initialization is done before the save.
// create attribute set
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product')
->getTypeId(); // 4 - Default
$newSet = Mage::getModel('eav/entity_attribute_set');
$newSet->setEntityTypeId($entityTypeId);
$newSet->setAttributeSetName(self::ATTRIBUTE_SET_NAME);
$newSet->save();
$newSet->initFromSkeleton($entityTypeId);
$newSet->save();
This is what worked for me.
$i_duplicate_attribut_set_id = 10; // ID of Attribut-Set you want to duplicate
$object = new Mage_Catalog_Model_Product_Attribute_Set_Api();
$object->create('YOUR_ATTRIBUT_SET_NAME', $i_duplicate_attribut_set_id);
Alex
Here you can find useful information about working with attribute sets.
Here:
$entityTypeId = Mage::getModel('eav/entity')
->setType('catalog_product') // This can be any eav_entity_type code
->getTypeId();
$attrSet = Mage::getModel('eav/entity_attribute_set');
$attrSetCollection = $attrSet->getCollection();
$attrSetCollection
->addFieldToFilter('entity_type_id', array('eq' => $entityTypeId))
->addFieldToFilter('attribute_set_name', array('eq' => 'Default')); // This can be any attribute set you might want to clone
$defaultAttrSet = $attrSetCollection->getFirstItem();
$defaultAttrSetId = $defaultAttrSet->getAttributeSetId();
$attrSet->setAttributeSetName('Assinaturas'); // This is the new attribute set name
$attrSet->setEntityTypeId($entityTypeId);
$attrSet->initFromSkeleton($defaultAttrSetId);
$attrSet->save();

How to find out master product of simple product?

How can I find out, if a simple product is part of a configurable product and then get the master product? I need this for the product listing.
Just found out:
$_product->loadParentProductIds();
$parentIds = $_product->getParentProductIds();
Let's say that you have your simple product's Product ID.
To get all the parent configurable product IDs of this simple product, use the following code:-
<?php
$_product = Mage::getModel('catalog/product')->load(YOUR_SIMPLE_PRODUCT_ID);
$parentIdArray = $_product->loadParentProductIds()
->getData('parent_product_ids');
if(!empty($parentIdArray)) {
// currently in the master configurable product
print_r($parentIdArray); // this prints all the parent product IDs using your simple product.
}
?>
I suppose this should get you going.
For Magento 1.4.2 and above use the following method instead:
$configurable_product_model = Mage::getModel(‘catalog/product_type_configurable’);
$parentIdArray = $configurable_product_model->getParentIdsByChild($simple_product_id);
After version 1.4.2.0 the loadParentProductIds() and getParentProductIds() methods are deprecated. Don't ask me why. Personally I kind of liked those methods. So I've reintroduced them to my local Mage classes. This is how:
Copy
app/code/core/Mage/Catalog/Model/Product.php
to
app/code/local/Mage/Catalog/Model/Product.php
and change the loadParentProductIds() method, found around line 1349 to:
public function loadParentProductIds()
{
return $this->_getResource()->getParentProductIds($this);
}
This piece of code will query its resource for its parent product ids. For this to work we'll need to rewrite the getParentProductIds() method in the resource class.
So copy:
app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php
to
app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Product.php
Find the deprecated getParentProductIds() method. Should be somewhere around line 535. Overwrite it with the pre 1.4.2.0 code:
public function getParentProductIds($object){
$childId = $object->getId();
$groupedProductsTable = $this->getTable('catalog/product_link');
$groupedLinkTypeId = Mage_Catalog_Model_Product_Link::LINK_TYPE_GROUPED;
$configurableProductsTable = $this->getTable('catalog/product_super_link');
$groupedSelect = $this->_getReadAdapter()->select()
->from(array('g'=>$groupedProductsTable), 'g.product_id')
->where("g.linked_product_id = ?", $childId)
->where("link_type_id = ?", $groupedLinkTypeId);
$groupedIds = $this->_getReadAdapter()->fetchCol($groupedSelect);
$configurableSelect = $this->_getReadAdapter()->select()
->from(array('c'=>$configurableProductsTable), 'c.parent_id')
->where("c.product_id = ?", $childId);
$configurableIds = $this->_getReadAdapter()->fetchCol($configurableSelect);
return array_merge($groupedIds, $configurableIds);
}
Now you once again can do this:
$_product->loadParentProductIds()->getData('parent_product_ids');
Hope this helps you out!

Categories