magento getProductUrl does not include category path - magento

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

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

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.

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: 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;
}

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