5

Short of creating my own widget or block class to do so, is there a way to display a product's name, sku, price, or other property in a static block or CMS page?

If there isn't a way to do this out of the box, is there a well excepted way to achieve this?

That is, I'd like to be able to do something like this in a block or page

{{block type="some/block" sku="PRODUCT_SKU" variable_to_display="name"}}

and have the product's name appear in the static-block/page.

Does this exist in Magento's core? Somewhere in the community?

1 Answer 1

3

I'm not fully aware of a core means to go about this (I've not looked), but the following isn't particularly difficult to implement.

Just a few days ago, we needed the output from a helper - passed back into another helper, rendered via a CMS block.

This won't require phtml files to be created just for the sake of a single function - and will allow a fairly flexible use of helper or model methods to be executed.

Whilst probably not the clean core solution you want, the output should be what you are after.

Calling Helper Methods

There is no fault tolerance in this (ie. checking a method/class even exists etc.); but if you really need it, you could modify it to suit.

What I wanted was to fetch the return value of a helper that got the minimum price of a bundle product; then push that output through the currency helper to format it appropriately.

{{block type="sonassi.core/helper" helper="sonassi.core/catalog" function="getMinMaxPrice" arg1="5" arg2="" arg3="min" callback_helper="core" callback_function="currency" callback_arg1="function_response" callback_arg2="true" callback_arg3="false"}

Which was rendered using the following class.

<?php
/*
* @category    Module
* @package     Sonassi_Core
* @copyright   Copyright (c) 2012 Sonassi
*/
class Sonassi_Core_Block_Helper extends Mage_Core_Block_Abstract
{
  protected function _toHtml()
  {
    try {
      $args = $this->getData();
      $_helper = Mage::helper($args['helper']);
      $html = false;
      $argStr = implode("###", array_keys($args));
      if (preg_match_all('/arg[0-9]+/', $argStr, $matches)) {
        $data = array_intersect_key($args,array_flip($matches[0]));
        array_walk($data, array(Mage::helper('sonassi.core'),'stringToBoolean'));
        $html = call_user_func_array(array($_helper, $args['function']), $data);
      }
      if (isset($args['callback_helper'])) {
        $_callbackHelper = Mage::helper($args['callback_helper']);
        if (preg_match_all('/callback\_arg[0-9]+/', $argStr, $matches)) {
          $data = array_intersect_key($args,array_flip($matches[0]));
          $testCase = array_flip($data);
          if (isset($testCase['function_response'])) {
            $position = $testCase['function_response'];
            $data[$position] = $html;
          }
          array_walk($data, array(Mage::helper('sonassi.core'),'stringToBoolean'));
          $html = call_user_func_array(array($_callbackHelper, $args['callback_function']), $data);
        }
      }
      return $html;
    } catch(Exception $e) {}
    return;
  }
}

Calling Model Methods

So tweaking the above code ever-so-slightly (I've stripped out the helper callback just to shorten the amount of code).

You could execute,

{{block type="sonassi.core/model" model="catalog/product" load_attribute="sku" load_value="product-sku" function="getName"}}

With the following supporting class,

<?php
/*
* @category    Module
* @package     Sonassi_Core
* @copyright   Copyright (c) 2012 Sonassi
*/
class Sonassi_Core_Block_Model extends Mage_Core_Block_Abstract
{
  protected function _toHtml()
  {
    try {
      $args = $this->getData();
      $_model = Mage::getModel($args['model'])
      if (isset($args['load_id']))
        $_model->load($args['load_id']);
      elseif (isset($args['load_attribute']) && isset($args['load_value']))
        $_model->loadByAttribute($args['load_attribute'], $args['load_value']);
      $html = false;
      $argStr = implode("###", array_keys($args));
      if (preg_match_all('/arg[0-9]+/', $argStr, $matches)) {
        $data = array_intersect_key($args,array_flip($matches[0]));
        array_walk($data, array(Mage::getModel('sonassi.core'),'stringToBoolean'));
        $html = call_user_func_array(array($_model, $args['function']), $data);
      }
      return $html;
    } catch(Exception $e) {}
    return;
  }
}

This class was kept fairly generic and loading a whole method for one method would be heavy - but you can just tailor it to your needs.

Obviously something like this would be more lightweight, but you can tweak your methods to suit your needs.

Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

Common Method

The referenced helper method above, stringToBoolean, was just the following:

public function stringToBoolean(&$value, $key)
{
  if ($value == "true")
    $value = true;
  elseif ($value == "false")
    $value = false;
}
1
  • +1 for Yup, had something similar in mind — but I'd rather not roll my own if there's a solution out there that's already been stress tested under real world use. Commented Feb 24, 2013 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.