简体   繁体   中英

Controller array value pass to view page javascript script tag in opencart

I'm working on opencart 2.0.1.1 where I am getting data from a model to the controller. Following this, I need to pass the data view page with th script tag. I have done this but it is displaying the dollowing issue.

(Notice: Array to string conversion in C:\\xampp\\www\\htdocs\\abc\\catalog\\view\\theme\\default\\template\\common\\success.tpl on line)

Any help in this regard will be appreciated.

controller code

success.php

    $this->load->model('checkout/order'); 
    $data['orderDetails'] = $this->model_checkout_order->getOrder($this->session->data['order_id']);
    $data['ordertax'] = $this->model_checkout_order->gettax($this->session->data['order_id']);
    $data['orderProduct'] = $this->model_checkout_order->getOrderProduct($this->session->data['order_id']);
    $this->response->setOutput($this->load->view('default/template/common/success.tpl', $data));

View code

success.tpl

<script type="text/javascript">
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
      'id': "<?php $orderDetails['order_id'];?>",                     // Transaction ID. Required. dynamic variable of order id
      'affiliation': "<?php $orderDetails['store_name'];?>",   // Affiliation or store name. Kuberan Silks
      'revenue': "<?php $orderDetails['total'];?>",               // Grand Total. grand total dynamic variable of the price
      'shipping':"<?php $ordertax['value'];?>" ,                  // Shipping. dynamic variable of shipping
      'tax': "<?php $orderProduct['tax'];?>"                     // Tax. dynamic tax variable
  });

  ga('ecommerce:addItem', {
      'id': "<?php $orderProduct['order_id'];?>",                     // Transaction ID. Required. 
      'name': "<?php $orderProduct['name'];?>",    // Product name. Required.
      'sku': "<?php $orderProduct['model'];?>",                 // SKU/code.
      //'category': 'Party Toys',         // Category or variation.
      'price': "<?php $orderProduct['price'];?>",                 // Unit price.
      'quantity':"<?php $orderProduct['quantity'];?>"                  // Quantity.
  });
  ga('ecommerce:send');
</script>
<?php echo $footer; ?>

when ever i checked inspected element it is showing like:

<script type="text/javascript">
  ga('require', 'ecommerce');
  ga('ecommerce:addTransaction', {
      'id': "<b>Notice</b>: Undefined variable: orderDetails in <b>C:\xampp\www\htdocs\kuberan\catalog\view\theme\default\template\common\success.tpl</b> on line <b>41</b>",                     
      'affiliation': "<b>Notice</b>: Undefined variable: orderDetails in <b>C:\xampp\www\htdocs\kuberan\catalog\view\theme\default\template\common\success.tpl</b> on line <b>42</b>",   
  });

  ga('ecommerce:addItem', {
      'id': "<b>Notice</b>: Undefined variable: orderProduct in <b>C:\xampp\www\htdocs\kuberan\catalog\view\theme\default\template\common\success.tpl</b> on line <b>49</b>",                     
      'sku': "<b>Notice</b>: Undefined variable: orderProduct in <b>C:\xampp\www\htdocs\kuberan\catalog\view\theme\default\template\common\success.tpl</b> on line <b>51</b>",                 
  });
  ga('ecommerce:send');
</script>

Array to string conversion.

This error you get when you try to echo the array value in PHP. You cannot use the array as a string!!

To overcome this issue you can add key to the array you want to print.

For example.

<?php $orderDetails[0]['order_id'];?>

This will return the value of order_id in $orderDetails array at the key of 0. It is recommended you use for loop to get the values from the array as you cannot be sure how many keys will be there.

Good Luck!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM