3

I'm trying to incorporate a tracking pixel from Share A Sale into my Magento 2 site.

Their instructions were as follows;

You will be marking changes to the following file: /app/design/frontend/default/[templatename]/template/checkout/success.phtml

(if this file does not exist, please modify this file instead: app/design/frontend/base/default/template/checkout/success.phtml )

Find the line that reads:

<p><strong><?php echo $this->__('Thank you for your purchase!') ?></strong></p>
and just below that line, add the following code:

<?php
$orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$total = $order->getGrandTotal();
$subtotal = $order->getSubtotal();
$discount = $order->getDiscountAmount();
$affiliateTotal = ($subtotal + $discount)
?>
<img src="https://shareasale.com/sale.cfm?tracking=<?php echo $orderId; ?&amount=<?php echo $affiliateTotal; ?>&transtype=sale&merchantID=66031" width="1" height="1">">"

However, the two files mentioned above dont' exist in Magento 2. The closest thing I could find is in; Public_html/vendor/magento/module-checkout/controller/Onepage/Success.php

In which the contents are;

<?php
/**
 *
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Checkout\Controller\Onepage;

class Success extends \Magento\Checkout\Controller\Onepage
{
    /**
     * Order success action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {   
        $session = $this->getOnepage()->getCheckout();
        if (!$this->_objectManager->get('Magento\Checkout\Model\Session\SuccessValidator')->isValid()) {
            return $this->resultRedirectFactory->create()->setPath('checkout/cart');
        }
        $session->clearQuote();
        //@todo: Refactor it to match CQRS  
        $resultPage = $this->resultPageFactory->create();
        $this->_eventManager->dispatch(
            'checkout_onepage_controller_success_action',
            ['order_ids' => [$session->getLastOrderId()]]
        );
        return $resultPage;
    }
}

I'm a front end developer so I possess a basic understanding of PHP that I use for creating themes, I attempted to incorporate the code but it breaks on $orderId = $this->getOrderId();

Any help or guidance would be greatly appreciated.

UPDATE:

I built the module and modified the files in the following manner per the first answer;

Success.phtml

<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?php echo __('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
        <?php  else :?>
            <p><?php echo __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif;?>
            <p><?php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif;?>

    <?php echo $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?php /* @escapeNotVerified */ echo $block->getUrl() ?>"><span><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>
<?php
$orderId = $block->getRealOrderId();
$total = $block->getGrandTotal();
$subtotal = $block->getSubtotal();
$discount = $block->getDiscountAmount();
$affiliateTotal = ($subtotal + $discount)
?>
<img src="https://shareasale.com/sale.cfm?tracking=<?php echo $orderId; ?>&amount=<?php echo $affiliateTotal; ?>&transtype=sale&merchantID=66031" width="1" height="1">
<h1>Order Id: <?php echo $orderId; ?></h1>
<h1>Affiliate Total: <?php echo $affiliateTotal; ?></h1>

Registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'lfc_shareAsale',
    __DIR__
);

Module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="lfc_shareAsale" setup_version="0.0.1" />
</config>

Success.php

<?php
namespace Vendor\Module\Block\Checkout;

class Success extends \Magento\Checkout\Block\Success
{
    /**
     * @return int
     */
    public function getGrandTotal()
    {
        /** @var \Magento\Sales\Model\Order $order */
        $order = $this->_orderFactory->create()->load($this->getLastOrderId());
        return $order->getGrandTotal();
    }
}

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Block\Success"
                type="Vendor\Module\Block\Checkout\Success"/>
</config>

File structure;

File structure

Commands ran successfully;

php magento module:enable lfc_shareAsale
php magento setup:upgrade

Issue now is that echoing $orderId and #affiliateTotal yield empty\0 values.

2

You will have to follow this answer to setup a module that'll let you do what you want: https://magento.stackexchange.com/a/116922/2380

Once you're done with that you will have to modify your theme template app/design/frontend/<Vendor>/<theme>/Magento_Checkout/templates/success.phtml with the following code:

<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?php echo __('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
        <?php  else :?>
            <p><?php echo __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif;?>
            <p><?php /* @escapeNotVerified */ echo __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif;?>

    <?php echo $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?php /* @escapeNotVerified */ echo $block->getUrl() ?>"><span><?php /* @escapeNotVerified */ echo __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>
<?php
$orderId = $block->getRealOrderId();
$total = $block->getGrandTotal();
$subtotal = $block->getSubtotal();
$discount = $block->getDiscountAmount();
$affiliateTotal = ($subtotal + $discount)
?>
<img src="https://shareasale.com/sale.cfm?tracking=<?php echo $orderId; ?&amount=<?php echo $affiliateTotal; ?>&transtype=sale&merchantID=66031" width="1" height="1">">"
  • Thanks so much, I think I may be missing something as I'm echoing the $affiliateTotal, and $orderId variables but they're empty. Updating post with code now... – Aus-tn Jul 1 '16 at 18:19
  • The $affiliateTotal line is missing a semicolon on the end. $affiliateTotal = ($subtotal + $discount); – Alexander May 3 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.