I am looking to add a block of JavaScript to the success.tpl file only if a specific product is purchased.
I know I have to edit the following file to place the id check.
template/common/success.tpl
I just need help figuring out how to edit the controller to create or allow the use of the variables needed.
catalog/controller/checkout/success.php
Everyone is talking about getting the order ID but I cant find anything that talks about receiving a list of the products purchased. Does anyone know how I can manage to get the product IDs so that I can create the check against them.
Tested on Opencart 2.2.0.0 :
open:
catalog/controller/checkout/success.php
Find:
if (isset($this->session->data['order_id'])) {
Add after it:
$order_product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_product WHERE order_id = '" . (int)$this->session->data['order_id'] . "'");
$order_products_id = array();
foreach ($order_product_query->rows as $product){
$order_products_id[] = $product['product_id'];
}
$data['order_products_id'] = $order_products_id;
Then open tpl
file:
catalog/view/theme/default/template/common/success.tpl
And add this:
<?php if(isset($order_products_id)){
echo '<pre>';
var_dump($order_products_id);
echo '</pre>';
$specific_product_id = 28;
if(in_array($specific_product_id, $order_products_id)){ ?>
<script>alert('Yes, it is');</script>
<?php }
} ?>
Hope this help you.