PROBLEM:
I'm trying to build an AJAX cart in magento, but I can't seem to fetch the chosen configuration option(s) of configurable products. I believe am invoking the the right method ($this->getOptionList()) to get them. It should return an array with options and labels, but it returns nothing!! To be clear, they are showing up in the normal cart.
EXPLANATION:
The short story of how I'm doing it:
I use the Cart Controller of the Mage_Checkout module, but I change the template to something very minimal (using layout updates in the current theme), and a custom module with a router defined in it.
Long story:
I have created a module MyNameSpace_Checkout in which I have defined a router that binds the frontName 'ajaxcart' to the controllers in the Mage_Checkout module.
Now in the local.xml file of my current theme I put the following layout updates under the 'ajaxcart_cart_index' handle:
<ajaxcart_cart_index>
<reference name="root">
<action method="setTemplate">
<template>ajaxcart/cart/index.phtml</template>
</action>
<block type="checkout/cart" name="checkout.cart" template="ajaxcart/cart/show.phtml" as="cart"></block>
</reference>
</ajaxcart_cart_index>
My template ('ajaxcart/cart/show.phtml') is being used, so this worked pretty well. I tested it when I went to http://domain.com/ajaxcart/cart
index.phtml:
<?php echo $this->getChildHtml('cart'); ?>
show.phtml:
<?php foreach($this->getItems() as $_item): ?>
<?php $_renderer = $this->getItemRenderer($_item->getProductType())->setItem($_item); ?>
<?php /* render an item */ ?>
<?php endforeach; ?>
Seeing as $this in this context refers to the Cart Block of the Mage_Checkout module, and digging around in the method getItemHtml() of this class (its superclass actually) I found that the block object per item in the cart is retrieved using the second line in the show.phtml sample above ($_renderer).
Does anyone know why information is missing? The whole reason I'm using the original controller is that it is probably doing some essential stuff, but it's still not working!!
Thanks in advance.
I found the problem myself, the layout updates (local.xml of the current theme) were the problem:
<action method="addItemRender">
<type>configurable</type>
<block>checkout/cart_item_renderer_configurable</block>
<template>checkout/cart/item/default.phtml</template>
</action>
I took another look at checkout.xml of the base/default theme and it sported some addItemRender (layout xml) methods in the cart/checkout block under the checkout_cart_index handle. Specifically for my problem, the above element was missing and that messed up the rendering of a configurable product item in the cart. A special type of block object needs to be loaded that actually has the (PHP) method getOptionList().
Related
I have created a custom module that attaches some information to each product. Now, I want to show the custom information on Catalog page under product name.
Here is snapshot to show you what I am trying to do.
Here is my layout xml
<?xml version="1.0"?>
<!--
/**
* Custom module
*/
-->
<layout>
<catalog_category_default translate="label">
<reference name="name.after">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml"/>
</reference>
</catalog_category_default>
</layout>
But this is not working. Is it possible to render custom content under product name without modifying any .phtml files?
Many Thanks!
I'm aware that this is a quick and dirty hack that more diligent magento devs will sneer at, but it will give you the result you want and is easy for you to impliment
The 'correct' solution probably involves writing a custom block and calling it in the foreach loop of your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml - however, I can offer you a quick and dirty hackaround that shouldn't tax you too much. You can do it very easily by abusing the product attributes system.
First create a new product attribute in the attribute set used by your product.
We'll use this to store the custom content we want to inject between your product name and price. For the sake of this example we'll call it customcontent. Make sure you set the attribute's Visible on "Product View Page on Front-end" value to 'No'.
Edit your product and set this new attribute equal to your desired custom content - it's fine you use html tags in here too. By default, product attributes have (for some reason) an arbitrary limit of 30 characters but if you need it to hold a longer string you can easily change this by editing app/code/mage/eav/model/entity/Attribute.php and changing the value of ATTRIBUTE_CODE_MAX_LENGTH.
Then in your app/design/frontend/YourPackage/YourTheme/template/product/list.phtml and find the following line of code somewhere around line 49
<?php foreach ($_productCollection as $_product): ?>
directly after it insert the following:
<?php $_customcontent =$_product->getResource()->getAttribute('customcontent')->getFrontend()->getValue($_product);?>
You've now stored the string from your products customcontent attribute in a variable called $_customcontent
So if you find the following line in your list.phtml
<h2 class="product-name"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></h2>
you can easily add a new paragraph or heading beneath it and populate it with your custom content using
<?php echo $_customcontent ?>
I know this probably represents a deeply unorthodox solution, but it does work and can be acheived very quickly, and without requiring you to know anything about writing your own custom blocks.
Can you try :
<catalog_categoryd_default>
<reference name="content">
<block type="catalog/product_list" name="custom_content" template="custommodule/default.phtml" after="nameofTheNameBlock"/>
</reference>
</catalog_categoryd_default>
I'm developing a Magento extension and having trouble trying to get it to run javascript when an item is added to cart.
In config.xml I have an observer
<checkout_cart_product_add_after>
In Observer.php
public function itemAddedToCart(Varien_Event_Observer $observer){}
This is firing (I can test by echo-ing). But how do I get it to inject a block of javascript (preferably into the footer)?
Thanks in advance.
UPDATE:
Used Chris' solution, but instead of registry I used the session to avoid potential multi-user issues:
In Observer.php
public function itemAddedToCart(Varien_Event_Observer $observer){}
$itemAddedToCart = 'true';
Mage::getSingleton('core/session')->setItemAddedToCart($itemAddedToCart);
In my custom block (script.phtml):
<?php if($itemAddedToCart) : ?>
<script type="text/javascript">
alert(<?php echo '"' . $this->__($itemAddedToCart) . '"' ?>);
</script>
<?php
// Clear itemAddedToCart session variable:
Mage::getSingleton('core/session')->unsItemAddedToCart();
?>
<?php endif; ?>
Hope this helps others.
You should be able to accomplish this via XML in the layout XML file of your extension.
<catalog_product_view>
<reference name="footer">
<block type="core/template" name="INSERT_CUSTOM_NAME_HERE" template="path/to/your/phtml/file.phtml" />
</reference>
</catalog_product_view>
Make sure you delete your cache because XML updates are only applied after refreshing your cache.
If you are using the default Magento theme you don't need to call this template because it automatically calls all children $this->getChildHtml('');.
But if you are using a custom theme you should add this to your footer.phtml: $this->getChildHtml('INSERT_CUSTOM_NAME_HERE');.
Or if you'd like to have the javascript on the cart page you should replace <catalog_product_view> with <checkout_cart_index>.
I'm having some trouble getting a custom Reference block to work in Magento.
These are the steps I have taken:
Step 1
Created a new “Reference” block in page.xml
<block type="core/text_list" name="newreference" as="newreference"></block>
Step 2
Added a reference to this block in the place I want it to appear in the page (above the footer in 1column.phtml, 2columns-left.phtml, 2columns-right.phtml, 3columns.phtml)
<?php $this->getChildHtml('newreference'); ?>
Step 3
Added a reference to catalog.xml which tells Magento I want to output a template part (specialfooter.phtml) in the ‘newreference’ Reference block on Category pages
<reference name="newreference">
<block type="core/template" name="specialfooter" template="page/html/specialfooter.phtml"></block>
</reference>
Step 4
Created ‘specialfooter.phtml’ in the page/html/ directory with a simple paragraph block to test.
And nothing happens.
The steps I have taken fit with my understanding of how Reference blocks work, but I could be wrong. I'm struggling to find any documentation, official or otherwise, or any previous SO questions which sheds any light on the subject.
I'm using Magento ver. 1.7.0.2.
Any help would be much appreciated.
Don't you have forget a echo ? :
<?php echo $this->getChildHtml('newreference'); ?>
I was having the same problem and this seems to work for me.
This block in layout/page.xml
<block type="page/html/new_newreference" name="newreference" as="newreference" template="page/html/new/newreference.phtml"/>
Can be referenced in a page eg. 1column.phtml in the template/page folder using:
<?php echo $this->getChildHtml('newreference') ?>
Note the correlation between the "type" naming and "template" path and the "name" and "as" with the getChildHtml().
Using the same principle for a product page. This block in layout/catalog.xml
<block type="catalog/product_new" name="catalogreference" as="catalogreference" template="catalog/product/new/catalogreference.html"/>
Can be referenced in template/catalog/product/view.phtml using:
<?php echo $this->getChildHtml('catalogreference'); ?>
Note both these examples are folder specific
If you want a block to use with a widget. Add this block in the appropriate reference block eg "content" or "head" in the relevant xml file eg. page.xml or catalog.xml:
<block type="core/text_list" name="mywidgetblock" as="mywidgetblock">
<label>My widget Block</label>
</block>
NB: I don't understand the "type" declaration but it works?
In the admin panel CMS/Widget/Widget instance new or existing Layout Updates/ Block Reference find "My widget Block" from the drop down.
I'm new to Magento and it took a lot of trial and error to work this out so I hope it helps someone in the same situation.
I am trying to work out how to create custom phtml files to include on view.phtml (and ultimately to be called from any default Magento phtml file).
I have created a seperate phtml file with the content I want in it called productbadges.phtml
This will be pulled through as the last item in
I understand the callout usually is
<?php echo $this->getChildHtml('phtmlfilename') ?>
However I know I need to do add something to catalog.xml so Magento recognizes the callout and can source the correct file. But I do not properly understand Magento's XML syntax.
Could anyone assist?
vicch's response is the correct way of doing it.
However, it's also helpful to know that there is an alternate method:
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'choose_a_block_name',
array('template' => 'folder/myphtmlfile.phtml')
);
I am posting this for general knowledge. This is not the accepted way of doing this, since it is not consistent with how Magento templates and blocks are used.
you can use
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('goodtest/test.phtml')->toHtml(); ?>
see also here:
How do i call .phtml block at specfic page in magento?
and
want to call one phtml file in another phtml file using anchor tag
Given the information you provided, I can only give a general solution.
First you need to find the layout XML for this view.phtml. You should be looking for something like:
<block type="..." name="..." ... template="../view.phtml">
To add the declaration of the new template directly under the wrapping block, it should be:
<block type="..." name="..." ... template="../view.phtml">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
...
</block>
It is also possible to reference the outter block somewhere else:
<reference name="[name_of_view.phtml_block]">
<block type="..." name="phtmlfilename" template="../phtmlfilename.phtml"/>
</reference>
Type of the new template is the class name, which should be core/template or a subtype of it.
The answer to this question is below codes,just change "directory/acc_drop.phtml" to your file path name.
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('directory/acc_drop.phtml')->toHtml(); ?>
I am trying to customize a theme using only local.xml whenever possible. I want to add a static block to the header without modifying header.phtml. This code works fine for the content area, but not for the header:
<default>
<reference name="content">
<block type="cms/block" name="how-it-works-button">
<action method="setBlockId"><block_id>how-it-works</block_id></action>
</block>
</reference>
</default>
Anybody know why? I thought that all I would need is to change “content” to “header”, but nothing shows up when I do.
Thanks for your help!
The content block is a special block known as a core/text_list block (PHP class Mage_Core_Block_Text_List). These blocks will automatically render out any child blocks that are added to them.
The header block, on the other hand, is a page/html_header block (PHP class Mage_Page_Block_Html_Header). This block class inherits from Mage_Core_Block_Template, making it a core/template block. Template blocks will only render sub-blocks if their corresponding phtml template requests the block. So, by adding your block to the header, you're only doing half the work you need to. You'll need to create a custom phtml template.
The simplest way to do this (post 1.4.1.1 is to, in your own theme, create a file at
template/page/html/header.phtml
And then at the end of this file add
<?php echo $this->getChildHtml('how-it-works-button'); ?>
Assuming you've added a block to header block via layout xml, this should render your template.
Please Try this
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('how-it-works')->toHtml() ?>
And this code in header.phtml
add output="toHtml" in the block tag. I think it is only that.