开发者

use case for getProductAdditionalInformationBlock in magento

开发者 https://www.devze.com 2023-04-08 10:27 出处:网络
I\'d like to put some additional info under each item in the cart. I have this info already saved in table \"sales_flat_quote_item\" in \"additional_info\" field. So the question is only how to show i

I'd like to put some additional info under each item in the cart. I have this info already saved in table "sales_flat_quote_item" in "additional_info" field. So the question is only how to show it globally at all places where the items are shown.

I saw in several places under item name there is a structure like this:

<?php if ($addtInfoBlock = $this->getProductAdditionalInformationBlock()):?>
 <?php echo $addtInfoBlock->setItem($_item)->toHtml() ?>
<?php endif;?>

For example in this files: /app/design/frontend/base/default/template/checkout/cart/item/default.phtml /app/design/frontend/base/default/template/checkout/onepage/review/item.phtml

So I suppose this is the place I should use for such task.

What I figured out is that: I have to add my own block definition to for example:

<checkout_cart_index>
  <block type="core/text_list" name="additional.product.info" translate="label">
  <label>Additional Product Info</label>
  <block type="various/itemrendererdefault" name="glass.additional" as="glass"    template="checkout/cart/glass_additional.phtml"/>
  </block>
</checkout_cart_index>

This is no problem so far. My class is loaded

class Site1_Various_Block_Itemrendererdefault extends Mage_Core_Block_Template {

public开发者_运维百科 function setItem(Varien_Object $item) {
    $this->setData('item', $item);
    return $this;
}

public function getItem() {
    return $this->_getData('item');
}

}

and the template checkout/cart/glass_additional.phtml is called.

But inside the template I have no idea how to get the info about what $item should I process. I tried:

$_item = $this->getItem();
print_r($_item);
$_item = $this->getData();
print_r($_item);

but it returns nothing.

So my question is: How to get $item data inside my template. Can I access the data set in?

...
$addtInfoBlock->setItem($_item)->toHtml();
...


Krystian, the OP, already self-answered his question.

Quote:

I just solved the issue by setting my block as "additional.product.info"

<checkout_cart_index>
    <block type="various/itemrendererdefault" name="additional.product.info" translate="label" template="checkout/cart/glass_additional.phtml"></block>
</checkout_cart_index>

Note: It's absolutely OK to self-answer your own question. Please just post it as an real answer, but not in a question or comment. Posting as real answer helps to keep the "Unanswered" list more clear (avoids making other people wasting their time).


I think to get the item instance you have to try this:

class Site1_Various_Block_Itemrendererdefault extends Mage_Core_Block_Template {

public function setItem(Varien_Object $item) {
    $this->setData('item', $item);
    return $this;
}

public function getItem() {
    $parent = $this->getParentBlock();
    if ($parent) {
        $item = $parent->getItem();
    }       
}

}

Thanks !


I was having issues with this as well until I used a block type of core/template and a call to getParentBlock() in my custom template file. This might work for any custom block type but I didn't test.

In your layout/local.xml file:

<checkout_cart_index>
    <reference name="additional.product.info">
        <block type="core/template" name="additional.product.info.your_template" as="your_template" template="checkout/cart/item/your-template.phtml"/>
    </reference>
</checkout_cart_index>

The $addtInfoBlock->setItem($_item)is called on the additional.product.info block, which would be the parent of any blocks you add underneath it. Because of this, you can call $this->getParentBlock() in your template to get access to it's data.

So now, in your checkout/cart/item/your-template.phtml:

$_item = $this->getParentBlock()->getItem();
/* get access to all product attributes, with a performance hit. */
$_product = $_item->getProduct()->load();

/* Some product attributes */
echo $_product->getName();
echo $_product->getSku();
0

精彩评论

暂无评论...
验证码 换一张
取 消