Opencart get product downloads - php

I want to add a new tab into the products page allowing logged in customers to directly download from product page.
At first this seems simple, but it seems that opencart is missing the download_id from order_download table so maybe I am doing this all wrong.
UPDATE: Been doing some digging into the admin and find that opencart is actually using the filename instead of download_id so guess I will have to use that. E.g:
$this->db->query("UPDATE " . DB_PREFIX . "order_download SET `filename` = '" . $this->db->escape($data['filename']) . "', mask = '" . $this->db->escape($data['mask']) . "', remaining = '" . (int)$data['remaining'] . "' WHERE `filename` = '" . $this->db->escape($download_info['filename']) . "'");
The updated method below so am I doing this correct? Seems a lot of queries so not sure if this can be improved?
public function getProductDownloads($product_id) {
$data = array();
$result = $this->db->query("SELECT download_id FROM " . DB_PREFIX . "product_to_download WHERE product_id = '" . (int)$product_id . "'");
foreach ($result->rows as $product_download) {
$product_download_query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "download d LEFT JOIN " . DB_PREFIX . "download_description dd ON (d.download_id = dd.download_id) WHERE d.download_id = '" . (int)$product_download['download_id'] . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
foreach ($product_download_query->rows as $download) {
if (!file_exists(DIR_DOWNLOAD . $download['filename'])) continue;
$row = array(
'download_id' => $download['download_id'],
'date_added' => date($this->language->get('date_format_short'), strtotime($download['date_added'])),
'name' => $download['name'],
'href' => ''
);
if($this->customer->isLogged()){
// 1. Get customer orders
$order_download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId(). "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_complete_status_id') . "' AND (od.remaining = -1 OR od.remaining > 0) AND od.filename = '" . $this->db->escape($download['filename']) . "'");
foreach($order_download_query->rows as $order_download) {
// 2. Check customer ordered product
$order_product_download_query = $this->db->query("SELECT 1 FROM " . DB_PREFIX . "order_product op WHERE op.order_product_id = '" . (int)$order_download['order_product_id']. "' AND op.product_id = '" . (int)$product_id . "'");
if($order_product_download_query->row) {
$row['href'] = $this->url->link('account/download/download', 'order_download_id=' . $order_download['order_download_id'], 'SSL');
$row['remaining'] = $order_download['remaining'];
break;
}
}
}
$data[] = $row;
}
}
return $data;
}

First You don't have to run update query, after Placing order it adds data
see /catalog/model/checkout/order.php::addOrder()
To download files
Product Must have download File
Order Status should be Complete
Download remaining should be more than 0
see /catalog/model/account/download.php::getDownloads()
Solution
add following code after if ($product_info) { around line 170 of /catalog/controller/product/product.php
if ($this->customer->isLogged()) {
$this->load->model('account/download');
$results = $this->model_account_download->getDownloadByProduct($product_id);
foreach ($results as $result) {
if (file_exists(DIR_DOWNLOAD . $result['filename'])) {
$size = filesize(DIR_DOWNLOAD . $result['filename']);
$i = 0;
$suffix = array(
'B',
'KB',
'MB',
'GB',
'TB',
'PB',
'EB',
'ZB',
'YB'
);
while (($size / 1024) > 1) {
$size = $size / 1024;
$i++;
}
$this->data['downloads'][] = array(
'order_id' => $result['order_id'],
'name' => $result['name'],
'remaining' => $result['remaining'],
'size' => round(substr($size, 0, strpos($size, '.') + 4), 2) . $suffix[$i],
'href' => $this->url->link('account/download/download', 'order_download_id=' . $result['order_download_id'], 'SSL')
);
}
}
}
Default download method fetches all downloads by customer but we need downloads on product by customer
so /catalog/model/account/download.php add new function. This will fetch associated downloads of product if customer have remaining download of that product.
public function getDownloadByProduct($product_id) {
$query = $this->db->query("SELECT o.order_id, o.date_added, od.order_download_id, od.name, od.filename, od.remaining FROM " . DB_PREFIX . "order_download od LEFT JOIN `" . DB_PREFIX . "order` o ON (od.order_id = o.order_id) LEFT JOIN `" . DB_PREFIX . "order_product` op ON (o.order_id = op.order_id) WHERE o.customer_id = '" . (int)$this->customer->getId() . "' AND o.order_status_id > '0' AND o.order_status_id = '" . (int)$this->config->get('config_complete_status_id') . "' AND od.remaining > 0 AND op.product_id = ' " . $product_id . " ' ORDER BY o.date_added DESC");
return $query->rows;
}
now in product.tpl file you can echo download
<?php
if(isset($downloads)){
foreach ($downloads as $download) { ?>
<?php echo $download['name'].'('.$download['remaining'].')' ?>
<?php }
}
?>
Hope This helps

Related

edit queries are writing over all records

Im having some issues will my queries in my model. I have this edit function and inside that is a foreach that controls what gets updated and inserted where. the problem Im having is the last set of else statements that write to the communication table.
The records being written are tied to an overall campaign id. that id is stored to each record. So each record may have its own communication_id but they all would have the same campaign_id.
So currently setting the WHERE to campaign_id edits all records. I need to use the communication_id in this instance but how do I get it before the communication queries? Say a record has communication_id 50 I want to get that id and then use that in the WHERE. Im not sure how to do that though.
public function editCampaign($campaign_id, $data) {
$this->db->query("UPDATE " . DB_PREFIX . "campaigns SET campaign_name = '" . $this->db->escape($data['campaign_name']) . "', campaign_giving_goal = '" . (float)$data['campaign_giving_goal']
. "', code = '" . $this->db->escape($data['code']) . "', campaign_active = '" . $this->db->escape($data['campaign_active']) . "', campaign_giving_count_goal = '" . (float)$data['campaign_giving_count_goal'] . "', campaign_owner = '" . $this->db->escape($data['campaign_owner']). "'
, date_beginning = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "' WHERE campaign_id = '" . (int)$campaign_id . "'");
$parent_id = 0;
$this->db->query("DELETE FROM " . DB_PREFIX . "campaign_components WHERE campaign_id = '" . (int)$campaign_id . "'");
//$this->db->query("DELETE FROM " . DB_PREFIX . "communication WHERE campaign_id = '" . (int)$campaign_id . "'");
foreach($data['component_module'] as $component_data) {
if ($component_data['component_type'] =='EVENT'){
if(isset($component_data['component_parent_id'])){
$parent_id = $component_data['component_parent_id'];
$this->db->query("UPDATE " . DB_PREFIX . "product SET model = '" . $this->db->escape($component_data['component_type']) . "', date_starting = '" . $this->db->escape($component_data['component_start_date']). "', date_ending = '" . $this->db->escape($data['date_ending']). "', date_added = NOW() WHERE product_id = '" . (int)$parent_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($component_data['component_name']) . "', language_id = '1' WHERE product_id ='" . (int)$parent_id . "'");
}else{
$this->db->query("INSERT INTO " . DB_PREFIX . "product SET model = '" . $this->db->escape($component_data['component_type']) . "', date_starting = '" . $this->db->escape($data['date_beginning']). "', date_ending = '" . $this->db->escape($data['date_ending']). "', date_added = NOW()");
$parent_id = $this->db->getLastId();
$this->db->query("INSERT INTO " . DB_PREFIX . "product_description SET name = '" . $this->db->escape($component_data['component_name']) . "', language_id = '1', product_id ='" . (int)$parent_id . "'");
$this->db->query("INSERT INTO " . DB_PREFIX . "product_to_category SET category_id = '82', product_id ='" . (int)$parent_id . "' ");
}
}else{
$this->db->query("UPDATE " . DB_PREFIX . "communication SET subject = '" . $this->db->escape($component_data['component_name']) . "', channel = '" . $this->db->escape($component_data['component_type']) . "', status = '" . $this->db->escape($component_data['component_status']) . "'
, status_date = '" . $this->db->escape($component_data['component_start_date']). "', status = '" . $this->db->escape($component_data['component_status']) . "', created_by = '" . $this->db->escape($component_data['component_owner']) . "', date_added = NOW(), campaign_id = '" . (int)$campaign_id . "'");
}
$this->db->query("INSERT INTO " . DB_PREFIX . "campaign_components SET component_name = '" . $this->db->escape($component_data['component_name']) . "', component_type = '" . $this->db->escape($component_data['component_type']) . "', component_status = '" . $this->db->escape($component_data['component_status']) . "'
, component_owner = '" . $this->db->escape($component_data['component_owner']). "', component_start_date = '" . $this->db->escape($component_data['component_start_date']). "', campaign_id = '" . (int)$campaign_id . "', parent_id = '" . (int)$parent_id . "'");
}
$this->cache->delete('parent_id');
return $campaign_id;
}
Join with the communication table:
UPDATE campaigns AS ca
JOIN communication AS co ON ca.communication_id = co.communication_id
SET ca.col1 = val1, ca.col2 = val2, ...
WHERE co.campaign_id = $campaign_id
(I've left out all the PHP variables so you can see the general structure of the query.)

Opencart 2.2.0 enable search by manufacturer

I am using OC 2.2.0 and been struggling with the following problem for a while now:
Example: I enter Siemens in header search and click SHOW ALL RESULTS, my search page appears with all results. The problem is - results list includes only products that have Siemens in their NAME. What I need is to show all products in search results list, belonging to that manufacturer, which in our example is Siemens manufacturer. In my search.php controller file, results are defined in this line:
$results = $this->model_catalog_product->getProducts($filter_data);
Which shows me that getProducts($filter_data) function of product.php file in model-catalog-product is where I need to define results. I tried tweaking the query in this function so that it includes manufacturer in search results too, but with no luck. So far, my getProducts($filter_data) function looks like this:
public function getProducts($data = array()) {
$sql = "SELECT p.product_id, (SELECT AVG(rating) AS total FROM " . DB_PREFIX . "review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id) AS rating, (SELECT price FROM " . DB_PREFIX . "product_discount pd2 WHERE pd2.product_id = p.product_id AND pd2.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND pd2.quantity = '1' AND ((pd2.date_start = '0000-00-00' OR pd2.date_start < NOW()) AND (pd2.date_end = '0000-00-00' OR pd2.date_end > NOW())) ORDER BY pd2.priority ASC, pd2.price ASC LIMIT 1) AS discount, (SELECT price FROM " . DB_PREFIX . "product_special ps WHERE ps.product_id = p.product_id AND ps.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND ((ps.date_start = '0000-00-00' OR ps.date_start < NOW()) AND (ps.date_end = '0000-00-00' OR ps.date_end > NOW())) ORDER BY ps.priority ASC, ps.price ASC LIMIT 1) AS special";
if (!empty($data['filter_category_id'])) {
if (!empty($data['filter_sub_category'])) {
if(!empty($data['filter_sub_subcategory'])) {
$sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
} else {
$sql .= " FROM " . DB_PREFIX . "product_to_category p2c";
}
//$sql .= " FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (cp.category_id = p2c.category_id)";
} else {
$sql .= " FROM " . DB_PREFIX . "product_to_category p2c LEFT JOIN " . DB_PREFIX . "category cc ON (p2c.category_id = cc.category_id)";
}
if (!empty($data['filter_filter'])) {
$sql .= " LEFT JOIN " . DB_PREFIX . "product_filter pf ON (p2c.product_id = pf.product_id) LEFT JOIN " . DB_PREFIX . "product p ON (pf.product_id = p.product_id)";
} else {
$sql .= " LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)";
}
} else {
$sql .= " FROM " . DB_PREFIX . "product p";
}
$sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
if (!empty($data['filter_category_id'])) {
if (!empty($data['filter_sub_category'])) {
if(!empty($data['filter_sub_subcategory'])) {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_sub_subcategory'] . "'";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_sub_category'] . "'";
}
//$sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
} else {
$sql .= " AND cc.parent_id = '" . (int)$data['filter_category_id'] . "'";
}
if (!empty($data['filter_filter'])) {
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id) {
$implode[] = (int)$filter_id;
}
$sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
}
}
if (!empty($data['filter_subcategory_id'])) {
if (!empty($data['filter_sub_category'])) {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_sub_category'] . "'";
//$sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
}
if (!empty($data['filter_filter'])) {
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id) {
$implode[] = (int)$filter_id;
}
$sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
}
}
if (!empty($data['filter_sub_subcategory'])) {
if (!empty($data['filter_sub_subcategory'])) {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_sub_subcategory'] . "'";
//$sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
}
if (!empty($data['filter_filter'])) {
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id) {
$implode[] = (int)$filter_id;
}
$sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
}
}
if (!empty($data['filter_name']) || !empty($data['filter_tag'])) {
$sql .= " AND (";
if (!empty($data['filter_name'])) {
$implode = array();
$words = explode(' ', trim(preg_replace('/\s+/', ' ', $data['filter_name'])));
foreach ($words as $word) {
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
}
if ($implode) {
$sql .= " " . implode(" AND ", $implode) . "";
}
if (!empty($data['filter_description'])) {
$sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
}
}
if (!empty($data['filter_name']) && !empty($data['filter_tag'])) {
$sql .= " OR ";
}
if (!empty($data['filter_tag'])) {
$sql .= "pd.tag LIKE '%" . $this->db->escape($data['filter_tag']) . "%'";
}
if (!empty($data['filter_name'])) {
$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.wholesale) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
$sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
}
$sql .= ")";
}
if (!empty($data['filter_manufacturer_id'])) {
$sql .= " AND p.manufacturer_id = '".(int)$data['filter_manufacturer_id']."'";
}
$sql .= " GROUP BY p.product_id";
$sort_data = array(
'pd.name',
'p.model',
'p.quantity',
'p.price',
'rating',
'p.sort_order',
'p.date_added'
);
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ")";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
if (isset($data['order']) && ($data['order'] == 'DESC')) {
$sql .= " DESC, LCASE(pd.name) DESC";
} else {
$sql .= " ASC, LCASE(pd.name) ASC";
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$product_data = array();
$query = $this->db->query($sql);
foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}
return $product_data;
}
Can anyone help tweak the query so that it can show all products belonging to a searched manufacturer?
Thank you in advance.
So, finally i realized what was the missing query. Before the line
$sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON
(p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX .
"product_to_store p2s
I had to put $sql .= " LEFT JOIN " . DB_PREFIX . "manufacturer m ON (m.manufacturer_id = p.manufacturer_id) ";
And then just before the line
$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
I had to put
$sql .= " OR LCASE(m.name) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
So obviously I was missing the manufacturer data. This way I pulled the data from manufacturer table and processed it correctly. I hope this helps someone, cheers!

Opencart: Error No:1062 Duplicate entry '1' for key 'PRIMARY'

I'm developing a carousel extension for opencart that has Item ID, Item Name, Link, Image, Sort Order. The carousel had an issue while saving it will change the item id so I had to modify the code to get fixed ID queryed to the DB, however now I get the 1062 Error.
Notice: Error: Duplicate entry '1' for key 'PRIMARY' Error No: 1062
INSERT INTO crousal SET crousal_id = '1', name = 'Baby & Toys', link =
'/index.php?route=product/product&product_id=7570', image =
'data/carousel/banner2.jpg', sort_order = '0' in
/home/user/public_html/system/database/mysql.php on line 49
Sharing the model/carousel.php editCarousel Function
public function editCrousal($crousal_image) {
$crousal_id = $this->db->getLastId();
$this->db->query("DELETE FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_id . "'");
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', name = '" . $this->db->escape($crousal_image['name']) . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
}
}
}
Any advice why I'm getting this error and how to resolve, thank you in advanced.
P.S.: Opencart 1.5.x
Edited: DB Structure & Module info
$this->db->query("
CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "mobiapp_crousal` (
`crousal_id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`link` VARCHAR(255) NOT NULL,
`image` VARCHAR(255) NOT NULL,
`sort_order` INT(3) NOT NULL,
PRIMARY KEY (`crousal_id`)
) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
Model File
public function addCrousal($crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET name = '" . $this->db->escape($crousal_image['name']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "'");
$crousal_id = $this->db->getLastId();
}
}
}
public function editCrousal($crousal_image) {
$crousal_id = $this->db->getLastId();
$this->db->query("DELETE FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_id . "'");
if (isset($crousal_image['crousal_image'])) {
foreach ($crousal_image['crousal_image'] as $crousal_image) {
$this->db->query("INSERT INTO " . DB_PREFIX . "mobiapp_crousal SET crousal_id = '" . (int)$crousal_id . "', name = '" . $this->db->escape($crousal_image['name']) . "', link = '" . $this->db->escape($crousal_image['link']) . "', image = '" . $this->db->escape($crousal_image['image']) . "', sort_order = '" . (int)$crousal_image['sort_order'] . "'");
$crousal_id = $this->db->getLastId();
}
}
}
public function getCrousalImages() {
$crousal_image_data = array();
$crousal_image_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal ORDER BY sort_order ASC");
foreach ($crousal_image_query->rows as $crousal_image) {
$crousal_image_description_data = array();
$crousal_image_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "mobiapp_crousal WHERE crousal_id = '" . (int)$crousal_image['crousal_id'] . "'");
$crousal_image_data[] = array(
'crousal_image' => $crousal_image,
'link' => $crousal_image['link'],
'name' => $crousal_image['name'],
'crousal_id' => $crousal_image['crousal_id'],
'image' => $crousal_image['image'],
'sort_order' => $crousal_image['sort_order']
);
}
return $crousal_image_data;
}
I did review for this module. Seems like this module has totally wrong structure. As you can see in method addCrousal foreach-statement with insertion to database. But in this insert carousal_id not incrementing so in the table will be records with not unique carousal_id. So this column can't be PRIMARY.
I suggest you to rewrite module with two tables "Slides" with relations (has many) "Images". Or you can remove PRIMARY index and operation will pass but it's not true solution.

How to fix the following error in php?

I have a two piece of code for Regions and Categories. They are exactly the same. The code for Category works, but "Region" returns the following error:
Fatal error: Call to a member function getRegion() on null in C:\wamp64\www\site\catalog\controller\module\latest.php on line 81
// Region
$product_region = array();
$regions = $this->model_profile_profile->getProductRegions($result['product_id']);
foreach ($regions as $region_id) {
print_r($region_id); // !!! ($region_id = 2) !!! $region_id is not empty !!!
$region_info = $this->model_account_region->getRegion($region_id); // LINE 81
if ($region_info) {
$product_region[] = array(
'name' => ($region_info['path']) ? $region_info['path'] . ' > ' . $region_info['name'] : $category_info['name'],
'href' => $this->url->link('account/profile', '&path=' . $region_info['region_id'])
);
}
}
And here is the code which probably causes the error:
public function getRegion($region_id) {
$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");
return $query->row;
}
I'd appreciate if you help me by giving a piece of code, because I'm dummy in PHP.
$this->model_account_region //This property is null
Check
echo 'FILE : '.__FILE__ .'<br/>';
echo 'LINE : '.__LINE__ .'<br/>';
echo '<pre>';
var_dump( empty( $this->model_account_region ) );
echo '</pre>';
exit;
model_account_region model class is messed up. Since you are running out of time. I suggest you copy the function
public function getRegion($region_id) {
$query = $this->db->query("SELECT DISTINCT *, (SELECT GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR ' > ') FROM " . DB_PREFIX . "region_path cp LEFT JOIN " . DB_PREFIX . "region_description cd1 ON (cp.path_id = cd1.region_id AND cp.region_id != cp.path_id) WHERE cp.region_id = c.region_id AND cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' GROUP BY cp.region_id) AS path, (SELECT DISTINCT keyword FROM " . DB_PREFIX . "url_alias WHERE query = 'region_id=" . (int)$region_id . "') AS keyword FROM " . DB_PREFIX . "region c LEFT JOIN " . DB_PREFIX . "region_description cd2 ON (c.region_id = cd2.region_id) WHERE c.region_id = '" . (int)$region_id . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'");
return $query->row;
}
into model_profile_profile then call it from there ie
$region_info = $this->model_profile_profile->getRegion($region_id); // LINE 81
if that fails its the db class failing to load properly so you add this line before calling your function on line 81
$this->load->database();
let me know if you are facing more challenges. I am a bit bored over here.

Opencart price calculating, custom price calculation

Im currently working on an opencart site. I have wrote custom price calculation php, depending which options are selected in order. This work fine, when options are select, radio ot checkboxes. But..
I have an option, where customer can input a number with text field. I use this number in price calculation, so depending on this value, opencart calculate the price. I send this number using SESSION to system/library/cart.php, where i use it for calculation. This works fine when customer order just once.
When customer already have a product in his cart, and want make another order, opencart will overwrite the session value, and this will recalculate price for the item already in cart.
is there any good method to store separately this session values?
Here is my modified system/library/cart.php:
if ($product_id == '65') {
$pqty = $_SESSION['pqty'];
$contents = file_get_contents("http://somepage.com/system/library/calculate.php?&qty=".$quantity."&pqty=".$pqty."&pnh=".$pnh."&czp=".$czp."&czt=".$czt."&bnd=".$bnd."&covcho=".$covcho."&covczp=".$covczp."&covczt=".$covczt."&lam=".$lam);
$contents = utf8_encode($contents);
$calcprice = json_decode($contents);
$this->data[$key] = array(
'key' => $key,
'product_id' => $product_query->row['product_id'],
'name' => $product_query->row['name'],
'model' => $product_query->row['model'],
'shipping' => $product_query->row['shipping'],
'image' => $product_query->row['image'],
'option' => $option_data,
'download' => $download_data,
'quantity' => $quantity,
'minimum' => $product_query->row['minimum'],
'subtract' => $product_query->row['subtract'],
'stock' => $stock,
'price' => $calcprice / quantity,
'total' => $calcprice,
'reward' => $reward * $quantity,
'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $quantity : 0),
'tax_class_id' => $product_query->row['tax_class_id'],
'weight' => ($product_query->row['weight'] + $option_weight) * $quantity,
'weight_class_id' => $product_query->row['weight_class_id'],
'length' => $product_query->row['length'],
'width' => $product_query->row['width'],
'height' => $product_query->row['height'],
'length_class_id' => $product_query->row['length_class_id'],
'recurring' => $recurring
);
You created if ($product_id == '65') { in your system file. Which means you have to make code for every product id which is not feasible solution.
My suggestion is that you should create a session array like $this->session->data['custom_price']['your_product_id'] = value entered by customer. And using this you should calculate price for each item.
Is the issue for you that opencart is NOT creating a separate item in your cart and instead doubling it up on the same product? I was unclear if this was your exact problem, but if it is - you can enable duplicate products to be in your cart by modifying the following in your system/library/cart.php file:
Around line 290, you can change:
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "cart WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
if (!$query->row['total']) {
$this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
} else {
$this->db->query("UPDATE " . DB_PREFIX . "cart SET quantity = (quantity + " . (int)$quantity . ") WHERE api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "' AND customer_id = '" . (int)$this->customer->getId() . "' AND session_id = '" . $this->db->escape($this->session->getId()) . "' AND product_id = '" . (int)$product_id . "' AND recurring_id = '" . (int)$recurring_id . "' AND `option` = '" . $this->db->escape(json_encode($option)) . "'");
}
$this->data = array();
}
Change to:
// Modified to ENABLE duplicate product
public function add($product_id, $quantity = 1, $option = array(), $recurring_id = 0) {
$query = $this->db->query("INSERT " . DB_PREFIX . "cart SET api_id = '" . (isset($this->session->data['api_id']) ? (int)$this->session->data['api_id'] : 0) . "', customer_id = '" . (int)$this->customer->getId() . "', session_id = '" . $this->db->escape($this->session->getId()) . "', product_id = '" . (int)$product_id . "', recurring_id = '" . (int)$recurring_id . "', `option` = '" . $this->db->escape(json_encode($option)) . "', quantity = '" . (int)$quantity . "', date_added = NOW()");
$this->data = array();
}
// End
I use this modification because I have my own custom pricing, too, and it interfers with OpenCart's original code when it was trying to multiply the product in cart. As you can tell from the code, you're removing OC's query of product total and preventing it from over-riding / increasing the QTY of the same product when you add another instance of it into your cart. Instead, a separate instance/item will be added instead. It won't tamper with the functionality of a customer adding more than 1 QTY from the product page or increasing it in their view cart page, so that all stays the same still in case that's still needed. This just affects how products of the same product id are added into the cart.

Categories