[英]Add Dynamic JS Code on Magento Product Page
On my magento product page I need to add a dynamic JavaScript array base on display upselling products on the product page. 在我的magento产品页面上,我需要基于产品页面上显示的向上销售产品添加动态JavaScript数组。 The goal is to change the images of the upselling products when the user change the color of the main product.
目的是当用户更改主产品的颜色时,更改加售产品的图像。
To achieve my goal I need a custom JavaScript array on every product page that give me information about crossselling product and the associated product image. 为了实现我的目标,我需要在每个产品页面上使用一个自定义JavaScript数组,该数组可以向我提供有关交叉销售产品和相关产品图像的信息。
What is the best way to do this? 做这个的最好方式是什么?
I try this 我尝试这个
add a observer event in my config.xml 在我的config.xml中添加观察者事件
<controller_action_layout_load_before>
<observers>
<crossselling_product_view>
<type>singleton</type>
<class>XXXXXXXX_Crossselling_Model_Observer</class>
<method>productview</method>
</crossselling_product_view>
</observers>
</controller_action_layout_load_before>
add observer to add specific JS Code 添加观察者以添加特定的JS代码
<?php
class XXXXXXXX_Crossselling_Model_Observer {
public function productview(Varien_Event_Observer $observer) {
$product = Mage::registry('current_product');
//only on product page
if (!($product instanceof Mage_Catalog_Model_Product)) {
return;
}
$controller = $observer->getAction();
$layout = $controller->getLayout();
$block = $layout->createBlock('core/text');
$block->setText(
'<script type="text/javascript">
function main_pulsestorm_hellojavascript()
{
alert("Foo");
}
main_pulsestorm_hellojavascript();
</script>'
);
$layout->getBlock('head')->append($block);
}
}
My error: Fatal error: Call to a member function append() on a non-object 我的错误: 致命错误:在非对象上调用成员函数append()
What is my problem and is it the right way to add dynamic js code? 我的问题是什么,添加动态js代码是否正确?
I would probably approach this from a different angle. 我可能会从另一个角度来解决这个问题。 Since you are only interested in interacting with data and output for the upsell block, you could change the behavior of just that block by observing its output and appending your extra JavaScript.
由于您只对与upsell块的数据和输出进行交互感兴趣,因此可以通过观察该块的输出并附加额外的JavaScript来更改该块的行为。 For the purposes of brevity this answer assumes that you understand the basics of Magento extensions.
为了简洁起见,此答案假定您了解Magento扩展的基础。
core_block_abstract_to_html_after
event: core_block_abstract_to_html_after
事件: etc/config.xml 等/ config.xml中
<core_block_abstract_to_html_after>
<observers>
<addCustomUpsellFormat>
<class>XXXXXXXX_Crossselling_Model_Observer</class>
<method>addCustomUpsellFormat</method>
</addCustomUpsellFormat>
</observers>
</core_block_abstract_to_html_after>
Mage_Catalog_Block_Product_List_Upsell
by appending the output of a new block that will read their data: Mage_Catalog_Block_Product_List_Upsell
实例采取Mage_Catalog_Block_Product_List_Upsell
: Model/Observer.php 型号/ Observer.php
public function addCustomUpsellFormat(Varien_Event_Observer $observer)
{
/* @var Mage_Core_Block_Abstract $block */
$block = $observer->getBlock();
if ($block instanceof Mage_Catalog_Block_Product_List_Upsell) {
/* @var Varien_Object $transport */
$transport = $observer->getTransport();
// Receive the standard output for the block.
$output = $transport->getHtml();
/* @var Mage_Core_Model_Layout $layout */
$layout = $block->getLayout();
$json = $layout->createBlock('core/template')
->setTemplate('catalog/product/list/upsell_json.phtml')
->setItems($block->getItems())
->toHtml();
// Append new JSON data to block output.
$transport->setHtml($output . $json);
}
return $this;
}
app/design/frontend/base/default/template/catalog/product/list/upsell_json.phtml 应用程序/设计/前端/基/默认/模板/目录/产品/列表/ upsell_json.phtml
<?php
$_json = array(); // Add data in here to convert to JSON for output.
$_items = $this->getItems();
/* @var Mage_Catalog_Model_Product $_product */
foreach ($_items as $_product) {
$_json[$_product->getId()] = array(
'image' => (string)Mage::helper('catalog/image')->init($_product, 'image')
);
}
?>
<script type="text/javascript">var upsellData = <?php echo json_encode($_json) ?>;</script>
Use 采用
$controller = $observer->getEvent()->getAction();
instead of 代替
$controller = $observer->getAction();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.