为Magento创建Ajax模块

时间:2014-09-23 11:09:59

标签: php jquery ajax magento

我试过按照本教程;

http://www.atwix.com/magento/ajax-requests-in-magento/

但是到最后却变得有点困惑。

我尝试创建的模块是使用AJAX加载高级Google再营销脚本所需的动态值的模块。生成这些值的代码工作正常,因为我在没有缓存的测试环境中对此进行了测试。一旦引入了缓存,值就会停止正确 - 所以希望每次都加载Ajax。

这些是我的文件;

\应用\代码\本地\公司名称\行销\控制器\ AjaxController.php



<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     default_modern
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

public function remarketingAction()
{
    echo '<!-- Google Code for Remarketing Tag -->';
    echo '<!--';
    echo 'Remarketing tags may not be associated with personally identifiable information or placed on pages related to sensitive categories. See more information and instructions on how to setup the tag on: http://google.com/ads/remarketingsetup';
    echo '-->';

    echo '<script type="text/javascript">';
    echo 'var google_tag_params = {';
        if (Mage::getSingleton('cms/page')->getIdentifier() == 'homepage'  && Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') {
            echo 'ecomm_pagetype: \'home\'';
        } else if ($this->getRequest()->getControllerName() == 'result' || $this->getRequest()->getControllerName() == 'advanced') {
            echo 'ecomm_pagetype: \'searchresults\'';
        } else if ($this->getRequest()->getControllerName() == 'catalog' || $this->getRequest()->getControllerName() == 'category') {
            echo 'ecomm_pagetype: \'category\'';
        } else if ($this->getRequest()->getControllerName() == 'product' || $this->getRequest()->getControllerName() == 'current_product')  {
            $product_id = Mage::registry('current_product')->getId();
            $prices = Mage::registry('current_product')->getFinalPrice();
            $prices = number_format($prices, 2, '.', '');
            echo ('ecomm_prodid: '.$product_id.',');
            echo 'ecomm_pagetype: \'product\',';
            echo ('ecomm_totalvalue: '.$prices);
        } else if ($this->getRequest()->getActionName() == 'success') {
            $quote = Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
            $prices = $quote['grand_total'];
            $prices = number_format($prices, 2, '.', '');
            echo 'ecomm_pagetype: \'purchase\',';
            echo ('ecomm_totalvalue: '.$prices);
        } else if ($this->getRequest()->getControllerName() == 'checkout' || $this->getRequest()->getModuleName() == 'onepagecheckout' || $this->getRequest()->getModuleName() == 'checkout') {
            $quote = Mage::getModel('checkout/session')->getQuote();
            $prices = $quote['grand_total'];
            $prices = number_format($prices, 2, '.', '');
            echo 'ecomm_pagetype: \'cart\',';
            echo ('ecomm_totalvalue: '.$prices);
        } else {
            echo 'ecomm_pagetype: \'other\'';
        }
    echo '};';
    echo '</script>';
    echo '<script type="text/javascript">';
    echo '/* <![CDATA[ */ ';
    echo 'var google_conversion_id = xxxxxxxxxx;';
    echo 'var google_custom_params = window.google_tag_params;';
    echo 'var google_remarketing_only = true;';
    echo ' /* ]]> */ ';
    echo '</script>';
    echo '<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">';
    echo '</script>';
    echo '<noscript>';
    echo '<div style="display:inline;">';
    echo '<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/1055454704/?value=0&guid=ON&script=0"/>';
    echo '</div>';
    echo '</noscript>';
}
&#13;
&#13;
&#13;

** \ app \ code \ local \ CompanyName \ Remarketing \ etc \ config.xml **

& #13;
&#13;
<?xml version="1.0" encoding="UTF-8"?>


<config>
    <modules>
        <CompanyName_Remarketing>
           <version>0.0.1</version>
        </CompanyName_Remarketing>
    </modules>

    <frontend>
        <layout>
            <updates>
                <remarketing>
                    <file>remarketing.xml</file>
                </remarketing>
            </updates>
        </layout>
    </frontend>
</config>
&#13;
&#13;
&#13;

** \ app \ design \ frontend \ Package \ Theme \ Layout \ _reReteting.xml **

& #13;
&#13;
<?xml version="1.0"?>
<!--
/**
 * Magento Enterprise Edition
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Magento Enterprise Edition License
 * that is bundled with this package in the file LICENSE_EE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.magentocommerce.com/license/enterprise-edition
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     enterprise_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://www.magentocommerce.com/license/enterprise-edition
 */

-->
<layout version="0.1.0">
    <remarketing_ajax_remarketing>
        <block type="remarketing/remarketing" name="root" template="page/html/footer.phtml" />
    </remarketing_ajax_remarketing>
</layout>
&#13;
&#13;
&#13;

\程序\设计\前台\包\主题\模板\页面\ HTML \ footer.phtml

&#13;
&#13;
<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     default_modern
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>
<div class="footer-container">
    <div class="footer">
            <?php echo $this->getChildHtml('footer_block') ?>
    </div>
    <address><?php echo $this->getCopyright() ?></address>

    <div id="remarketing">
    </div>

    <script>
        jQuery.ajax({
            url: "/remarketing/ajax/remarketing",
            type: "POST",
            data: "size="+jQuery(this).val(),
            success: function(data) {
            jQuery('#remarketing').html(data);
            }
        });
    </script>

</div>
&#13;
&#13;
&#13;

希望这是我做错了一些简单的事情。

我非常感谢任何帮助。

谢谢

0 个答案:

没有答案