0

I'm new to Magento and I've just built a custom module for a store. The module observes the event sales_order_place_before, and checks with an outside source if an item is still in stock. When the customer wants to pay for the order and a product in the cart is out of stock, I throw an error in my Observer.php model. It appears as a popup for the customer.

if(!$success){
    Mage::throwException("item not in stock");
}

Is it possible to throw this error in multiple languages, for example English and Dutch? And how does that work? I can't seem to make sense of how to use translations in an observer instead of frontend. And since this is just a custom string, it could be anything, I would not be overwriting any Magento error.

EDIT: My module's config xml (I've removed the actual namespace and module name). Do I define a helper like this? What do I put in the helper?

<config>
<modules>
    <Namespace_Modulename>
        <version>0.1.0</version>
    </Namespace_Modulename>
</modules>

<global>
    <helpers>
        <translations>
          <class>Namespace_Modulename_Helper</class>
        </translations>
    </helpers>
    <events>
        <sales_order_place_before>
            <observers>
                <Namespace_modulename_model_observer>
                    <type>singleton</type>
                    <class>Namespace_Modulename_Model_Observer</class>
                    <method>placingOrder</method>
                </Namespace_modulename_model_observer>
            </observers>
        </sales_order_place_before>
    </events>
</global>  

<frontend>
    <translate>
         <modules>
             <Namespace_Modulename>
                 <files>
                     <default>Namespace_Modulename.csv</default>
                 </files>
             </Namespace_Modulename>
         </modules>
     </translate>
</frontend>

I have created a helper with an empty class in Helper/Data.php in my module:

class Namespace_Modulename_Helper_Data extends Mage_Core_Helper_Abstract{}

I've added the csv file in app/locale/nl_NL

  • 1
    You should call your module's helper to provide the translation: Mage::throwException(Mage::helper('custom_module')->__("item not in stock")); If you would provide your custom module's config.xml in your question then we could specify this solution more. – 7ochem Nov 4 '15 at 10:14
3

Please try this but not sure

     if(!$success){
               Mage::throwException($this->__("item not in stock"));    //or
               Mage::throwException(Mage::helper('core')->__("item not in stock"));

         }
0

Try this

if(!$success){
   Mage::throwException(Mage::helper('core')->__("item not in stock"));
}

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.