1
votes

First off, sorry for the long post. I just can't seem to work past this one.

I am trying to display tracking code using an observer that looks at the success event. I enabled system and exception logs and I don't see any errors. I might have a capitalization / syntax error in my config. This is my first module, and the slashes are backwards because I copy and pasted from my desktop. Here is a zip of the files (only 6) Here is the code:

\app\etc\modules\Capps_Cact.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Capps_Cact>
            <active>true</active>
            <codePool>local</codePool>
        </Capps_Cact>
    </modules>
</config>

app\code\local\Capps\Cact\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Capps_Cact>
            <version>1.0.0</version>
        </Capps_Cact>
    </modules>
    <frontend>
       <layout>
            <updates>
                <Capps>
                    <file>cact.xml</file>
                </Capps>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <cappscact>
                <class>Capps_Cact_Block</class>
            </cappscact>
        </blocks>
        <models>
            <cappscact>
                <class>Capps_Cact_Model</class>
            </cappscact>
        </models>
    </global>   
    <events>
            <checkout_onepage_controller_success_action>
                <observers>
                    <Capps_Cact_order_success>
                        <class>capps_cact/observer</class>
                        <method>setCappsCactOnOrderSuccessPageView</method>
                    </Capps_Cact_order_success>
                </observers>
            </checkout_onepage_controller_success_action>
            <checkout_multishipping_controller_success_action>
                <observers>
                    <Capps_Cact_order_success>
                        <class>capps_cact/observer</class>
                        <method>setCappsCactOnOrderSuccessPageView</method>
                    </Capps_Cact_order_success>
                </observers>
            </checkout_multishipping_controller_success_action>
    </events>   
</config>

app\code\local\Capps\Cact\Model\observer.php

<?php
class Capps_Cact_Model_Observer
{

    /**
     * Add order information into CC block to render on checkout success pages
     *
     * @param Varien_Event_Observer $observer
     */
    public function setCappsCactOnOrderSuccessPageView(Varien_Event_Observer $observer)
    {
        $orderIds = $observer->getEvent()->getOrderIds();
        if (empty($orderIds) || !is_array($orderIds)) {
            return;
        }
        $block = Mage::app()->getFrontController()->getAction()->getLayout()->getBlock('capps_cact');
        if ($block) {
            $block->setOrderIds($orderIds);
        }
    }
}

app\code\local\Capps\Cact\Block\Track.php

<?php

class Capps_Cact_Block_Track extends Mage_Core_Block_Template 
{
     /**
     * Render information about specified orders and their items for Channel Advisor
     *
     * @link http://code.google.com/apis/analytics/docs/gaJS/gaJSApiEcommerce.html#_gat.GA_Tracker_._addTrans
     * @return string
     */
    protected function _getCATrackingCode()
    {
    $orderIds = $this->getOrderIds();
    if (empty($orderIds) || !is_array($orderIds)) {
            echo ('It worked its');
            return;
        } else {
            echo ('did that work');
            $orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
            $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
            echo ('<script type="text/javascript"> 
            ');
            echo ('var _caq = _caq || [];
            ');
            echo ('var products = []; 
            ');
            $Id = '';
            $Qty = '';
            $Price = '';
            $items = $order->getAllItems();
            foreach($items as $item){
                echo ('products.push({Sku: \'');
                echo $Id .= $item->getSku();
                echo ("', ");
                echo ('UnitPrice: \'');
                echo $Price .= $item->getPrice();
                echo ("', ");
                echo ('Quantity: \'');
                echo $Qty .= $item->getQtyOrdered();
                echo ('\'});
                ');
                $Id = '';
                $Qty = '';
                $Price = '';
                } 
            $separateones = substr($separateones, 0,-1);
            echo ('_caq.push(["Order", {OrderId: \'');
            echo $order->getIncrementId();
            echo ('\', Revenue: \'');
            echo $order->getSubtotal();
            echo ('\', CurrencyCode: \'USD\', Products: products}]);
            ');
            echo ('</script>');
        }
    }
}

app\design\frontend\base\default\layout\cact.xml

<?xml version="1.0"?>
<layout version="1.0.0">

<!--
Default layout, loads most of the pages
-->

    <default>
        <!-- Mage_GoogleAnalytics -->
        <reference name="after_body_start">
            <block type="cappscact/track" name="capps_cact" as="capps_cact" template="Capps/Cact.phtml" />
        </reference>
    </default>
</layout>

app\design\frontend\base\default\template\Capps\Cact.phtml

<!-- Begin Channel Advisor Tracking Code -->
    <?php echo $this->_getCATrackingCode(); ?>
<!-- End Channel Advisor Tracking Code -->
1

1 Answers

3
votes

The main problems that I see that <events> node in config.xml is not in right place. Please move <events> node inside <frontend> node. After clear cahce.

I am not sure but I think this

protected function _getCATrackingCode()

will not work.

You cannot call in template files methods which are protected. Make it public.