AwoCoupon Customer Balance module - "auto-click" the "Apply" button at checkout


  • Default avatar
    micha    
     8 months ago
    0

    Hi, I'm building a new platform - using the AwoCoupon Customer Balance mod - with the payments via gift certificate only (client request). Since there will be only one payment method availible (gift certificate) I would like to skip the step which is hitting the "Apply" button (next to the "Gift Certificate Balance") at the checkout. In other words, when the shopping is done and user is proceeding to the cart I would like not to bother him clicking the "Apply" button (next to the "Gift Certificate Balance") since the only payment method available for him will be the gift certificate anyway. I would like to automate that step for him. Can you help me with that? :)
  • Your avatar
    seyi    
     8 months ago
    0

    Hello,

    This is the function that gets called when the apply button gets clicked:

    www/components/com_awocoupon/controller.php
    function apply_voucher_balance()

    So you could write a system plugin to call that when the cart loads.
  • Default avatar
    micha    
     8 months ago
    0

    Thanks! I will try to figure this out :) That being said since I'm not familiar with writing my own plugins I would of course appreciate any additional help you can give me to get this work. I don't need anything fancy it just needs to work. :)
  • Default avatar
    micha    
     8 months ago
    0

    Ok, so I've got the plugin code but I can't figure out how to call apply_voucher_balance()function properly. Could you help me with that?
    Regards

    <?php

    defined
    ('_JEXEC') or die;

    class 
    PlgSystemAwoAutoApplyBalance 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')
            {
            
                    
    //calling apply_voucher_balance() here ;)
        
            
    }
        }
    }
    ?>

  • Your avatar
    seyi    
     8 months ago
    0

    I would try something like this:
    <?php
    if(!class_exists'AwoCouponSiteController' )) require JPATH_ROOT.'/components/com_awocoupon/controller.php';
    $controller JController::getInstance('AwoCouponSite', array('base_path'=>JPATH_ROOT.'/components/com_awocoupon') );
    $controller->apply_voucher_balance();
    ?>
  • Default avatar
    micha    
     8 months ago
    0

    Thanks for that. :) So now the plugin works and voucher is being applied properly - I see that in the mini cart - but I can't get to the cart. It keeps redirecting me - when I click on it. :/
  • Your avatar
    seyi    
     8 months ago
    0

    the function does a redirect, but you can set the request variable 'return' with base64 encoded url to return to:

    base64_encode(JUri::getInstance()->toString());
  • Default avatar
    micha    
     8 months ago
    0

    hmm so I've changed the $return = base64_decode($return); to $return = base64_encode(JUri::getInstance()->toString()); but it keeps redirecting me
  • Your avatar
    seyi    
     8 months ago
    0

    $return is a post variable, so not that variable directly. There are a few ways to do this

    Easy php way
    $_POST['return'] =...

    Joomla 2.5 way:
    JRequest::setVar('return',....

    Dont remember the Joomla 3 way with JInput
  • Default avatar
    micha    
     8 months ago
    0

    I have Jommla 2.5 but well i guess the easy way will be the most appropriate for me. ;) So i tried a couple of variants placing the
    $_POST['return'] = base64_encode(JUri::getInstance()->toString()); in my plugin file and in the controller.php as well but I guess I'm doing it wrong because it's still not working. So would you mind telling me where exactly should I place it? :)
  • Your avatar
    seyi    
     8 months ago
    0

    Hello,

    right before calling $controller->apply_voucher_balance()
  • Default avatar
    micha    
     8 months ago
    0

    hmm so I guess the easy way it's not working - still redirecting :/
  • Your avatar
    seyi    
     8 months ago
    0

    Hi,

    This is directly out of the module which you can review:
    www/modules/mod_awocoupon_balance/mod_awocoupon_balance.php

    <?php
    $router 
    JSite::getRouter();
    $var $router->getVars();
    $current_url 'index.php?'.JURI::buildQuery($router->getVars());
    $current_url base64_encode($current_url);
    $_GET['return'] = $current_url;
    if(!
    class_exists'AwoCouponSiteController' )) require JPATH_ROOT.'/components/com_awocoupon/controller.php';
    $controller JController::getInstance('AwoCouponSite', array('base_path'=>JPATH_ROOT.'/components/com_awocoupon') );
    $controller->apply_voucher_balance();
    ?>
  • Default avatar
    micha    
     8 months ago
    0

    Hey, still redirecting.. I think we may have some kind of loop here - as long as we are in the cart we won't be able to get to the cart (if that makes any sense ;)) because the plugin won't stop process and the function will keep us redirecting. So is there a way to tell the system not to process this plugin if the balance already has been applied?
  • Your avatar
    seyi    
     8 months ago
    0

    Yes, that makes sense.

    You really should look at the module I mentioned above. I believe it has the code to stop it from applying the balance if it already has applied it.
  • Default avatar
    micha    
     8 months ago
    0

    Hmm I feel like I'm getting close to this, I've added:

     <?php 
    $session 
    JFactory::getSession();
    $coupon_session $session->get('coupon''''awocoupon');
    if(
    $coupon_session)
    {return 
    false;}
    ?> 


    just before calling apply_voucher_balance() and now it redirects me only once.