0

Is it possible to set the order status for purchases of virtual products to 'completed' when the payments is made (so no manual invoicing is needed)? If so, how?

  • which magento version you are using ? – Hassan Ali Shahzad Oct 16 '18 at 10:08
  • You can create sales_order_place_after observer and check your product type if product type virtual then from their you can update the order status. – Himanshu Oct 16 '18 at 10:40
  • @HassanAliShahzad the tag says magento-1, so it's Magento 1 :) – jscar Oct 16 '18 at 11:31
0

For M1: You need to capture event sales_order_save_after because it triggers after order save in DB & you need it in this way.

create directory structure as followings image:-

enter image description here

config.xml

    <?xml version="1.0"?>
<config>
  <modules>
    <LevoSoft_OrderCompletedForVirtualProduct>
      <version>0.1.0</version>
    </LevoSoft_OrderCompletedForVirtualProduct>
  </modules>
  <global>
    <models>
      <ordercompletedforvirtualproduct>
        <class>LevoSoft_OrderCompletedForVirtualProduct_Model</class>
        <resourceModel>ordercompletedforvirtualproduct_mysql4</resourceModel>
      </ordercompletedforvirtualproduct>
    </models>
    <events>
      <sales_order_save_after>
        <observers>
          <sales_order_save_after_handler>
            <type>model</type>
            <class>ordercompletedforvirtualproduct/observer</class>
            <method>updateVirtualProductOrderStatus</method>
            <args></args>
          </sales_order_save_after_handler>
        </observers>
      </sales_order_save_after>
    </events>
  </global>
</config> 

Observer.php

<?php
class LevoSoft_OrderCompletedForVirtualProduct_Model_Observer
{

            public function updateVirtualProductOrderStatus(Varien_Event_Observer $observer)
            {
                if ($observer->getOrder()){
                $order = $observer->getOrder();
                $orderedItems = $order->getAllVisibleItems();
                $orderedProductIds = [];

              foreach ($orderedItems as $item) {
              $orderedProductIds[] = $item->getData('product_id');
              }
              // Here you can place your logic load product and check its type if its virtual in variable $virtual then mark order complete like followings
              if($virtual){
              $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true);
              $order->setStatus('completed');// whatever status you have in M1
              $order->save();
               }
            }

}

LevoSoft_OrderCompletedForVirtualProduct.xml

<?xml version="1.0"?>
<config>
  <modules>
    <LevoSoft_OrderCompletedForVirtualProduct>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </LevoSoft_OrderCompletedForVirtualProduct>
  </modules>
</config>

For M2: In Legacy way you have to capture the event sales_order_save_after and perform your custom work their. I will use new way here introduce in M2 that is Plugin. You need to capture sales_order_save_after:palce method in around plugin and perform your custom work here. You need to create directories like following:

enter image description here

and create files and directories as followings:

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'LevoSoft_OrderConpletedForVirtualProduct',
    __DIR__
);

etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="LevoSoft_OrderConpletedForVirtualProduct" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

etc/di.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\OrderManagementInterface">
        <plugin disabled="false" name="LevoSoft_OrderConpletedForVirtualProduct_Plugin_Magento_Sales_Api_OrderManagementInterface" sortOrder="10" type="LevoSoft\OrderConpletedForVirtualProduct\Plugin\Magento\Sales\Api\OrderManagementInterface"/>
    </type>
</config>

Plugin/Magento/Sales/Api/OrderManagementInterface.php

    <?php


    namespace LevoSoft\OrderConpletedForVirtualProduct\Plugin\Magento\Sales\Api;

    class OrderManagementInterface
    {

        public function aroundPlace(
            \Magento\Sales\Api\OrderManagementInterface $subject,
            \Closure $proceed,
            \Magento\Sales\Api\Data\OrderInterface $order
        ) {
            $return = $proceed($order);
            $orderId = $order->getEntityId();
            // your custom code here to fetch product from order and check its type if virtual mark order status complete


           $order->setState(Order::STATE_COMPLETED)->setStatus(Order::STATE_COMPLETED);
           $order->save();
            return $return;
        }
    }
  • This question is related to m1 please check tag and read question, you can not post unrelated answer. – Himanshu Oct 16 '18 at 10:42
  • He did not answer my question about version. How you can find He is asking about M1 ? while you had edited his question and add M1 in it. – Hassan Ali Shahzad Oct 16 '18 at 11:10
  • I clearly put magento1 as a tag when I posted the question. Not sure why you edited it :/ – jscar Oct 16 '18 at 11:28
  • updated my Answer let me know if you still facing issue in it. – Hassan Ali Shahzad Oct 16 '18 at 11:57
  • @jscar Still have an issue ? – Hassan Ali Shahzad Oct 17 '18 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.