Post by nightwing » Tue Aug 18, 2020 2:09 am

Hey Team,
I found two extensions, one created by thekrotek that allows you to create a primary category for products and another created by muglali that allows you to show product categories on product pages.
I want to be able to use the primary category (from the primary category module) as the category shown on the product pages (from the show category on product page module) and not all categories that the product is assigned to.
In the primary category module it adds the below code to "catalog/model/catalog/product.php"

Code: Select all

public function addProductPrimaryCategory($product_id = 0, $category_id)
{
if (!$product_id) {
$query = $this->db->query("SELECT product_id FROM `".DB_PREFIX."product` ORDER BY product_id DESC LIMIT 1");
if ($query->num_rows) {
$product_id = $query->row['product_id'];
}
}
$this->db->query("INSERT INTO `".DB_PREFIX."product_primary_category` (`category_id`, `product_id`) VALUES ('".(int)$category_id."', '".(int)$product_id."')");
}
public function editProductPrimaryCategory($product_id, $category_id)
{
$primary_category_id = $this->getProductPrimaryCategory($product_id);
if ($primary_category_id) {
$this->db->query("UPDATE `".DB_PREFIX."product_primary_category` SET category_id = '".(int)$category_id."' WHERE product_id = '".(int)$product_id."'");
} else {
$this->addProductPrimaryCategory($product_id, $category_id);
}
}
public function getProductPrimaryCategory($product_id)
{
$query = $this->db->query("SELECT category_id FROM `".DB_PREFIX."product_primary_category` WHERE product_id = '".(int)$product_id."'");
if ($query->num_rows) return $query->row['category_id'];
else return 0;
}
In the Show Product Category On Product Page Module it has the below in catalog/controller/product/product.php

Code: Select all

$this->load->language('common/header');
$data['text_category'] = $this->language->get('text_category');
$querycats = $this->model_catalog_product->getCategories($product_id);
$categories = array();
foreach( $querycats as $item ) {
$categ = $this->model_catalog_category->getCategory($item['category_id']);
if ($categ['parent_id'] > 0) {
$catinfo['href'] = $this->url->link('product/category', 'path=' . $categ['parent_id'] . '_' . $item['category_id']);
}
else {					
$catinfo['href'] = $this->url->link('product/category', 'path=' . $item['category_id']);
}
$catinfo['name'] = $categ['name'];
$categories[] = $catinfo;
}
In thekrotek's comment he said
"When you install this extension, three new functions are added to product model in admin and catalog:

addProductPrimaryCategory($product_id = 0, $category_id)
editProductPrimaryCategory($product_id, $category_id)
getProductPrimaryCategory($product_id)

Their names say for themselves and usage is very simple. Any developer or customer with some basic PHP knowledge can implement these functions easily. All our extensions already utilize them, when necessary.
"

I am using Opencart version 3.0.3.2 Default theme.

I am still new to PHP, and would like some help achieving this.
I have taken care of the twig and language section.
Thank you in advance for your help.
Last edited by nightwing on Sat Aug 22, 2020 1:14 am, edited 1 time in total.

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm


Post by IP_CAM » Tue Aug 18, 2020 7:10 am

Well, why didn't you ask either thekrotek or the other fellow on this in the first place? ???

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by xxvirusxx » Tue Aug 18, 2020 3:34 pm

First extension is for Admin and has nothing to do with catalog...
So, if you want to display only primary category on product page then you should make changes in second extension.

Upgrade Service | OC 2.3.0.2 PHP 8 | My Custom OC 3.0.3.8 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by nightwing » Tue Aug 18, 2020 8:03 pm

Hey xxvirusxx, I know, I printed it here so you guys could see it as it created the possibility to use the primary category.
xxvirusxx wrote:
Tue Aug 18, 2020 3:34 pm
First extension is for Admin and has nothing to do with catalog...
So, if you want to display only primary category on product page then you should make changes in second extension.

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm


Post by nightwing » Wed Aug 19, 2020 12:44 am

I did...
IP_CAM wrote:
Tue Aug 18, 2020 7:10 am
Well, why didn't you ask either thekrotek or the other fellow on this in the first place? ???

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm


Post by nightwing » Fri Aug 21, 2020 8:34 pm

Was anyone able to figure this out?
I am facing my errors when testing from Undefined property: Proxy::getProductPrimaryCategory etc
nightwing wrote:
Wed Aug 19, 2020 12:44 am
I did...
IP_CAM wrote:
Tue Aug 18, 2020 7:10 am
Well, why didn't you ask either thekrotek or the other fellow on this in the first place? ???

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm

Who is online

Users browsing this forum: Semrush [Bot] and 98 guests