Can’t Access Custom Attributes - magento

I’ve been looking all over for an answer to this. I’ve been using Magento for about a week now and Im fixing a bunch of templates that we messed up. Anyway I’m at a spot now that should probably be easy but its not working. I need to access and custom attribute… this code works.
standard attribute
$_product->getName()
but this code wont.. custom attribute of “designer”
$_product->getDesigner()
the original code from the developer had this.. however this code didn’t work either lol
$_helper->productAttribute($_product, $_product->getDesigner(), 'designer');
any help would be great thanks guys!

How do you get $_product? Maybe u need to make a load.
$_product = Mage::getModel("catalog/product")->load($_product->getId());
Magento doesn't load all attributes of an model until you make an load.
Try to log all data in product by doing Mage::log($_product->getData()).

Make sure that $_product is an instance of Mage_Catalog_Model_Product, or set it to one with $_product = Mage::getModel("catalog/product")->load($_product->getId())
In addition to the "magic" getter of $_product->getDesigner() you could use getData() instead:
$_product = Mage::getModel("catalog/product")->load($_product->getId());
$_designer = $_product->getData('designer');

Related

Product model not returning description

Which reasons can it have, when a product model is not returning the description attribute?
I tested several approaches:
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getData();
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getData('description');
Mage::getModel('catalog/product')->loadByAttribute('sku', 'P001')->getDescription();
The getData() method returns even the short_description, but not the description.
I think it can't be a code fault, because in my local environment, it's working. Via git, I have the same codebase also on my stage server, where it's not working anymore.
Can somebody have edited some attribute settings to cause that problem? (I couldn't find any differences between short_description and description which could cause that problem in my opinion.
EDIT: On the stage page, descriptions are shown in articles.
To get the product details by loading sku, use the below code
<?php
$product_sku = "P001";
$product = Mage::getModel('catalog/product')->loadBySku($product_sku);
echo $product->getDescription();
?>

Magento - Get attribute options value and quantity

Hello and good day to all the members of this great community. I'm still new in PHP and especially in Magento.
I'm not posting, waiting for answers, and leaving without replying back. This is a learning process. I hope to get a great support from all of you.
I have a product. I did create custom option for the product, that is an attribute named "a_size". The attribute has value of S, M and L. Each of the value has quantity.
In the single product view, I would like to call all the available size. That is the size (S, M, or L) that has quantity more than 0. I just want to show the available size, not how much the size left.
Can anybody guide me? I'm using Magento 1.7.x and as far for this 2 weeks, I did try pretty many of suggested answers from the community thru the search function.
The replies will be much appreciated. Thank you.
There are a few things to try.
Firstly check that when you set up your new attribute in the Magento Admin (Catalog->Attributes->Manage Attribute) that in the Frontend Properties box you have set Visible on Product View Page on Front-end to yes.
To get size values I use this code:
$cabac_sizeAttribute = $_product->getAttributeText("a_size");
but I have other code for getting attribute values that goes like this:
$_product_helper = Mage::helper('catalog/output');
$temp = $_product_helper->productAttribute($_product, $_product->getASize(), 'a_size');
I think it is related to the type of attribute: text, dropdown, multiselect etc so try both and see how you get on. But really the function productAttribute() is just applying formatting. You can read the function in the file app/core/Mage/Catalog/Helper/Output.php
Also, I wonder, if you have set up a configurable product and you are on the product view page then you will be viewing the configurable product. That product won't have an a_size value: you are trying to access the a_size attribute of the simple products that make up the configurable product, yes? Everything I wrote above is (I think) correct but to get the attribute of the simple products that are part of a configured product you should study the code in the function getJsonConfig() of the file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
And in particular to these lines:
//file: file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php
//class: Mage_Catalog_Block_Product_View_Type_Configurable
//function: getJsonConfig()
foreach ($this->getAllowProducts() as $product) {
$productId = $product->getId();
foreach ($this->getAllowAttributes() as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
Being careful about variable naming: $product is local here, I suggest changing it, and about $this - but if you are in a .phtml of the product view for configurables then I think your $this is already Mage_Catalog_Block_Product_View_Type_Configurable
Welcome to Magento coding. You are doing well; it is a long but rewarding path. (hints: local.xml is your vital friend and so is Alan Storm if you haven't come across his content yet.)
[Additionally, (welcome to Magento) I think you are trying to say eg S and L are out of stock and M is in stock but actually the function getAllowProducts() will disallow a product with zero stock and exclude it from the returned object. You will need to use
$allProducts = $this->getProduct()->getTypeInstance(true)
->getUsedProducts(null, $this->getProduct());
(taken from function getAllowProducts() in file app/core/Mage/Catalog/Block/Product/View/Type/Configurable.php)
and then, if needed, check that each product is allowed to be shown eg status=ENABLED, and then check its stock level...
]
Malachy.
If you want to get the values of your drop down attribute use the following code
$_product->getASize();
and initially load the product object

Load multiple products by custom attribute into a collection [magento]

Doing this:
Mage::getModel('catalog/product')->loadByAttribute('ordernumber', 500);
gives me an object of a product, which means everything with the custom attribute (ordernumber) is fine.
Doing this, though:
Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('ordernumber')
->addAttributeToFilter('ordernumber', 500);
returns a collection, but an empty one - without items. And they should be 3. Couple of hours ago it worked. Now it stopped working, and I haven't changed anything concerning the collection or the attribute. I have no clue what the problem might be...
The attribute is set to Yes for the Usied in Product Listing
Your code may not work if you are using it on frontend and the flat catalog is enabled.
To make it work, you have 2 possibilities:
Option 1:
Make the attribute ordernumber to be used in product listing. Edit the attribute in the backend and set the flag Used in product listing to Yes. Reindex is required.
Option 2:
Use the eav collection directly:
Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('ordernumber')
->addAttributeToFilter('ordernumber', 500);
I recommend the first approach.
Please use
$products = Mage::getModel('catalog/product')->loadByAttribute('ordernumber', '500');
ANOTHER WAY
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('ordernumber','500')
Please Make Sure Used in Product Listing option set to yes for Attributes ordernumber.
After that clear cache. Hope this will work.

Magento: how to get the price of a product with catalog rules applied

I'm developing a script (external to Magento, not a module) which aims to output a text list of all available products, their prices and some other attributes. However, catalog price rules don't seem to be applied to product prices. If I use any of the following:
$_product->getPrice()
$_product->getFinalPrice()
I get the normal price (without rules being applied).
If I use:
$_product->getSpecialPrice()
I get null unless the product actually has a special price inserted in the product itself (i.e. if special price is not related with catalog rules).
I also tried
Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice())
as suggested in the answer given by Fabian Blechschmidt, but interestingly it returns the normal price only if the product is affected by any catalog rule, returning null otherwise.
There was a similar question in StackOverflow and Magento Forums some time ago, but the provided answer (which is to insert the code bellow) doesn't work for me (returned prices remain the same).
Mage::app()->loadAreaPart(Mage_Core_Model_App_Area::AREA_FRONTEND,Mage_Core_Model_App_Area::PART_EVENTS);
Does anybody have an idea of how to achieve this?
I'm using Magento 1.6.2.0.
Thanks in advance.
Thanks to you, I found a new site:
http://www.catgento.com/magento-useful-functions-cheatsheet/
And they mentioned:
Mage::getModel('catalogrule/rule')->calcProductPriceRule($product,$product->getPrice())
HTH
As catalog price rules heavily depend on time, store and visiting customer, you need to set those parameters when you want to retrieve the product final price with it's price rules applied.
So, in your case, make sure that provided product is passed with the desired store and customer group id, which can be set as:
Mage::getModel('catalogrule/rule')->calcProductPriceRule($product->setStoreId('STORE_ID')->setCustomerGroupId('CUSTOMER_GROUP_ID'),$product->getPrice())
I discovered the problem. The discounted prices display Ok in the store frontend. The problem was that I was developing a script "external" to Magento (thus not a Magento module), something like:
<?php
set_time_limit(0);
ignore_user_abort();
error_reporting(E_ALL^E_NOTICE);
header("Content-Type: text/plain; charset=utf-8");
require_once "app/Mage.php";
// Get default store code
$default_store = Mage::app()->getStore();
...
For everything to work properly it seems that one must follow the proper Magento bootstrap, and develop everything as a module. My script was so simple that I thought it wouldn't be necessary to code a complete module. In other words, everything in Magento should really be a module.
Concluding, using the module approach, all the methods work as expected:
$_product->getPrice()
$_product->getFinalPrice()
$_product->getSpecialPrice()
Thank you all for your input.
This helped me in this issue: http://www.magentocommerce.com/boards/viewthread/176883/
. Jernej's solution seems plausible, but it does not handle rules that Overwrite other rules by using 'stop processing' and therefore can apply more than one rule.
$original_price = $_product->getPrice();
$store_id = 1; // Use the default store
$discounted_price = Mage::getResourceModel('catalogrule/rule')->getRulePrice(
Mage::app()->getLocale()->storeTimeStamp($store_id),
Mage::app()->getStore($store_id)->getWebsiteId(),
Mage::getSingleton('customer/session')->getCustomerGroupId(),
$_product->getId());
// if the product isn't discounted then default back to the original price
if ($discounted_price===false) {
$discounted_price=$original_price;
}

What's an effective way of getting all the keywords of a product in Magento?

I couldn't find this documented in Magento Wiki and I didn't know exactly what database table or class in the core/ folder it lies in.
Load the object and use getMetaKeyword() on it. Note that categories use getMetaKeywords()
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku))
$_product->getMetaKeyword();
A faster method to load the object since SKU is a static attribute is to use
$_product = Mage::getModel('catalog/product')->load($sku,'sku');
$_product->getMetaKeyword();

Resources