Post by straightlight » Tue Oct 03, 2017 10:27 pm

Followed is a training on how to send values with jQuery codes upon successful checkout process. Whenever a payment provider generates a schema in order to integrate in Opencart, here is a good straightforward example that you could implement into your ocmod or VQmod files from now on. Obviously, the variables will need to be changed accordingly as well as the schema size to fit what's according with your provider.

Dependable on this topic schema example: viewtopic.php?f=190&t=191761

Take note that this strategy requires at least Opencart v2.x releases or above:

1 - In your catalog/controller/checkout/success.php file,

find:

Code: Select all

$this->cart->clear();
add before:

Code: Select all

if (!empty($this->session->data['order_id'])) {
    $data['order_id'] = (int)$this->session->data['order_id'];
}
Then, at the end of the file,

find:

Code: Select all

$this->response->setOutput($this->load->view('common/success', $data));
	}
replace with:

Code: Select all

$this->response->setOutput($this->load->view('common/success', $data));
	}
	
	public function send() {
		$json = array();
		
		if (!empty($this->request->post['order_id'])) {
			$order_id = (int)$this->request->post['order_id'];
			
			$this->load->model('checkout/order');
			
			$this->load->model('catalog/product');
			
			$this->load->model('catalog/category');
			
			$order_info = $this->model_checkout_order->getOrder($order_id);
			
			$order_products = $this->model_checkout_order->getOrderProducts($order_id);
			
			$products_array = array();
			
			$i = 0;
			
			$tax_value = 0.00;
			
			foreach ($order_products as $order_product) {
				$product = $this->model_catalog_product->getProduct($order_product['product_id']);
				
				if (!empty($product['product_id'])) {
					$categories = $this->model_catalog_product->getCategories($product['product_id']);
					
					$categories_array = array();
					
					$j = 0;
					
					foreach ($categories as $category) {
						if (!empty($category['product_id']) && !empty($category['category_id'])) {
							$category_info = $this->model_catalog_category->getCategory($category['category_id']);
							
							if (!empty($category_info['category_id'])) {
								$categories_array[$category['product_id']][$j] = strip_tags(html_entity_decode($category['name'], ENT_QUOTES, 'UTF-8'));
								
								++$j;
							}
						}
					}
					
					if (!empty($categories_array[$product['product_id']][$i])) {
						$products_array[] = array('sku'					=> strip_tags(html_entity_decode($product['sku'], ENT_QUOTES, 'UTF-8')),
												  'name'				=> strip_tags(html_entity_decode($product['name'], ENT_QUOTES, 'UTF-8')),
												  'category'			=> strip_tags(html_entity_decode($categories_array[$product['product_id']][$i], ENT_QUOTES, 'UTF-8')),
												  'price'				=> strip_tags(html_entity_decode($order_product['price'], ENT_QUOTES, 'UTF-8')),
												  'quantity'			=> (int)$order_product['quantity'],
												 );
												 
						$tax_value = (float)$order_product['tax'];
					}
					
					++$i;
				}
			}
			
			if (!empty($products_array)) {
				$json[] = array('transactionId'					=> strip_tags(html_entity_decode((string)$order_info['invoice_prefix'] . $order_info['invoice_no'], ENT_QUOTES, 'UTF-8')),
								'transactionAffiliation'		=> strip_tags(html_entity_decode($order_info['store_name'], ENT_QUOTES, 'UTF-8')),
								'transactionTotal'				=> strip_tags(html_entity_decode(((float)$order_info['total'] - (float)$tax_value), ENT_QUOTES, 'UTF-8')),
								'transactionTax'				=> strip_tags(html_entity_decode($tax_value, ENT_QUOTES, 'UTF-8')),
								'transactionShipping'			=> (!empty($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]]) ? (float)$this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]] : '0',
								'transactionProducts'			=> $products_array,
							   );
			}
			
			$this->response->addHeader('Content-Type: application/json');
			$this->response->setOutput(json_encode($json));
		}
	}
In catalog/view/theme/<your_theme>/common/success.tpl file,

Version 2.x:

find:

Code: Select all

<?php echo $footer; ?>
add below:

Code: Select all

<?php if (!empty($order_id)) { ?>
<script type="text/javascript"><!--
$(document).ready(function() {
	$.ajax({
		url: 'index.php?route=checkout/success/send',
		type: 'post',
		data: 'order_id=' + encodeURIComponent('<?php echo $order_id; ?>'),
		dataType: 'json',
		success: function(json) {
		        // The myJsonString can be removed IF the provider does not require
		        // the use of json strings but rather an array. If an array, simply use the
		        // json variable along with the provider's function.
			var myJsonString = JSON.stringify(json);
						
			// dataLayer and the push function are only for the purposes of a specific API.
			// It can be changed to other relatives when needed.
			dataLayer.push(myJsonString);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script> 
<?php } ?>
Version 3.x:

find:

Code: Select all

{{ footer }}
add below:

Code: Select all

{% if order_id %}
<script type="text/javascript"><!--
$(document).ready(function() {
	$.ajax({
		url: 'index.php?route=checkout/success/send',
		type: 'post',
		data: 'order_id=' + encodeURIComponent('{{ order_id }}'),
		dataType: 'json',
		success: function(json) {
		        // The myJsonString can be removed IF the provider does not require
		        // the use of json strings but rather an array. If an array, simply use the
		        // json variable along with the provider's function.
			var myJsonString = JSON.stringify(json);
						
			// dataLayer and the push function are only for the purposes of a specific API.
			// It can be changed to other relatives when needed.
			dataLayer.push(myJsonString);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});
//--></script> 
{% endif %}
This should resolved the problem.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by straightlight » Thu Oct 05, 2017 6:10 am

The post has been updated above in compliance of the invoice prefix and the invoice number.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by brinquedoseducativos » Fri Mar 23, 2018 5:51 am

Anyone have a tutorial to make a simple onepage custom checkout (from scratch)?
moto infantil,moto elétrica,moto elétrica infantil

Moto Infantil
Brinquedos
Brinquedos Educativos
Aluguel de Louças para Festas
Aluguel de Louças


User avatar

Posts

Joined
Tue Mar 13, 2018 12:28 am


Post by straightlight » Fri Mar 23, 2018 6:02 am

To make sure you're posting on the right topic, you're looking to do use a one page checkout with an API?

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by grubybarti666 » Sat Oct 20, 2018 5:42 pm

Ok but can you tell me what should I paste over here ? Especialy - "ORDER_ID" and "CUSTOMER_EMAIL"

Code: Select all

<script>
  window.renderOptIn = function() { 
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          // WYMAGANE
          "merchant_id":"12345",
          "order_id": "ORDER_ID",
          "email": "CUSTOMER_EMAIL",
          "delivery_country": "COUNTRY_CODE",
          "estimated_delivery_date": "YYYY-MM-DD",

          // OPCJONALNE
          "opt_in_style": "OPT_IN_STYLE"
        }); 
     });
  }
</script>
Last edited by straightlight on Tue Mar 31, 2020 9:20 am, edited 1 time in total.
Reason: Added code tags.

Newbie

Posts

Joined
Sun Dec 18, 2016 5:55 pm

Post by straightlight » Sun Oct 21, 2018 1:07 am

grubybarti666 wrote:
Sat Oct 20, 2018 5:42 pm
Ok but can you tell me what should I paste over here ? Especialy - "ORDER_ID" and "CUSTOMER_EMAIL"

Code: Select all

<script>
  window.renderOptIn = function() { 
    window.gapi.load('surveyoptin', function() {
      window.gapi.surveyoptin.render(
        {
          // WYMAGANE
          "merchant_id":"12345",
          "order_id": "ORDER_ID",
          "email": "CUSTOMER_EMAIL",
          "delivery_country": "COUNTRY_CODE",
          "estimated_delivery_date": "YYYY-MM-DD",

          // OPCJONALNE
          "opt_in_style": "OPT_IN_STYLE"
        }); 
     });
  }
</script>
Partial codes posted.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by brinquedoseducativos » Mon Apr 22, 2019 12:56 pm

straightlight wrote:
Fri Mar 23, 2018 6:02 am
To make sure you're posting on the right topic, you're looking to do use a one page checkout with an API?
Yes, I was looking for a one page checkout using ajax to communicate with an API, but I found a plugin that satisfies my needs.
Thanks for your patience.

Moto Infantil
Brinquedos
Brinquedos Educativos
Aluguel de Louças para Festas
Aluguel de Louças


User avatar

Posts

Joined
Tue Mar 13, 2018 12:28 am


Post by zgua » Tue Mar 31, 2020 7:02 am

Hello! I'm trying to get order data to datalayer and your code is exactly what i need. But... my debugger says it's an error at line

Code: Select all

'transactionShipping'			=> (!empty($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]]) ? (float)$this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]] : '0',
Do you have any idea?

And than i just hardcoded the shipping. But... nothing was pushed to the data layer.
Does somebody else have the code for the success page that's working on 3.x?

Thanks in advance!
Last edited by straightlight on Tue Mar 31, 2020 9:18 am, edited 1 time in total.
Reason: Added code tags.

Newbie

Posts

Joined
Sun Mar 22, 2020 11:59 pm

Post by straightlight » Tue Mar 31, 2020 9:21 am

zgua wrote:
Tue Mar 31, 2020 7:02 am
Hello! I'm trying to get order data to datalayer and your code is exactly what i need. But... my debugger says it's an error at line

Code: Select all

'transactionShipping'			=> (!empty($this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]]) ? (float)$this->session->data['shipping_methods'][$shipping[0]]['quote'][$shipping[2]] : '0',
Do you have any idea?

And than i just hardcoded the shipping. But... nothing was pushed to the data layer.
Does somebody else have the code for the success page that's working on 3.x?

Thanks in advance!
That is one old tutorial I used to make. Nowadays, use events. It's way faster and efficient. See the wiki: https://github.com/opencart/opencart/wiki/Events-System

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 23 guests