5

How will final result should look like for this implementation. Thank you

file location: /vendor/magento/module-checkout/Controller/Onepage/Success.php

Below is my default Success.php for Magento 2.1

<?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;
    } }

............................ where do i paste this code on Success.php

<script> window.renderOptIn = function() { window.gapi.load('surveyoptin', function() { window.gapi.surveyoptin.render( { "merchant_id": XXXXXXXXX, "order_id": "<?php echo $order->getIncrementId() ?>", "email": "<?php echo $order->getCustomerEmail() ?>", "delivery_country": "<?php echo $order->getShippingAddress()->getCountryId() ?>", "estimated_delivery_date": "YYYY-MM-DD" }); }); } </script>

4 Answers 4

2

I think you would use the file located at or whatever the vendor/template you use

vendor/magento/module-checkout/view/frontend/templates/success.phtml

In which case, you would put the google code after the last in the success.phtml file

1

You need to put this code in vendor/magento/module-checkout/view/frontend/templates/success.phtml.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?= __('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><?= __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif;?>
            <p><?= /* @escapeNotVerified */ __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif;?>

    <?= $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?= /* @escapeNotVerified */ $block->getContinueUrl() ?>"><span><?= /* @escapeNotVerified */ __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>
<script>
    window.renderOptIn = function() {
        window.gapi.load('surveyoptin', function() {
            window.gapi.surveyoptin.render(
            {
                "merchant_id": XXXXXXXXX,
                "order_id": "<?php echo $order->getIncrementId() ?>",
                "email": "<?php echo $order->getCustomerEmail() ?>",
                "delivery_country": "<?php echo $order->getShippingAddress()->getCountryId() ?>",
                "estimated_delivery_date": "YYYY-MM-DD"
            });
        });
    }
</script>
0

app/design/frontend/magento/theme/Magento_Checkout/templates/success.phtml

Add this script

<script>

  window.renderOptIn = function() {

  window.gapi.load('surveyoptin', function() {

  window.gapi.surveyoptin.render(

    {

      "merchant_id": xxxxxxxxx,

      "order_id": "<?php echo $orderData->getIncrementId() ?>",

      "email": "<?php echo $orderData->getCustomerEmail() ?>",

      "delivery_country": "<?php echo $orderData->getShippingAddress()->getCountryId() ?>",

      "estimated_delivery_date": "YYYY-MM-DD"

    });

 });

 }

</script>
1
  • This looks like code for Magento 1, not Magento 2. Commented Nov 14, 2017 at 19:27
0

How about the GTIN for products review?

     "products":[{"gtin":"GTIN1"}, {"gtin":"GTIN2">}],

Anyone can help to retrieve the "barcode" of the items ordered?

I can retrieve the SKU with this code:

public function getGtinProducts() {
    $order = $this->getOrder();
    $products = [];
    foreach ($order->getAllItems() as $product) {
        $gtin = ["gtin" => $product->getSku()];
        $products[] = (object)$gtin;
    }
    return json_encode($products);
}

But I need to retrieve the barcode attribute instead SKU. Any idea how to?

1
  • create a text attribute to product and than get it. $prdObj = $this->productLoader->create()->load($product->getData('product_id')); $gtin = ['gtin' => $prdObj->getBarcode()]; Commented Jul 3, 2023 at 10:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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