Reputation: 343
I'm running Magento CE 1.9 and I have a piece of code that displays the sku on the product page: template > catalog > product > view.phtml
the php code is: < ?php echo $this->getProduct()->getSku() ?>
which works perfectly, but if I try to add the sku somewhere else in the page... such as for a piece of code for PowerReviews where it needs the SKU to be dynamically inserted for tracking... if I add the same php code to get the sku, it breaks the page right there. I am not sure why I can display the sku in the view.phtml, but not in a product page tab for example...
in this example, I need it in a custom tab I made... so it would be: frontend/rwd/default/template/so/reviews.phtml
Upvotes: 0
Views: 2242
Reputation: 398
I think your issue is understanding how Magento blocks are defined. Templates, like app/design/frontend/[DESIGN PACKAGE]/[THEME]/template/catalog/product/view.phtml
, have a corresponding PHP block definition. This specific template, for example, has its block defined in app/code/core/Mage/Catalog/Block/Product/View.php
--and you should even see that this is commented at the top of the core template file for the product view block:
/**
* Product view template
*
* @see Mage_Catalog_Block_Product_View
* @see Mage_Review_Block_Product_View
*/
Inside the block definition, you should find a method called getProduct
which is why you can access that method from the view.phtml
block. The $this
variable refers to an instance of the block definition object, so an instance of Mage_Catalog_Block_Product_View
, in other words. If the other block definitions (including those from third-party libraries) where you wish to access the product do not define a method like getProduct
(or inherit from a block definition that does), then you will not be able to call that method and return a product. What you can do, is define a similar method on the block definition for whatever block you want to use to access product data. As an example, you could copy the getProduct
method definition from the core product view block definition into the block definition for the block from your PowerReviews extension.
This resource should give you a decent overview and hopefully a better understanding of the concept:
UPDATE: Here's a bit more direction and some elaboration on my reply in the comments. One way to tackle this is to use an existing block definition for your block in the layout definition, like so:
You could choose to use an existing block definition for your template file, or you could create your own (and possibly inherit that class)
<block type="catalog/product_view" name="my.product.block" template="path/to/custom/template.pthml" />
Notice I'm using the Magento core catalog/product_view
block type which corresponds to the PHP file I mentioned previously that defines the block and its getProduct
method.
Or if you need some kind of custom functionality in your block but you want to rely on the base functionality of an existing block, define your own block and extend an existing one that has the desired functionality:
class MyPackage_MyExtension_Block_Product_View extends Mage_Catalog_Block_Product_View {
// add your custom methods
}
Here I'm extending the existing Magento core block definition that has the method you want. Then use your custom block definition in the layout definition:
<block type="mypackage_myextension/product_view" name="my.product.block" template="path/to/custom/template.pthml" />
Hopefully that helps clear it up a bit more.
Upvotes: 0