Post by Lozza2 » Fri Feb 26, 2021 5:47 pm

Solution: Create a library - I found this page helpful: https://webkul.com/blog/creating-own-li ... -opencart/

I have written an extremely complex pricing function which I am using to display the price in product.php. In addition to the customer viewing these prices throughout the catalog, I also want them to pay the same price in the cart system.

I would ideally want to access this routine inside of admin too since there will be a page for admin to add orders and enter quotes, although this isn't as important.

Ideally, I don't want to create this code 3 times for obvious reasons.

Ideas and have tried and results:
idea) create code in an include file
result) doesn't work because you cannot include a file inside a class

idea) create code inside a model file
result) doesn't work because you cannot access a catalog model from cart.php

idea) create 3 versions of the code
result) don't want potential of price displayed not being the same as the price paid!

idea) create code inside a model file in catalog and create a controller with localhost IP address security and use curl to get results outside of catalog
result) would work but would only use if no other option

idea supplied in another post was to use the events system. I don't understand how that could help me.

I would be grateful if anyone has any other ideas.

Thanks in advance
Laurence
Last edited by Lozza2 on Sat Feb 27, 2021 8:23 pm, edited 2 times in total.

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by thekrotek » Fri Feb 26, 2021 5:58 pm

This is what libraries are for.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by Lozza2 » Fri Feb 26, 2021 6:15 pm

That sounds good, thank you :-)

So could I create a custom library which could access the DB and contain functions which could be accessed from all areas of the system (catalog pages, cart, admin).

Is there a document on this that I could view or basic skeleton somewhere please?

Many thanks

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by Lozza2 » Fri Feb 26, 2021 6:17 pm

Found some documentation here: https://code.tutsplus.com/tutorials/how ... -cms-25173
Just reading.... ::)

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by Lozza2 » Fri Feb 26, 2021 11:36 pm

thekrotek wrote:
Fri Feb 26, 2021 5:58 pm
This is what libraries are for.
Thanks for help - that solved my issue :)

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by Lozza2 » Sat Feb 27, 2021 8:22 pm

I have added a system library and can access it fine in catalog scripts, but get this error from cart.php:
Notice: Undefined property: Cart\Cart::$mytest ....

I have added the following code to catalog/controller/startup/startup.php
$this->registry->set('mytest', new Mytest($this->registry));

and I access using this code:
$str = $this->mytest->myfunction();

How do I make it accessable from inside the system/library/cart/cart.php

Many thanks
Laurence

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Lozza2 » Sat Feb 27, 2021 8:46 pm

JNeuhoff wrote:
Sat Feb 27, 2021 8:36 pm
I would use this:

Code: Select all

$this->load->library('mytest');
Hi, thanks for the reply.
If I put that code in cart.php I get the following error:

Notice: Undefined property: Cart\Cart::$load in /home/ezec/storage_topsoil2/modification/system/library/cart/cart.php on line 34

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by JNeuhoff » Sat Feb 27, 2021 9:33 pm

Add this to the system/library/cart/cart.php __construct:

Code: Select all

		$this->load = $registry->get('load');

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member
Online

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Lozza2 » Sat Feb 27, 2021 9:54 pm

JNeuhoff wrote:
Sat Feb 27, 2021 9:33 pm
Add this to the system/library/cart/cart.php __construct:

Code: Select all

		$this->load = $registry->get('load');
That stops the new error but back to the original one:
Notice: Undefined property: Cart\Cart::$mytest

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by straightlight » Sat Feb 27, 2021 10:25 pm

Way overcomplicated. Events can simplify all these.

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 Lozza2 » Sat Feb 27, 2021 10:49 pm

straightlight wrote:
Sat Feb 27, 2021 10:25 pm
Way overcomplicated. Events can simplify all these.
Please explain how I can use an event to facilitate my performing the same code in the catalog and the cart? I don't understand what you are telling me to do.

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by straightlight » Sat Feb 27, 2021 11:00 pm

What is your route and controller name? Also, to state the admin or catalog path in front.

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 Lozza2 » Sat Feb 27, 2021 11:09 pm

straightlight wrote:
Sat Feb 27, 2021 11:00 pm
What is your route and controller name? Also, to state the admin or catalog path in front.
It is the cart, so there is no route or controller.

Can anyone provide an example of how I can instanciate a system library file in the cart please? I am hoping someone knows!

New member

Posts

Joined
Sat Sep 12, 2020 4:13 pm

Post by straightlight » Sat Feb 27, 2021 11:11 pm

Lozza2 wrote:
Sat Feb 27, 2021 11:09 pm
straightlight wrote:
Sat Feb 27, 2021 11:00 pm
What is your route and controller name? Also, to state the admin or catalog path in front.
It is the cart, so there is no route or controller.

Can anyone provide an example of how I can instanciate a system library file in the cart please? I am hoping someone knows!
You could always create a new service request in the Commercial Support section of the forum to get this done as a custom job.

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 7 guests