Author Topic: Sending order status to vendor only  (Read 334 times)

MecDev

  • Beginner
  • *
  • Posts: 1
  • A beginner
Sending order status to vendor only
« on: May 17, 2018, 22:53:15 pm »
Hello I just wanted to share something I found today.

We recently migrated joomla versions from 2.5 to 3.6.5. Along with that we had to setup the mail settings for the new server so some of the order status emails were not sent out properly. The vendor needed to get the notification emails for indexing/quick search purposes.

There is currently no way to only send the notifications to the vendor only. You either send the notification to the shopper and the vendor or neither. You don't get an either or option. The notification is sent out whenever the order status is changed in the virtuemart back end interface.

We didn't want to send the emails to the user as they were a couple of days old. We only needed the vendor to get the emails.

Here is how we did it.. (it's no elegant)

in /public_html/administrator/components/com_virtuemart/models/orders.php

You will find this function which handles the notifications

At the bottom you can see a commented chunk of code - that chunk of code takes care to email the user so we simply commented it out. Uploaded the modified file, sent the notifications, and restored the original file.

/**
    * Notifies the customer that the Order Status has been changed
    *
    * @author Christopher Roussel, Valérie Isaksen, Max Milbers
    *
    */
   public function notifyCustomer($virtuemart_order_id, $newOrderData = 0 ) {

      if (isset($newOrderData['customer_notified']) && $newOrderData['customer_notified']==0) {
          return true;
      }
      if(!class_exists('shopFunctionsF')) require(VMPATH_SITE.DS.'helpers'.DS.'shopfunctionsf.php');

      //Important, the data of the order update mails, payments and invoice should
      //always be in the database, so using getOrder is the right method

      $vendorModel = VmModel::getModel('vendor');
      //Lets set the language to the BE default of the main vendor
      $vendorUserId = $vendorModel->getUserIdByVendorId(1);
      $vu = JFactory::getUser($vendorUserId);
      $vLang = $vu->getParam('admin_language',VmConfig::$jDefLangTag);

      shopFunctionsF::loadOrderLanguages($vLang);
      $order = $this->getOrder($virtuemart_order_id);


      $vars['orderDetails']=$order;

      $payment_name = $shipment_name='';
      if (!class_exists('vmPSPlugin')) require(VMPATH_PLUGINLIBS . DS . 'vmpsplugin.php');

      JPluginHelper::importPlugin('vmshipment');
      JPluginHelper::importPlugin('vmpayment');
      $dispatcher = JDispatcher::getInstance();
      $returnValues = $dispatcher->trigger('plgVmOnShowOrderFEShipment',array(  $order['details']['BT']->virtuemart_order_id, $order['details']['BT']->virtuemart_shipmentmethod_id, &$shipment_name));
      $returnValues = $dispatcher->trigger('plgVmOnShowOrderFEPayment',array(  $order['details']['BT']->virtuemart_order_id, $order['details']['BT']->virtuemart_paymentmethod_id, &$payment_name));
      $order['shipmentName']=$shipment_name;
      $order['paymentName']=$payment_name;
      if($newOrderData!=0){   //We do not really need that
         $vars['newOrderData'] = (array)$newOrderData;
      }

      //$vars['includeComments'] = vRequest::getVar('customer_notified', array());
      //I think this is misleading, I think it should always ask for example $vars['newOrderData']['doVendor'] directly
      //Using this function garantue us that it is always there. If the vendor should be informed should be done by the plugins
      //We may add later something to the method, defining this better
      $vars['url'] = 'url';
      if(!isset($newOrderData['doVendor'])) $vars['doVendor'] = false; else $vars['doVendor'] = $newOrderData['doVendor'];

      $invoice = $this->createInvoiceByOrder($order);
      if($invoice){
         $vars['mediaToSend'][] = $invoice;
      }

      $virtuemart_vendor_id = $order['details']['BT']->virtuemart_vendor_id;

      $vendorEmail = array();
      $vendorEmail[] = $vars['vendorEmail'] = $vendorModel->getVendorEmail($virtuemart_vendor_id);
      $addVendorEmails = VmConfig::get('addVendorEmail','');
      if (!empty($addVendorEmails)) $vendorEmail = array_merge($vendorEmail,explode(';',$addVendorEmails));

      $vendor = $vendorModel->getVendor($virtuemart_vendor_id);
      $vars['vendor'] = $vendor;

      //Mail for vendor
      $orderstatusForVendorEmail = VmConfig::get('email_os_v',array('U','C','R','X'));
      if(!is_array($orderstatusForVendorEmail)) $orderstatusForVendorEmail = array($orderstatusForVendorEmail);

      if ( in_array((string)$order['details']['BT']->order_status,$orderstatusForVendorEmail)){
         //shopFunctionsF::loadOrderLanguages(VmConfig::$jDefLangTag);
         //VmConfig::setLanguageByTag(VmConfig::$jDefLangTag);
         $view = shopFunctionsF::prepareViewForMail('invoice', $vars);
         $res = shopFunctionsF::sendVmMail( $view, $vendorEmail, TRUE );
      }

      /*

      // Send the email
      //$res = shopFunctionsF::renderMail('invoice', $order['details']['BT']->email, $vars, null,$vars['doVendor'],$this->useDefaultEmailOrderStatus);
      $sendMail = false;
      if(!$this->useDefaultEmailOrderStatus and isset($vars['newOrderData']['customer_notified']) and $vars['newOrderData']['customer_notified']==1){
         $sendMail = true;
      } else {
         $orderstatusForShopperEmail = VmConfig::get('email_os_s',array('U','C','S','R','X'));
         if(!is_array($orderstatusForShopperEmail)) $orderstatusForShopperEmail = array($orderstatusForShopperEmail);
         if ( in_array((string) $vars['orderDetails']['details']['BT']->order_status,$orderstatusForShopperEmail) ){
            $sendMail = true;
            vmdebug('renderMail by default orderstati');
         }
      }

      shopFunctionsF::loadOrderLanguages(VmConfig::$vmlangTag);
      $res = true;
      if($sendMail){
         if(!empty($vars['orderDetails']['details']) and !empty($vars['orderDetails']['details']['BT']->order_language)) {
            $orderLang = $vars['orderDetails']['details']['BT']->order_language;
            shopFunctionsF::loadOrderLanguages($orderLang);
            $vendor = $vendorModel->getVendor($virtuemart_vendor_id);
            $vars['vendor'] = $vendor;
            $vars['orderDetails'] = $this->getOrder($virtuemart_order_id);
         }

         $shopperEmail = array();
         $shopperEmailFields = VmConfig::get('email_sf_s',array('email'));
         foreach ($shopperEmailFields as $field) {
            if (!empty($order['details']['BT']->$field)) $shopperEmail[] = $order['details']['BT']->$field;
         }
         if (count($shopperEmail) < 1) $shopperEmail[] = $order['details']['BT']->email;

         $view = shopFunctionsF::prepareViewForMail('invoice', $vars);
         $res = shopFunctionsF::sendVmMail( $view, $shopperEmail, false );

      }

      if(is_object($res) or !$res){
         $string = 'COM_VIRTUEMART_NOTIFY_CUSTOMER_ERR_SEND';
         vmdebug('notifyCustomer function shopFunctionsF::renderMail throws JException');
         $res = 0;
      } //We need this, to prevent that a false alert is thrown.
      else if ($res and $res!=-1) {
         $string = 'COM_VIRTUEMART_NOTIFY_CUSTOMER_SEND_MSG';
      }

      if($res!=-1){
         vmLanguage::setLanguageByTag(vmLanguage::$jSelLangTag);
         vmInfo( vmText::_($string,false).' '.$order['details']['BT']->first_name.' '.$order['details']['BT']->last_name. ', '.$order['details']['BT']->email);
      }

      //quicknDirty to prevent that an email is sent twice
      $app = JFactory::getApplication();
      if($app->isSite()){
         if (!class_exists('VirtueMartCart'))
            require(VMPATH_SITE . DS . 'helpers' . DS . 'cart.php');
         $cart = VirtueMartCart::getCart();
         $cart->customer_notified = true;
      }
      return true;
      */
   }


I hope that this helps someone if they're experiencing a similar issue.

Jörgen

  • Global Moderator
  • Full Member
  • *
  • Posts: 1722
    • Kreativ Fotografi
  • VirtueMart Version: 3.2.12
Re: Sending order status to vendor only
« Reply #1 on: May 22, 2018, 16:48:42 pm »
You do not mention VM versions but it i probably out of date and Joomla version is out of date. Setting e-mail for vendor and customer is standard since a long time.

regards

Jörgen @ Kreativ Fotografi
Joomla 3.8.3
Virtuemart 3.2.12
Olympiantheme Hera (customized)