douwen4401 2014-12-19 21:14
浏览 23
已采纳

Magento产品页面变量

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

  • 写回答

1条回答 默认 最新

  • dsfhe34889 2014-12-20 19:59
    关注

    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:

    http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-4-magento-layouts-blocks-and-templates

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler