How can i reference category name and image from a static block in magento through the magento backend? I'm running 1.7.
I am not aware of a way in which you can easily reference these values from within a static block.
Instead, I would suggest you create and use a widget (one of the most underused features of Magento in my opinion) which will provide a much cleaner and more extendible way of achieving this - though it does require more work upfront :)
Please see code below for a full (simplified) example of a Magento Widget which does exactly what you have asked from the static block:
app/etc/modules/YourCompany_Categorywidget.xml
<config>
<modules>
<MyCompany_Categorywidget>
<active>true</active>
<codePool>community</codePool>
</MyCompany_Categorywidget>
</modules>
</config>
app/code/community/MyCompany/Categorywidget/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<MyCompany_Categorywidget>
<version>1.0.0</version>
</MyCompany_Categorywidget>
</modules>
<global>
<blocks>
<categorywidget>
<class>MyCompany_Categorywidget_Block</class>
</categorywidget>
</blocks>
<helpers>
<categorywidget>
<class>MyCompany_Categorywidget_Helper</class>
</categorywidget>
</helpers>
</global>
</config>
app/code/community/MyCompany/Categorywidget/etc/widget.xml
<?xml version="1.0"?>
<widgets>
<category_widget type="categorywidget/catalog_category_widget_info" translate="name description" module="categorywidget">
<name>Category Info Block</name>
<description>Category Info Block showing name, image etc</description>
<parameters>
<block_title translate="label">
<required>1</required>
<visible>1</visible>
<label>Block Title</label>
<type>text</type>
</block_title>
<template>
<required>1</required>
<visible>1</visible>
<label>Template</label>
<type>select</type>
<value>categorywidget/info.phtml</value>
<values>
<default translate="label">
<value>categorywidget/info.phtml</value>
<label>Category Widget Info Block - Default Template</label>
</default>
<!-- Add different temmplates here for different block positions -->
</values>
</template>
<category translate="label">
<visible>1</visible>
<required>1</required>
<label>Category</label>
<type>label</type>
<helper_block>
<type>adminhtml/catalog_category_widget_chooser</type>
<data>
<button translate="open">
<open>Select Category...</open>
</button>
</data>
</helper_block>
<sort_order>10</sort_order>
</category>
</parameters>
</category_widget>
</widgets>
app/code/community/MyCompany/Categorywidget/Helper/Data.php
<?php
class MyCompany_Categorywidget_Helper_Data extends Mage_Core_Helper_Abstract
{}
app/code/community/MyCompany/Categorywidget/Block/Catalog/Category/Widget/Info.php
<?php
class MyCompany_Categorywidget_Block_Catalog_Category_Widget_Info
extends MyCompany_Categorywidget_Block_Catalog_Category_Info
implements Mage_Widget_Block_Interface
{
protected function _prepareCategory()
{
$this->_validateCategory();
$category = $this->_getData('category');
if (false !== strpos($category, '/')) {
$category = explode('/', $category);
$this->setData('category', (int)end($category));
}
return parent::_prepareCategory();
}
}
app/code/community/MyCompany/Categorywidget/Block/Catalog/Category/Info.php
<?php
class MyCompany_Categorywidget_Block_Catalog_Category_Info extends Mage_Core_Block_Template
{
protected $_category;
protected function _beforeToHtml()
{
$this->_category = $this->_prepareCategory();
return parent::_beforeToHtml();
}
protected function _prepareCategory()
{
$this->_validateCategory();
return Mage::getModel('catalog/category')->load($this->_getData('category'));
}
protected function _validateCategory()
{
if (! $this->hasData('category')) {
throw new Exception('Category must be set for info block');
}
}
public function getCategoryName()
{
return $this->_category->getName();
}
public function getCategoryImage()
{
return $this->_category->getImageUrl();
}
}
app/design/frontend/base/default/template/categorywidget/info.phtml
<?php
$_categoryName = $this->getCategoryName();
$_categoryImage = $this->getCategoryImage();
?>
<div class="categoryinfo_block block">
<p><strong><?php echo $_categoryName ?></strong></p>
<img src="<?php echo $_categoryImage ?>" alt="<?php echo $_categoryName ?>" />
</div>
I wanted the image to link to the category as well so I added ...
app/code/community/MyCompany/Categorywidget/Block/Catalog/Category/Info.php
public function getCategoryUrl()
{
return $this->_category->getUrl();
}
app/design/frontend/base/default/template/categorywidget/info.phtml
<?php
$_categoryName = $this->getCategoryName();
$_categoryImage = $this->getCategoryImage();
$_categoryUrl = $this->getCategoryUrl();
?>
<div class="categoryinfo_block block">
<p><strong><?php echo $_categoryName ?></strong></p>
<a href="<?php echo $_categoryUrl ?>" title="<?php echo $_categoryName ?>" alt="<?php echo $_categoryName ?>">
<img src="<?php echo $_categoryImage ?>" alt="<?php echo $_categoryName ?>" />
</a>
</div>
If that helps anyone else?
Related
I have the original file and I want to append fragment in security tag.
<!-- Original File -->
<example>
<header>
<facticeA>123</facticeA>
<facticeB>456</facticeB>
</header>
<body>
<facticeC>789</facticeC>
</body>
<security></security>
</example>
<!-- ------------ -->
<!-- Original Fragment -->
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_eb0b47cc-d4b0-44ba-a08c-90047e3a8b03" IssueInstant="2022-07-18T14:08:46.138Z" Version="2.0">
<saml2:Issuer></saml2:Issuer>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">XXXXXXXX</Signature>
</saml2:Assertion>
<!-- ----------------- -->
I use "createDocumentFragment()" and "appendXml()" PHP functions
And I have this result.
<!-- Final File -->
<example>
<header>
<facticeA>123</facticeA>
<facticeB>456</facticeB>
</header>
<body>
<facticeC>789</facticeC>
</body>
<security>
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" xmlns:default="http://www.w3.org/2000/09/xmldsig#" ID="_eb0b47cc-d4b0-44ba-a08c-90047e3a8b03" IssueInstant="2022-07-18T14:08:46.138Z" Version="2.0">
<saml2:Issuer/>
<default:Signature xmlns="http://www.w3.org/2000/09/xmldsig#">XXXXXXXX</default:Signature>
</saml2:Assertion>
</security>
</example>
<!-- ---------- -->
The inserted fragment is not the same than original fragment.
"Signature" tag become "default:Signature" tag. And the namespace "xmldsig" present in Signature tag is append to Assertion tag with word "default"
If I delete namespace "xmldsig" in the Signature tag I have no problem.
The inserted fragment is the same than original fragment.
This seems to be a bug in the node import. A possible workaround is to define a prefix for this namespace on the document element.
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->loadXML($exampleXML);
$xpath = new DOMXpath($document);
$document->documentElement->setAttributeNS(
'http://www.w3.org/2000/xmlns/', 'xmlns:sig', 'http://www.w3.org/2000/09/xmldsig#'
);
$fragment = $document->createDocumentFragment();
$fragment->appendXML($samlXML);
foreach ($xpath->evaluate('(//security)[1]') as $security) {
$security->appendChild($fragment);
}
$document->formatOutput = true;
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<example xmlns:sig="http://www.w3.org/2000/09/xmldsig#">
<header>
<facticeA>123</facticeA>
<facticeB>456</facticeB>
</header>
<body>
<facticeC>789</facticeC>
</body>
<security>
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_eb0b47cc-d4b0-44ba-a08c-90047e3a8b03" IssueInstant="2022-07-18T14:08:46.138Z" Version="2.0">
<saml2:Issuer/>
<sig:Signature xmlns="http://www.w3.org/2000/09/xmldsig#">XXXXXXXX</sig:Signature>
</saml2:Assertion>
</security>
</example>
Interesting enough, if you create the nodes using DOM methods it works correctly:
$document = new DOMDocument();
$document->preserveWhiteSpace = false;
$document->loadXML($exampleXML);
$xpath = new DOMXpath($document);
foreach ($xpath->evaluate('(//security)[1]') as $security) {
$security->appendChild(
$saml = $document->createElementNS('urn:oasis:names:tc:SAML:2.0:assertion', 'saml2:Assertion')
);
$saml->setAttribute('ID', '_eb0b47cc-d4b0-44ba-a08c-90047e3a8b03');
$saml->appendChild(
$document->createElementNS('urn:oasis:names:tc:SAML:2.0:assertion', 'saml2:Issuer')
);
$saml->appendChild(
$signature = $document->createElementNS('http://www.w3.org/2000/09/xmldsig#', 'Signature')
);
$signature->textContent = 'XXXXXXXX';
}
$document->formatOutput = true;
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<example>
<header>
<facticeA>123</facticeA>
<facticeB>456</facticeB>
</header>
<body>
<facticeC>789</facticeC>
</body>
<security>
<saml2:Assertion xmlns:saml2="urn:oasis:names:tc:SAML:2.0:assertion" ID="_eb0b47cc-d4b0-44ba-a08c-90047e3a8b03">
<saml2:Issuer/>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">XXXXXXXX</Signature>
</saml2:Assertion>
</security>
</example>
You could create a recursive function the recreates the nodes from the fragment.
Below is my PHP code and XML file, I have been trying with so many different echos to loop through my images in my XML file to display each image to correct product but can only display the first image to all three products.
XML code:
<my_products>
<product>
<id>1</id>
<image> csuT.jpg</image>
<name>Champion T-Shirt</name>
<price>18.00</price>
<description>
Get the perfect look to let everyone know you are a stylish fan!
</description>
</product>
<product>
<id>2</id>
<image> webBook.jpg</image>
<name>C# Programming: Analysis to Program Design</name>
<price>192.00</price>
<description>
Your hands-on guide to Microsoft Visual C# fundamentals with Visual Studio 2017
</description>
</product>
<product>
<id>3</id>
<image> calcPic.jpg</image>
<name>Calculator TI-BAII Plus 10DIG/24CASH</name>
<price>39.00</price>
<description>
Performs common math as well as various financial functions
</description>
</product>
</my_products>
PHP code:
$xml = simplexml_load_file($file);
$script_images="";
$script_products="";
$script_product_prices="";
//Loop through the products defined in the products.xml file
foreach ($xml->product as $r)
{
$script_images.="products[".($r->id)."]=\"".($r->image)."\";\n";
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
$script_product_prices.="product_prices[".($r->id)."]=\"".($r->price)."\";\n";
?>
<div>
<p class="lead">
<h3 class="pull-right no-top-margin"><?php echo $currency_symbol;?><?php echo $r->price;?></h3>
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
</p>
<h3><?php echo $r->name;?></h3>
</p>
<p>
<?php echo $r->description;?>
</p>
<br/>
<?php
//If there is details link set for the product, show a Details button
if(trim($r->details_link)!="")
{
?>
<a target="_blank" href="http://<?php echo str_replace("http://","",trim($r->details_link));?>"</a>
<?php
}
?>
<a class="btn btn-xs btn-info" href="javascript:AddToCart(<?php echo $r->id;?>)">Add to Cart</a>
</div>
<hr/>
<?php
}
?>
<script>
var currency_symbol="<?php echo $currency_symbol;?>";
var products=Array();
<?php echo $script_images;?>
var product_images=Array();
<?php echo $script_products;?>
var product_prices=Array();
<?php echo $script_product_prices;?>
</script>
You just need to extract the image from the XML (the same way as done for the Javascript) and I've added a trim() to remove any spaces round the field...
<h3><?php $image = trim($r->image);
echo "<image src='$image'/>";?></h3>
Also add trim to...
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
What is this? :
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
your src attribute is static and you must change it
I install new theme for magento and change "System > Configuration"> Design.
I write name for new theme in themes options. After "Save config" now I get error when try go to "System > Configuration": Class 'ThemeAdminPanel_ExtraConfig_Helper_Data' not found in .../app/Mage.php on line 546
When upload files for new theme I see that create new folders in app/code/local:
/app/code/local/ThemeAdminPanel
/app/code/local/Mage
app/code/local/ThemeAdminPanel/ExtraConfig/etc/config.xml:
<?xml version="1.0"?>
<config>
<modules>
<ThemeAdminPanel_ExtraConfig>
<version>1.0.0</version>
<depends>
<!-- no dependencies -->
</depends>
</ThemeAdminPanel_ExtraConfig>
</modules>
<global>
<models>
<!-- ... -->
<ExtraConfig>
<class>ThemeAdminPanel_ExtraConfig_Model</class>
</ExtraConfig>
<!-- ... -->
</models>
<helpers>
<ExtraConfig>
<class>ThemeAdminPanel_ExtraConfig_Helper</class>
</ExtraConfig>
</helpers>
<resources />
<extraconfig />
<blocks>
<mygeneral><class>ThemeAdminPanel_ExtraConfig_Block</class></mygeneral>
</blocks>
</global>
<!---->
<default>
<mygeneral>
<generaloptions>
<menutype>1</menutype>
<topbanner>default/phone.png</topbanner>
<googlefont>Source Sans Pro:200,200italic,300,300italic,regular,italic,600,600italic,700,700italic,900,900italic</googlefont>
<additional_nav>Contacts</additional_nav>
<additional_nav_href>index.php/contacts/</additional_nav_href>
<topbtn>1</topbtn>
<enable_ajax>1</enable_ajax>
<hide_wish>0</hide_wish>
<hide_compare>0</hide_compare>
<hide_cart>0</hide_cart>
</generaloptions>
<background>
<maincolor>F7F7F9</maincolor>
<pattern>default/pattern.png</pattern>
<bg_repeat></bg_repeat>
<bg_attachment></bg_attachment>
<bg_position_x></bg_position_x>
<bg_position_y></bg_position_y>
</background>
<product_list>
<shorten_name>23</shorten_name>
<layer>1</layer>
<sale_label>1</sale_label>
<sale_label_text>Sale</sale_label_text>
<new_label>1</new_label>
<new_label_text>New</new_label_text>
</product_list>
<share>
<facebookgroupid>115403961948855</facebookgroupid>
<twittername>dedalx</twittername>
<use_share>1</use_share>
<share_code><![CDATA[<span class='st_fblike_hcount' displayText='Facebook Like'></span>
<span class='st_twitter_hcount' displayText='Tweet'></span>
<span class='st_pinterest_hcount' displayText='Pinterest'></span>
<span class='st_plusone_hcount' displayText='Google +1'></span>]]></share_code>
</share>
<slideshow>
<use_slideshow>1</use_slideshow>
<hide_right_sliders>0</hide_right_sliders>
<autoplay>1</autoplay>
<speed>8000</speed>
<slides_count>2</slides_count>
</slideshow>
<productpage>
<use_zoom>1</use_zoom>
<use_carousel>1</use_carousel>
</productpage>
<colors>
<active_color>6CBE42</active_color>
<button_hover>58BAE9</button_hover>
</colors>
<customcode>
<customcss>/* Add any CSS code here */</customcss>
<customjs>// Add any JavaScript code here</customjs>
</customcode>
</mygeneral>
</default>
<!---->
<adminhtml>
<menu>
<metroshop translate="title" module="ExtraConfig">
<title>MetroShop</title>
<sort_order>6666</sort_order>
<children>
<settings translate="title" module="ExtraConfig">
<title>Theme Settings</title>
<sort_order>10</sort_order>
<action>adminhtml/system_config/edit/section/mygeneral</action>
</settings>
</children>
</metroshop>
</menu>
<layout>
<updates>
<ExtraConfig>
<file>options.xml</file>
</ExtraConfig>
</updates>
</layout>
</adminhtml>
</config>
app/code/local/ThemeAdminPanel/ExtraConfig/Helper/Data.php:
<?php
class ThemeAdminPanel_ExtraConfig_Helper_Data extends Mage_Core_Helper_Abstract
{
}
?>
I see that this class added to code but can't find in /app/Mage.php in this part:
public static function helper($name)
{
$registryKey = '_helper/' . $name;
if (!self::registry($registryKey)) {
$helperClass = self::getConfig()->getHelperClassName($name);
self::register($registryKey, new $helperClass);
}
return self::registry($registryKey);
}
As I understand have some problem in theme about adding class to Mage.php.
Additional in file etc/config.xml don't have Class 'ThemeAdminPanel_ExtraConfig_Helper_Data'. But' I'm not sure how to fix and solve this problem.
Please, help me to solve this problem.
All cache clean by FTP.
I have a problem with Zend navigation and finding active element of menu.
I build an Zend 1.12 application with modules, and read the navigation from xml file.
I wrote a simple helper which store information about current page, and to find this page I use function:
protected function findDeepestActivePage()
{
$naviHelper = $this->getView()->navigation();
$renderInvisible = $naviHelper->getRenderInvisible();
$result = $naviHelper->setRenderInvisible(true)->findActive($naviHelper->getContainer());
$naviHelper->setRenderInvisible($renderInvisible);
if (empty($result)) {
return null;
}
return $result['page'];
}
But in few elements of menu that function can't find active element. Navigation part which is problematic(elements from menu users):
<admin route="admin.summary" id="admin" resource="admin.index.index" visible="1">
<label>Panel Administracyjny</label>
<pages>
<main route="admin.summary" id="amain" resource="admin.index.index" visible="1">
<pages>
<summary route="admin.summary" resource="admin.index.index">
<label>Podsumowanie</label>
<module>admin</module>
<controller>index</controller>
<action>index</action>
</summary>
</pages>
</main>
<users route="admin.users.list" resource="admin.user.index" id="users" visible="1">
<label>Użytkownicy</label>
<pages>
<list route="admin.users.list" resource="admin.user.index" visible="0">
<label>Lista użytkowników</label>
<module>admin</module>
<controller>user</controller>
<action>index</action>
</list>
<add route="admin.users.add" resource="admin.user.add" visible="0">
<label>Dodaj użytkownika</label>
<module>admin</module>
<controller>user</controller>
<action>add</action>
</add>
<details route="admin.users.details" resource="admin.user.details" visible="0">
<label>Szczególy użytkownia</label>
<module>admin</module>
<controller>user</controller>
<action>details</action>
</details>
<edit route="admin.users.edit" resource="admin.user.edit" visible="0">
<label>Edycja użytkownika</label>
<module>admin</module>
<controller>user</controller>
<action>edit</action>
</edit>
</pages>
</users>
</pages>
</admin>
I have other part of navigation with module panel and there is similar part in it and works fine. I try to solve the problem, but everything I found was not helpfull.
My Code is in a single xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created using vQModerator's XML Generator by The Wizard of Osch for http://www.crystalcopy.nl //-->
<!-- (Based on vQmod XML Generator by UKSB - http://www.opencart-extensions.co.uk) //-->
<modification>
<id><![CDATA[vQModerator Installation]]></id>
<version><![CDATA[1.1.6]]></version>
<vqmver><![CDATA[2.4.1]]></vqmver>
<author><![CDATA[Nidhishanker Modi]]></author>
<file name="catalog/view/theme/shopitout/template/common/header.tpl" error="abort">
<operation info="">
<search position="Replace" offset="" ><![CDATA[<img src="<?php echo $logo; ?>" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" />]]></search>
<add><![CDATA[<img src="image/data/my_logo.png" title="<?php echo $name; ?>" alt="<?php echo $name; ?>" />]]></add>
</operation>
</file>
<file name="catalog/controller/module/slideshow.php" error="">
<operation info="">
<search position="After" ><![CDATA[
$results = $this->model_design_banner->getBanner($setting['banner_id']);
]]></search>
<add><![CDATA[echo "Testing Data to hold screen"; exit; ]]></add>
</operation>
</file>
</modification>
Now Friends the second file tag is replacing my search data always. Even i am not giving postion="replace".
where i am wrong please give me suggestions.
There is an error in your first file tag code. Most probably:
Wrong file location (catalog/view/theme/shopitout/template/common/header.tpl) .
Or mentioned search code doesn't exist in catalog/view/theme/shopitout/template/common/header.tpl
Proof for the above statements:
Move the second file tag to first - Working.
Or remove the error="abort" parameter form first file tag - 2nd file code Working
if you still can't find the error, then refer the section under the heading - Opencart Vqmod xml file not working?? in https://sankartypo3.wordpress.com/2013/11/25/opencart-vqmod-tutorial/
Have a nice day!!