Product model not returning description - magento

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();
?>

Related

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

magento getProductUrl does not include category path

does getProductUrl return product url with the category path, how can i get it to work like that, it seems others will suggest to use a helper, but i guess it is not the only way.
I haven't checked it but I am quite sure the product needs to know about the category.
$product->setCategoryId($categoryId)->getProductUrl();
The proposed answer did not work on my Magento version (1.9).
Here is what worked for me:
$category = Mage::getModel('catalog/category')->load($categorieId);
Mage::unregister('current_category');
Mage::register('current_category', $category);
$product->getProductUrl();

Can’t Access Custom Attributes

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');

Magento product load - difference between loadByAttribute and load methods

today I'm fighting with Magento again :) and I found a difference between
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);
and
$product = Mage::getModel('catalog/product')->load($product_id);
Can anyone exaplain me a difference between these two approaches? I found that when I'm loading a product by sku then when I 'try to re-save it with changed data then I get error exception 'Varien_Exception' with message 'Invalid method Varien_Object::save in app\code\core\Mage\CatalogInventory\Model\Observer.php(153): Varien_Object->__call('save', Array) that's true because once you try to load by sku then another observer sets product's stock item as Varien_Object, that looks like pitfall or I just dont understand it enough, but
I do daily Magento development from its beginnig so I know a lot about system and this is new for me. Thanks in advance, Jaro.
Interesting. While both methods will net you a single product model instance with fully loaded EAV data (provided the third parameter of loadByAttribute() is not passed or is *), the observers which add stock-related data are different for products vs. product collections, yet both stock information objects are added to the product data key "stock_item". It's debatable, but this feels like a bug. I would think that Mage_CatalogInventory_Model_Observer::saveInventoryData() or Mage_CatalogInventory_Model_Observer::_prepareItemForSave() would handle this.
You could resolve this issue by setting the product stock item fully on your product instance using the stock_item object.
loadByAttribute is a serious misnomer in my opinion because it doesn't actually trigger a load(); rather it uses getResourceCollection():
public function loadByAttribute($attribute, $value, $additionalAttributes = '*')
{
$collection = $this->getResourceCollection()
->addAttributeToSelect($additionalAttributes)
->addAttributeToFilter($attribute, $value)
->setPage(1,1);
Because it doesn't trigger the observer events associated with load() it means the resulting product object doesn't include the full set of product data you might want. In my case I needed the "description" attribute and it wasn't included.
There are several ways to resolve this:
Use a different method to load by SKU:
$product = Mage::getModel("catalog/product");
$product->load($product->getIdBySku("whatever"));
Force the desired attribute data to be included in the default product resource data by visiting Magento Admin > Catalog > Attributes > Edit attribute > "Used in Product Listing" = "Yes" and then reindexing. You should then be able to use the attribute data (in the frontend, at least) using loadByAttribute().
See also https://magento.stackexchange.com/a/197286/18855

Magento - Load product by url

How would one load a product model in Magento, if the product id is not available and only the product's url is? For example, I want to retrieve the product model from it's friendly url, such as
electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html
I found the following code in another post:
$oRewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath(
$path
);
but it doesn't seem to work correctly. The Magento documentation is very lacking in this area; does anyone know how to accomplish this?
Here's an alternative solution.
First use the URL rewrite model to find the route which matches your product:
$vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
$oRewrite = Mage::getModel('core/url_rewrite')
->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath($vPath);
Then you can call getProductId() on the route to locate the produc's id:
$iProductId = $oRewrite->getProductId();
Finally if you require the product model object itself it's then a simple matter to call:
$oProduct = Mage::getModel('catalog/product')->load($iProductId);
The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.
The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.
I'm curious as to why you need to do this, since this might not be the best solution. This should be easy enough using the addAttributeToFilter() method on the collection:
$path = 'folder/folder/my-product-name';
// Get the product permalink
$productName = explode('/', $path);
$productName = end($productName);
// Filter the url_path with product permalink
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('url_path', $productName)
->getFirstItem();
Zend_Debug::dump($products->getData());exit;

Resources