• Rishi Mehta

    (@rcreators)


    Hello,

    The plugin works great and i am able to get everything automated but i found there are lot of questions regarding it. So here are some steps you guys can follow to do everything automatically. This will only work for online payment like credit card, PayPal or any other payment gateway.

    1) Make sure in the license delivery setting, set delivery to processing and completed. So when an order is generated and gets successful payment, it assigns a license key to the order.

    2) Add below code to your child theme functions.php file.

    add_action( 'woocommerce_payment_complete', 'rc_set_completed_for_paid_orders' );
    
    function rc_set_completed_for_paid_orders( $order_id ) {
    
        $order = wc_get_order( $order_id );
        $order->update_status( 'completed' );
        
    }
    
    add_action( 'woocommerce_order_status_completed', 'rc_woocommerce_order_status_completed', 10, 1);
    
    function rc_woocommerce_order_status_completed( $order_id ) {
       // Send invoice email to customer
       //use order ID as trigger value
       $order = wc_get_order( $order_id );
       WC()->mailer()->emails['LMFWC_Customer_Deliver_License_Keys']->trigger($order_id, $order);
    }

    So the first function will automatically make the order complete when there is successful payment. The second function will trigger license delivery email to the customer when the order status changes to complete.

    Now In the first step, if you don’t select the license delivery process, then the license delivery key email will not have the key. So make sure in license delivery, select process status as well.

    Happy Coding.
    Rishi Mehta

Viewing 8 replies - 1 through 8 (of 8 total)
  • sanji17

    (@sanji17)

    The second step (php coding), is it optional or compulsory step?

    Thread Starter Rishi Mehta

    (@rcreators)

    Its compulsory. I am still updating code for license key stock. So i will update the code once stock verification is included.

    Thread Starter Rishi Mehta

    (@rcreators)

    Updated Code for Stock checking and Low stock notification for license key,

    I have updated this for my specific need like i only have variable products. So i am using variation id in the $license_key function. You have to update some code for your need.

    //Mark Paid Order as Completed if Licese key stock available
    add_action( 'woocommerce_payment_complete', 'rc_set_completed_for_paid_orders' );
    
    use LicenseManagerForWooCommerce\Repositories\Resources\License as LicenseResourceRepository;
    use LicenseManagerForWooCommerce\Enums\LicenseStatus;
    
    function rc_set_completed_for_paid_orders( $order_id ) {
    
        $order = wc_get_order( $order_id );
        $items = $order->get_items();
        
        $is_key = true;
        
        foreach ( $items as $item ) {
            $product   = $item->get_product();
            //$product_name = $item->get_name();
            $product_id = $item->get_product_id();
            $product_variation_id = $item->get_variation_id();
            
            $neededAmount = absint($item->get_quantity());
            
            //Check available license code for variation
            $licenseKeys = LicenseResourceRepository::instance()->findAllBy(
                array(
                    'product_id' => $product_variation_id,
                    'status' => LicenseStatus::ACTIVE
                )
            );
            
            $availableStock = count($licenseKeys);
            
            if($availableStock < $neededAmount) $is_key = false;
            
            //Send Low Stock Email for Product when No of license key available is less than 10
            if($availableStock < 10) {
                do_action( 'woocommerce_low_stock', $product );
            }
            
        }
        
        if($is_key == true) {
            $order->update_status( 'completed' );
        }
        
    }
    
    //Send License Key Email to User when order status changed to complete
    add_action( 'woocommerce_order_status_completed', 'rc_woocommerce_order_status_completed', 10, 1);
    
    function rc_woocommerce_order_status_completed( $order_id ) {
       // Send invoice email to customer
       //use order ID as trigger value
       $order = wc_get_order( $order_id );
       WC()->mailer()->emails['LMFWC_Customer_Deliver_License_Keys']->trigger($order_id, $order);
    }
    Anonymous User 21140128

    (@anonymized-21140128)

    Isn’t this something you can just solve with the autocomplete orders plugin?

    Thread Starter Rishi Mehta

    (@rcreators)

    Nope. Auto complete order plugin will not check if license key is available for that product or not. So that will create a issue where your customer orders are completed but they didn’t received license key at all.

    Anonymous User 21140128

    (@anonymized-21140128)

    ah sure! my use case is slightly different, I think ??

    daneshh

    (@daneshh)

    Good job.

    I was using plugin WunderAutomation for auto completing the orders but now I got rid of that plugin because of no support and updates and start using your code with product_id variable.
    Thank you

    Plugin Support Mirza Hamza

    (@hamza1010)

    Hello @anonymized-21140128,

    Can you please share the use case so we can help you further.

    Thank you

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Tip: To process everything automatically’ is closed to new replies.