plugin

  • Posts: 25
  • Thank you received: 1
  • Hikashop Business
1 year 11 months ago #347427

-- Joomla version -- : 4.latest
-- PHP version -- : 8.1

Hello,

I am writing a plugin that does the following upon successful payment:

1. Get the invoice number and email address of the customer.
2. Inspect the products that were ordered.
3. If products A, B and C were ordered (these are E-books), then add his/her email address to each page of an existing pdf file (E-book). And save the pdf.
4. Email the customer with a download link for the newly created pdf.

I am not an expert in php, but I know how to google. Step 3, I managed to program using the Setas package. Step 4 ... I still need to think about how to do this. As to steps 1 and 2.....I spent most of this day on your manual and Forums. I came up with the php code below. It seems to work ok.

My question is: Is this a valid way to obtain the invoice number and email address of the customer, and the products that were ordered? I am slightly confused why $cart = $cartClass->getFullCart(); is linked to the current order. Why can't you take the ordered products from the $order object? Would that not be better?

Alain


<?php
class plgHikashopSocialdrm extends JPlugin
{

 function plgHikashopSocialdrm(&$subject, $config){
		parent::__construct($subject, $config);
	}


      // Execute code upon order confirmation
      function onAfterOrderUpdate(&$order,&$send_email){
         
            if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }
         
            //Step 1: Get order ID, invoice number, user ID and email address
            $order_id = $order->order_id;
            $Output = 'Order id = ' . $order_id; 
            hikashop_writeToLog($Output);

            $InvNum = $order->order_invoice_number;
            $Output ="Invoice number: " .  $InvNum;
            hikashop_writeToLog($Output);
                
            $UserID = $order->order_user_id;        
            $Output ="User ID: " .  $UserID;
            hikashop_writeToLog($Output);
         
            $userClass = hikashop_get('class.user');
            $user = $userClass->get($order->order_user_id);
            $UserEmail = $user->user_email;
            $Output ="User email: " .  $UserEmail;
            hikashop_writeToLog($Output);

           
           //Step 2: Order confirmed?
           $app = JFactory::getApplication();
           $config =& hikashop_config();
		   $confirmed = $config->get('order_confirmed_status');

           //If order is confirmed show the products.
           if($order->order_status == $confirmed){
                //Load the current cart data:
                $cartClass = hikashop_get('class.cart');
                $cart = $cartClass->getFullCart();
               
               //The products in the cart will be available in the 
               //attribute as an array of product objects:
                foreach($cart->products as $product) {
                    hikashop_writeToLog($product->product_name);
                    //TO BE EXTENDED WITH switch OR if TO CHECK WHETHER PRODUCTS A, B OR C WERE SELECTED.
                }
		   }
	}
}


And this is the xml file
<?xml version="1.0" encoding="utf-8"?>
<extension version="1.0" type="plugin" method="upgrade" group="hikashop">
    <name>Hikashop Social DRM</name>
    <author>Alain Zuur</author>
    <creationDate>December 2022</creationDate>
    <version>1.0.0</version>
    <description>Social DRM</description>
    <files>
        <filename plugin="socialdrm">socialdrm.php</filename>
    </files>
</extension>

Last edit: 1 year 11 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 11 months ago #347428

Hi,

You should use the loadFullOrder method instead of the get method on class.order.
That way, you'll get all the data of the order including the user data in $order->customer and the products in $order->products

Loading the cart data there is risky as the current cart might not correspond to the current order. For example, if the "clean cart when order is" setting of the HikaShop configuration is set to "created" instead of "confirmed", the cart will be deleted before reaching your code and it won't work.

The page www.hikashop.com/support/documentation/6...umentation.html#code actually has some sample codes including some code to load the data of an order and loop through its products.

Please Log in or Create an account to join the conversation.

  • Posts: 25
  • Thank you received: 1
  • Hikashop Business
1 year 11 months ago #347443

Hello....thanks for your feedback. Greatly appreciated. I wish I had changed from Virtuemart to Hikashop much earlier!

Following your suggestions, I ended up with the code below.

1. That's better now?
2. I assume that the order_id in &$order is the same order id as from loadFullOrder?
3. And can I just double-check that onAfterOrderUpdate is only being called after payment? I copied the if($FullOrder->order_status == $confirmed){ from somewhere....but I am just guessing that it may be needed in case a payment is not successful?

Alain

<?php
class plgHikashopSocialdrm extends JPlugin
{

 function plgHikashopSocialdrm(&$subject, $config){
		parent::__construct($subject, $config);
	}

      // Execute code upon order confirmation
      function onAfterOrderUpdate(&$order,&$send_email){
         
            if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }
         
            //Step 1: Get order ID.
            $order_id = $order->order_id;

            //Step 2: Load the whole information of an order via loadFullOrder
            $orderClass = hikashop_get('class.order');
            $FullOrder = $orderClass->loadFullOrder($order_id, true, false);

            //Step 3: Get invoice number, user ID and email address
            //3A: Invoice number
            $InvNum = $FullOrder->order_invoice_number;
                
            //3B: User ID     
            $UserID = $FullOrder->order_user_id;        
            $Output ="User ID: " .  $UserID;
         
            //3C: User email
            $userClass = hikashop_get('class.user');
            $user = $userClass->get($FullOrder->order_user_id);
            $UserEmail = $user->user_email;

            //Step 4: Order confirmed?
            $app = JFactory::getApplication();
            $config =& hikashop_config();
	    $confirmed = $config->get('order_confirmed_status');

            //If order is confirmed, show the products.
            if($FullOrder->order_status == $confirmed){
                
                foreach($FullOrder->products as $product) {
 	                 hikashop_writeToLog($product->order_product_name);
 	                 hikashop_writeToLog($product->order_product_id);
 	                 $ProductID =  $product->order_product_id;
                         // Now do things with $ProductID 
                } 
    }
	}
}

Last edit: 1 year 11 months ago by Jerome. Reason: [code] tag is really nice

Please Log in or Create an account to join the conversation.

  • Posts: 82863
  • Thank you received: 13372
  • MODERATOR
1 year 11 months ago #347444

Hi,

1. That's better.

2. Yes.

3. You're raising a good point here. onAfterOrderUpdate is called each time an order is changed. So no only when an order's status changes to confirmed.
So you need to improve your check to something like that:

if(!empty($order->order_status) && $order->order_status == $confirmed && $order->old->order_status != $confirmed){
...
}
That way, you'll be sure the code inside will only run when the order's status is being changed, and only when it's changed from something which isn't confirmed to confirmed.
Also, you might want to add a check like this, especially if you plan on releasing this plugin to others or if you plan on using HikaMarket in the future:
if($order->old->order_type!='sale') return;
HikaMarket will create sub sales (one sub order for each vendor of an order) with a different order_type than "sale". And you likely want to ignore these sub sales with that extra check.

Please Log in or Create an account to join the conversation.

  • Posts: 25
  • Thank you received: 1
  • Hikashop Business
1 year 11 months ago #347462

Thank you very much. That seems to all work fine!

Alain

Please Log in or Create an account to join the conversation.

Time to create page: 0.053 seconds
Powered by Kunena Forum