Jump to content
operador

Products Id in order.confirmation.tpl

Recommended Posts

I need get the Product Id´s from an order in order-confirmation.tpl

I suppose I have to use an foearch but I dont know so much about smarty

Can you help me?

Share this post


Link to post
Share on other sites

In OrderConfirmationController.php you will get id_cart. Using id_cart you can get products of that order. But to do that you need to override OrderConfirmationController.php

This is default code for initContent() method of OrderConfirmationCOntroller.php

public function initContent()
    {
        parent::initContent();

        $this->context->smarty->assign(array(
            'is_guest' => $this->context->customer->is_guest,
            'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
            'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn()
        ));

        if ($this->context->customer->is_guest) {
            $this->context->smarty->assign(array(
                'id_order' => $this->id_order,
                'id_cart' => $this->id_cart,
                'cart' => new Cart($this->id_cart, $this->context->language->id),
                'reference_order' => $this->reference,
                'id_order_formatted' => sprintf('#%06d', $this->id_order),
                'email' => $this->context->customer->email
            ));
            /* If guest we clear the cookie for security reason */
            $this->context->customer->mylogout();
        }

        $this->setTemplate(_PS_THEME_DIR_.'order-confirmation.tpl');
    }

and this is code after override.

public function initContent()
    {
        parent::initContent();
		$cart = new Cart($this->id_cart, $this->context->language->id);
		$products = $cart->getProducts();
        $this->context->smarty->assign(array(
            'is_guest' => $this->context->customer->is_guest,
            'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
            'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn()
        ));

        if ($this->context->customer->is_guest) {
            $this->context->smarty->assign(array(
                'id_order' => $this->id_order,
                'products' => $products,
                'reference_order' => $this->reference,
                'id_order_formatted' => sprintf('#%06d', $this->id_order),
                'email' => $this->context->customer->email
            ));
            /* If guest we clear the cookie for security reason */
            $this->context->customer->mylogout();
        }

        $this->setTemplate(_PS_THEME_DIR_.'order-confirmation.tpl');
    }

After doing this you can access all data of products of that order form $products array variable in order-confirmation.tpl

 

Hope this will help you.

  • Like 1

Share this post


Link to post
Share on other sites

I add this code OrderConfirmationController.php, then I add this other code in order-confirmation.tlp but it dont work:

{foreach $products as $product}
              '{$product["id_product"]}',
{/foreach}

Share this post


Link to post
Share on other sites

Check at both places in OrderConfirmationController.php and order-confirmation.tp with print_r and printr function to check what array contains. Because what i have told you is tested code with my installation. You can check in .php file like this

print_r($products);

and in .tpl file like this

{$products|print_r}

and also verify that $this->id_cart have proper value.

Share this post


Link to post
Share on other sites
print_r($products);

Works fine in php file. Show all info about the order, but

{$products|print_r}

in tpl file print "1" value

 

Any idea?

Edited by operador

Share this post


Link to post
Share on other sites

Also, you should use {$product.id_product} to call an array element in Smarty, not {$product["id_product"]}

Share this post


Link to post
Share on other sites

Thanks rocky, I tried this too, but de issue is like OrderConfirmationController.php dont pass variables to tpl file.

 

I use print_r to check the values, and all variables from OrderConfirmationController.php print "1" in order-confirmation.tpl

 

{$id_order|print_r} this print 1
{$email|print_r} this print 1

Share this post


Link to post
Share on other sites

Then you'll have to override OrderConfirmationController.php to correctly pass the right variables.

Share this post


Link to post
Share on other sites

It would seem you're passing the variables incorrectly if they are all 1. Can you paste the override here so we can check it?

Share this post


Link to post
Share on other sites

of couse

class OrderConfirmationControllerCore extends FrontController
{
    public $ssl = true;
    public $php_self = 'order-confirmation';
    public $id_cart;
    public $id_module;
    public $id_order;
    public $reference;
    public $secure_key;

    /**
     * Initialize order confirmation controller
     * @see FrontController::init()
     */
    public function init()
    {
        parent::init();

        $this->id_cart = (int)(Tools::getValue('id_cart', 0));
        $is_guest = false;

        /* check if the cart has been made by a Guest customer, for redirect link */
        if (Cart::isGuestCartByCartId($this->id_cart)) {
            $is_guest = true;
            $redirectLink = 'index.php?controller=guest-tracking';
        } else {
            $redirectLink = 'index.php?controller=history';
        }

        $this->id_module = (int)(Tools::getValue('id_module', 0));
        $this->id_order = Order::getOrderByCartId((int)($this->id_cart));
        $this->secure_key = Tools::getValue('key', false);
        $order = new Order((int)($this->id_order));
        if ($is_guest) {
            $customer = new Customer((int)$order->id_customer);
            $redirectLink .= '&id_order='.$order->reference.'&email='.urlencode($customer->email);
        }
        if (!$this->id_order || !$this->id_module || !$this->secure_key || empty($this->secure_key)) {
            Tools::redirect($redirectLink.(Tools::isSubmit('slowvalidation') ? '&slowvalidation' : ''));
        }
        $this->reference = $order->reference;
        if (!Validate::isLoadedObject($order) || $order->id_customer != $this->context->customer->id || $this->secure_key != $order->secure_key) {
            Tools::redirect($redirectLink);
        }
        $module = Module::getInstanceById((int)($this->id_module));
        if ($order->module != $module->name) {
            Tools::redirect($redirectLink);
        }
    }

    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
     
     
     public function initContent()
    {
        parent::initContent();
        $cart = new Cart($this->id_cart, $this->context->language->id);
        $products = $cart->getProducts();
        $this->context->smarty->assign(array(
            'is_guest' => $this->context->customer->is_guest,
            'HOOK_ORDER_CONFIRMATION' => $this->displayOrderConfirmation(),
            'HOOK_PAYMENT_RETURN' => $this->displayPaymentReturn()
        ));

        if ($this->context->customer->is_guest) {
            $this->context->smarty->assign(array(
                'id_order' => $this->id_order,
                'products' => $products,
                'reference_order' => $this->reference,
                'id_order_formatted' => sprintf('#%06d', $this->id_order),
                'email' => $this->context->customer->email
            ));
            /* If guest we clear the cookie for security reason */
            $this->context->customer->mylogout();
        }

        $this->setTemplate(_PS_THEME_DIR_.'order-confirmation.tpl');
    }
     
     

    /**
     * Execute the hook displayPaymentReturn
     */
    public function displayPaymentReturn()
    {
        if (Validate::isUnsignedId($this->id_order) && Validate::isUnsignedId($this->id_module)) {
            $params = array();
            $order = new Order($this->id_order);
            $currency = new Currency($order->id_currency);

            if (Validate::isLoadedObject($order)) {
                $params['total_to_pay'] = $order->getOrdersTotalPaid();
                $params['currency'] = $currency->sign;
                $params['objOrder'] = $order;
                $params['currencyObj'] = $currency;

                return Hook::exec('displayPaymentReturn', $params, $this->id_module);
            }
        }
        return false;
    }

    /**
     * Execute the hook displayOrderConfirmation
     */
    public function displayOrderConfirmation()
    {
        if (Validate::isUnsignedId($this->id_order)) {
            $params = array();
            $order = new Order($this->id_order);
            $currency = new Currency($order->id_currency);

            if (Validate::isLoadedObject($order)) {
                $params['total_to_pay'] = $order->getOrdersTotalPaid();
                $params['currency'] = $currency->sign;
                $params['objOrder'] = $order;
                $params['currencyObj'] = $currency;

                return Hook::exec('displayOrderConfirmation', $params);
            }
        }
        return false;
    }
}

 

Share this post


Link to post
Share on other sites

You can try moving the following code outside of the if statement so it applies to customers as well as guests:

$this->context->smarty->assign(array(
                'id_order' => $this->id_order,
                'products' => $products,
                'reference_order' => $this->reference,
                'id_order_formatted' => sprintf('#%06d', $this->id_order),
                'email' => $this->context->customer->email
            ));

Otherwise, the code looks fine to me. I have no idea why the variables would display as 1.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More