3

I have few functions which I want to call from my config.xml, like so:

<firegento_pdf_invoice_edit_page>
            <observers>
                <firegento_pdf>
                    <class>firegento_pdf/observer</class>
                    <method>addInvoiceBarcode</method>
                </firegento_pdf>
            </observers>
            <observers>
                <firegento_pdf>
                    <class>firegento_pdf/observer</class>
                    <method>addBankDetails</method>
                </firegento_pdf>
            </observers>
            <observers>
                <firegento_pdf>
                    <class>firegento_pdf/observer</class>
                    <method>addLogo</method>
                </firegento_pdf>
            </observers>
</firegento_pdf_invoice_edit_page>

The two functions which I've created are addBankDetails and addLogo, they're both in my Observer.php file. I've placed an error log in each function, to check whether they are being called, but only addLogo is being called. I'm unsure what to do to resolve this.

EDIT: Here are the 3 functions I'm using:

public function addInvoiceBarcode(Varien_Event_Observer $observer)
{
    if (!Mage::getStoreConfigFlag('sales_pdf/invoice/order_id_as_barcode')) {
        return $this;
    }
    return $this->_addBarcode($observer);
}

public function addBankDetails(Varien_Event_Observer $observer)
{
    error_log(print_r("addBankDetails", true), 3, '/var/www/html/error.log');
    $page = $observer->getPage();
    $customer = Mage::getModel('customer/customer')->load($observer->getOrder()->getCustomerId());
    $customerGroup = Mage::getModel('customer/group')->load($customer->getGroupId())->getId();
    $allowedCustomerGroups = Mage::getStoreConfig('sales_pdf/firegento_pdf/enable_bank_details');
    $allowedCustomerGroupsArray = explode(',', $allowedCustomerGroups);

    //if the customer is one of the enabled customer groups from the config settings, add the bank details to the PDF
    if (in_array($customerGroup, $allowedCustomerGroupsArray)){
        $bankDetails = Mage::getStoreConfig('sales_pdf/firegento_pdf/bank_details');
        //seperate by newline
        $lineSeperation = 15;
        $textLineArray = explode("\n", $bankDetails);
        foreach($textLineArray as $textLine){
            $page->drawText($textLine, 32, 250 - $lineSeperation, 'UTF-8');
            $lineSeperation += 15;
        }
    }
    return $this;
}

public function addLogo(Varien_Event_Observer $observer)
{
    $page = $observer->getPage();
    $pageHeight = $page->getHeight();
    $pageWidth = $page->getWidth();
    $startWidth = $pageWidth/4;
    $imgPath = Mage::getStoreConfig('sales_pdf/firegento_pdf/factoryx_logo_image');
    $logo = "media/logo/" . $imgPath;
    $logoSize = getimagesize(Mage::getBaseUrl('media') . "logo/" . $imgPath);
    $logoWidth = $logoSize[0];
    $logoHeight = $logoSize[1];
    error_log(print_r($logoHeight, true), 3, '/var/www/html/error.log');
    $customer = Mage::getModel('customer/customer')->load($observer->getOrder()->getCustomerId());
    $customerGroup = Mage::getModel('customer/group')->load($customer->getGroupId())->getId();
    $allowedCustomerGroups = Mage::getStoreConfig('sales_pdf/firegento_pdf/factoryx_logo');
    $allowedCustomerGroupsArray = explode(',', $allowedCustomerGroups);
    //if the image has been uploaded
    if ($imgPath != "") {
        $logo = Zend_Pdf_Image::imageWithPath($logo);
        //if the customer is one of the enabled customer groups from the config settings, add the fx logo
        if (in_array($customerGroup, $allowedCustomerGroupsArray)){
            $page->drawImage($logo, $startWidth, $pageHeight-$logoHeight, $startWidth+$logoWidth, $pageHeight);
        }
    }
    return $this;
}
  • are you using a single event i.e 'firegento_pdf_invoice_edit_page' for all three methods ? – Pawan Mar 7 '18 at 3:45
  • Yes I am, is that the wrong thing to do? – Jack Perry Mar 7 '18 at 4:09
  • I think yes, see my answer with comments, let me know if it workd. – Pawan Mar 7 '18 at 4:28
3

You should use unique identifiers when defining observes

Try something like this

<firegento_pdf_invoice_edit_page>
    <observers>
        <firegento_pdf_add_invoice_bar_code>
            <class>firegento_pdf/observer</class>
            <method>addInvoiceBarcode</method>
        </firegento_pdf_add_invoice_bar_code>
    </observers>
    <observers>
        <firegento_pdf_add_bank_details>
            <class>firegento_pdf/observer</class>
            <method>addBankDetails</method>
        </firegento_pdf_add_bank_details>
    </observers>
    <observers>
        <firegento_pdf_add_logo>
            <class>firegento_pdf/observer</class>
            <method>addLogo</method>
        </firegento_pdf_add_logo>
    </observers>
</firegento_pdf_invoice_edit_page>
  • This worked for me! Thank you very much Priyank :) – Jack Perry Mar 7 '18 at 4:54
3

There is no need to add multiple tag for single event. You can call method inside main observed method.

If you are listing a single event you can call multiple method in single observer like:

config.xml

<events>
 <firegento_pdf_invoice_edit_page><!-- observe the event -->
   <observers>
    <firegento_pdf>
        <class>firegento_pdf/observer</class>
        <method>addInvoiceBarcode</method>
        </firegento_pdf>
   </observers>
 </firegento_pdf_invoice_edit_page>
</events>

Observer.php

class Firegento_Pdf_Model_Observer extends Varien_Event_Observer
    {

      public function addInvoiceBarcode($observer)
      {
          //some code;
          $this->addBankDetails($observer);

      }
      public function addBankDetails($observer)
      {
          //some code;
          $this->addLogo($observer);
      }
      public function addLogo($observer)
      {
          //some code;
          return "here";
      }
    }
  • It won't let me call addBankDetails from addInvoiceBarcode, I get the error "FireGento_Pdf_Model_Observer::addBankDetails() must be an instance of Varien_Event_Observer" – Jack Perry Mar 7 '18 at 4:37
  • I will amend my OP to include the functions I'm using – Jack Perry Mar 7 '18 at 4:48
  • I have put die on each function and it is working. – Pawan Mar 7 '18 at 4:49
  • Hi Pawan, thank you so much for your help. The answer below solved my issue though. – Jack Perry Mar 7 '18 at 4:55
  • Jack, as you are using $observer in other method, you need to send this as a parameter to with function and it will work. – Pawan Mar 7 '18 at 4:58

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.