Magento 2 Load specific attribute on front end - magento

I'm trying to modify the product page. I know what files I need to change, however I can't seem to work out what specific thing I need to do.
How do you load a specific attribute to display on the product page? I want a specific one to appear in a more prominent position than the "More Information" tab.
I can sort out the styling and layout stuff, I just need to know how to grab a specific attribute.
Additional question: I've set one up as "Image Swatch". Is there a way to get it to display the image on the front end (it only seems to images when it's a product option).

Best way - Create a block
You can edit the relevant XML file to create a block and position it where you need it. You can add a container (a div with a specific class) if you need to;
<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="product/view/attribute.phtml" after="product.info.type">
<arguments>
<argument name="at_call" xsi:type="string">getSku</argument>
<argument name="at_code" xsi:type="string">sku</argument>
<argument name="css_class" xsi:type="string">sku</argument>
</arguments>
</block>
In the phtml template
If your working inside a phtml template you co do the below;
<?php $_product = $block->getProduct(); ?>
<?php echo $_product->getAttributeText('size');?>
Reference
http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#xml-manage-block
If anyone has a better way it would be interesting to find out.

You can also load attribute in custom template by:
<?php /* #escapeNotVerified */ echo $product->getResource()->getAttribute('my_attribute_id')->getFrontend()->getValue($product); ?>

Related

How to add custom content on Magento Catalog page under each product name?

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>

Fast login form on the right side in magento

I want to add same login form in my site
is there any module available for that or we can have something else coding changes for this
please suggest me something
i want something like this :
http://livedemo00.template-help.com/magento_42632/
You could create a block of type customer/form_login. Using the XML as follows
<block type="customer/form_login" name="customer_small_login" template="customer/form/small-login.phtml" />
You can view the customer/form/login.phtml form for reference of what kind of fields your block is expecting.0
You can then add this to your sidebar using
<?php $this->getChildHtml('customer_small_login'); ?>
NOTE: Ensure your adding you XML block as a child node of the sidebar which you wish to include it in.

Magento: What is the reference name for the block controlled by attributes.phtml? (using EasyTabs)

I'm using Magento with the EasyTabs extension. I have a customized attributes.phtml I want to use for certain products, I'm trying to use a design update XML to switch the default attributes.phtml for the custom one on certain products only. I'm trying to figure out the correct reference name for that block.
The template hint for the block shows:
frontend/default/MY_THEME_NAME/template/easytabs/attributes.phtml
The XML I'm trying to use for the layout update is:
<reference name="[REFERENCE NAME???]">
<action method="setTemplate"><template>[CUSTOM ATTRIBUTES.PHTML]</template></action>
</reference>
I've tried:
product.info.attributes
product_info_attributes
product.attributes
product_attributes
easytabs.attributes
easytabs_attributes
product.view.attributes
product_view_attributes
None of these are working. What is the correct reference name for this block when using EasyTabs?
I don't know what "EasyTabs" are, but if you temporarily edit attributes.phtml to include the following
<?php var_dump($this->getNameInLayout()); ?>
the name of the block will be output to the screen.

Magento Including a CUSTOM phtml file in view.phtml

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(); ?>

Trying to build an AJAX cart in Magento, configuration options not showing

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().

Resources