News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Call a function when the cart loads

Started by 6.522, March 18, 2016, 22:34:12 PM

Previous topic - Next topic

6.522

hi :) I would like to call / trigger a function when the cart loads but.. don't know how to do that. ;) The function is part of the 3rd party extension mod (AwoCoupon Customer Balance mod) and it's located in www/components/com_awocoupon/controller.php - function apply_voucher_balance(). When the function gets called the apply button gets clicked. By default you must click it manually but I would like to automate that process. I need the simplest solution - it just needs to work. :) Can it be done by copy and paste?
Greets!

my Jommla: 2.5.28
my Virtuemart: 2.6.22

Jumbo!

Create a Joomla system plugin. Check in onAfterDispatch function. Refer - https://docs.joomla.org/Plugin/Events Then use a if clause to check if it is the cart page.

Example plugin:

<?php

defined
('_JEXEC') or die;

class 
PlgSystemCustom extends JPlugin
{
public function onAfterDispatch()
{
$app JFactory::getApplication();

if($app->isAdmin())
{
return false;
}

if(JRequest::getCmd('option') == 'com_virtuemart' && JRequest::getCmd('view') == 'cart' && JFactory::getDocument()->getType() == 'html')
{
// It is cart page after load
// Your codes goes here
}
}
}

6.522


6.522

So according to your instructions I've created the plugin but I'cant get this work.  :-\

In the .xml file I have:
<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="plugin" group="system">
        <name>awo-auto-apply-balance-on-cart</name>
        <files>
                <filename plugin="awo-auto-apply-balance-on-cart">awo-auto-apply-balance-on-cart.php</filename>
                <filename>index.html</filename>
        </files>
</extension>


and the .php contains:
<?php

defined
('_JEXEC') or die;

class 
PlgSystemCustom extends JPlugin
{
public function onAfterDispatch()
{
$app JFactory::getApplication();

if($app->isAdmin())
{
return false;
}

if(JRequest::getCmd('option') == 'com_virtuemart' && JRequest::getCmd('view') == 'cart' && JFactory::getDocument()->getType() == 'html')
{

function apply_voucher_balance() {
//JRequest::checkToken() or jexit( 'Invalid Token' );

$handlerclass 'AwoCoupon'.AWOCOUPON_ESTORE.'CouponHandler';
if (!class_exists$handlerclass )) require JPATH_ADMINISTRATOR.'/components/com_awocoupon/helpers/estore/'.AWOCOUPON_ESTORE.'/couponhandler.php';
call_user_func(array($handlerclass,'process_customer_balance'));

if ($return JRequest::getVar('return''''BASE64')) {
$return base64_decode($return);
if (!JUri::isInternal($return)) $data['return'] = '';
}

if (empty($return)) $return 'index.php?';
JFactory::getApplication()->redirect(JRoute::_($return));

}

}
}
}

Jumbo!

It seems you do not have knowledge of PHP coding or on Joomla plugin development. You have an invalid PHP codes and also a invalid Joomla plugin xml with respect to the plugin class.

Please need first correct your PHP codes and call function correctly. Then you need create a correct manifest file for the plugin. Now install it and enable it from Joomla plugin manager. Read this carefully if you want learn more - https://docs.joomla.org/J3.x:Creating_a_Plugin_for_Joomla

I would suggest you better take help from some PHP programmer.

6.522

You are right I'm not a programmer :) but I thought that, with the help of more experienced users, I could try to handle it by myself. :) Thanks for your help.

6.522

That being said :) since you're a programmer and we have already got so far maybe you could give me a few additional tips? For instance could you tell me what's wrong with my .xml code? - I don't have any errors on install. Also can I use your code (without additional motifications) or it's just an example? And finally what command shoud I use to call my function? Should it be $dispatcher = JDispatcher::getInstance();?
Regards

6.522

ok, my mistake :D "the plugin class" silly error :D thanks!

6.522

One more thing - how to change this plugin to call function not 'when in the cart' but 'when adding to the cart' (or when total in the cart is > 0). ?
Regards