Custom module issue - module

I tried creating one new module. For that i created indexController, config.xml, block, layout and other required files, even though i am unable to view the message that i wrote in my block file. Whenever the url is fired (http://mydomain.com/foobar/) the output "it is from foo bar" should be displayed, but at the moment it is not working.
My indexController.php code :
class Foo_Bar_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
}
Code related to block :
class Foo_Bar_Block_News extends Mage_Core_Block_Template
{
public function _prepareLayout()
{
return parent::_prepareLayout();
}
public function fetchRow()
{
echo "it is from foo bar";
}
}
code related to config.xml :
<?xml version="1.0"?>
<config>
<modules>
<Foo_Bar>
<version>1.0.0</version>
</Foo_Bar>
</modules>
<frontend>
<routers>
<foobar>
<use>standard</use>
<args>
<module>Foo_Bar</module>
<frontName>foobar</frontName>
</args>
</foobar>
</routers>
<layout>
<updates>
<bar>
<file>foobar.xml</file>
</bar>
</updates>
</layout>
<!--<events>
<catalog_product_load_after>
<observers>
<foo_bar>
<type>model</type>
<class>foo_bar/observer</class>
<method>catalogProductLoadAfter</method>
</foo_bar>
</observers>
</catalog_product_load_after>
</events>-->
</frontend>
<global>
<blocks>
<foobar>
<class>Foo_Bar_Block</class>
</foobar>
</blocks>
</global>
</config>
code related to layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
<default>
<reference name="content">
</reference>
</default>
<foobar_index_index>
<reference name="content">
<block type="foobar/news" name="news_test" template="bar/check.phtml"/>
</reference>
</foobar_index_index>
</layout>
Code related to template file
<?php
echo $this->fetchRow()
?>
Any help realted to this will be appreciated.

The solution to this issue in my case is to Disable the compilation from admin. Everything else is fine. The desired output is obtained after disabling the compilation from admin.
To disable compilation login to admin side of your magento project and then go to system>>Tools>>Compilation and then click on disable button to disable the compilation.

Related

Magento 2 change pdf filename

I want to change the filenames of the PDF files in Magento 2.
The default names are not clear and I want invoices to be searchable when saved to a location on my pc.
Is it possible to change the filenames of the PDF-files in Magento 2 to a format like "invoice_1000000123.pdf"?
You should never edit core files. Seriously, don't.
Since /vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php is an abstract class you have to use a plugin or preference in your module in order to override it.
What you need to achieve that:
The usual minimum files:
/Vendor/Module/composer.json, /Vendor/Module/registration.php, /Vendor/Module/etc/module.xml
In /Vendor/Module/etc/module.xml you should sequence Magento_Sales
<?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="Vendor_Module" setup_version="1.0.0">
<sequence>
<module name="Magento_Sales"/>
</sequence>
</module>
</config>
Then you can either use a plugin or a preference in /Vendor/Module/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">
<preference for="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction" type="Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction"/>
</config>
Plugin would look something like this:
<type name="Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction">
<plugin name="Vendor_Module::aroundPrintInvoice" type="Vendor\Module\Block\Class" sortOrder="0"/>
</type>
Now add a PrintAction.php to the path specified in your preference / plugin (Example for preference)
<?php
namespace Vendor\Module\Controller\Adminhtml\Invoice\AbstractInvoice;
class PrintAction extends \Magento\Sales\Controller\Adminhtml\Invoice\AbstractInvoice\PrintAction
{
/* Write code here */
}
You need override the invoice admin controller.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="\Magento\Sales\Controller\Adminhtml\Order\Invoice"
type="Vendor\Module\Controller\Adminhtml\Order\Invoice" />
</config>
In your custom module admin controller, You need to change the name of Pdf file.
public function execute()
{
$invoiceId = $this->getRequest()->getParam('invoice_id');
return $this->_fileFactory->create(
'invoice_13012020' . $invoiceId . '.pdf', //<== Change the pdf name here.
$pdf->render(),
DirectoryList::VAR_DIR,
'application/pdf'
);
...
}
Reference Link
Yes it is possible to change invoice pdf filename.
Please go to following path:
/vendor/magento/module-sales/Controller/Adminhtml/Invoice/AbstractInvoice/PrintAction.php
You can change filename from above file.

Must multiple Logback PropertyDefiner implementations be created to pull in multiple properties?

I am able to use a Logback PropertyDefiner to access a single property from a logback.xml configuration file. If I have 3 properties to access, I am currently using 3 separate implementations of PropertyDefiner (one for each property).
Is there a way to access multiple properties from a single PropertyDefiner implementation? Or perhaps there is another interface that supports multiple properties?
I want to be be able to use properties to plugin different values, based on environment (dev, ist, uat, perf, prod) for various logging configurations (context name, log levels, appender file names, file sizes, etc.).
I found this question, which is similar, but did not answer the question of how to access multiple properties.
Create a PropertyDefiner implementation class. The PropertyDefinerBase implementation is already provided.
package foo.bar;
import java.util.HashMap;
import java.util.Map;
import ch.qos.logback.core.PropertyDefinerBase;
public class LoggingPropertiesDefiner extends PropertyDefinerBase {
private static Map<String, String> properties = new HashMap<>();
static {
properties.put("encoderPattern", "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %message%n");
properties.put("maxFileSize", "50MB");
properties.put("rootLogLevel", "INFO");
}
private String propertyLookupKey;
public void setPropertyLookupKey(String propertyLookupKey) {
this.propertyLookupKey = propertyLookupKey;
}
#Override
public String getPropertyValue() {
//TODO In the real world, get properties from a properties loader.
return properties.get(propertyLookupKey);
}
}
In the logback.xml, use the same PropertyDefiner class for each property. Provide a different lookup key for each:
<define name="encoderPattern" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>encoderPattern</propertyLookupKey>
</define>
<define name="maxFileSize" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>maxFileSize</propertyLookupKey>
</define>
<define name="rootLogLevel" class="foo.bar.LoggingPropertiesDefiner">
<propertyLookupKey>rootLogLevel</propertyLookupKey>
</define>
Reference the property names in the logback.xml:
<root level="${rootLogLevel}">
<appender-ref ref="FILE"/>
</root>
You could use the property resource support from logback
logback.properties
mode=prod
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property resource="logback.properties" />
<if condition='property("mode").equals("prod")'>
<then>
<include file="logback-prod.xml" />
</then>
</if>
<if condition='property("mode").equals("dev")'>
<then>
<include resource="logback-dev.xml" />
</then>
</if>
</configuration>
logback-prod.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--Daily rolling file appender -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${MYAPP_HOME}/myApp.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>myApp.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>2</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</Pattern>
</layout>
</appender>
<root level="ERROR">
<appender-ref ref="file" />
</root>
</configuration>
logback-dev.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--Daily rolling file appender -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${MYAPP_HOME}/myApp.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>myApp.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>2</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</Pattern>
</layout>
</appender>
<root level="TRACE">
<appender-ref ref="file" />
</root>
</configuration>
More detailed explaination here.
Does that help to resolve your problem?

override core controller in custom module in Magento

HI i have to extend the core controller in my own module for this i am referencing the below link
http://inchoo.net/tools-frameworks/how-to-extend-magento-core-controller/
below is my module structure
/var/www/magento1.9/app/etc/modules
<?xml version="1.0"?>
<!--we need to enable this module as any other if-->
<!--you wish to do it as standalone module extension-->
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codepool>local</codepool>
</Inchoo_Coreextended>
</modules>
</config>
/var/www/magento1.9/app/code/local/Inchoo/Coreextended/controllers/Frontend/Customer/AccountController.php
<?php
require_once Mage::getModuleDir('controllers', 'Mage_Customer').DS.'AccountController.php';
//we need to add this one since Magento wont recognize it automatically
class Inchoo_Coreextended_Frontend_Customer_AccountController extends Mage_Customer_AccountController
{//here, you extended the core controller with our
public function indexAction()
{
parent::indexAction();
//you can always use default functionality
}
public function myactionAction()
{
//my code
//you can write your own methods / actions
}
public function mymethod()
{
//my code
//you can write your own methods
}
public function loginAction()
{
echo "hello";
//finally you can write your code that will rewrite the whole core method
//and you can call for your own methods, as you have full control over core controller
}
}
/var/www/magento1.9/app/code/local/Inchoo/Coreextended/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Inchoo_Coreextended>
<version>0.1.0</version>
</Inchoo_Coreextended>
</modules>
<frontend>
<routers>
<customer>
<args>
<modules>
<Inchoo_Coreextended before="Mage_Customer_AccountController">Inchoo_Coreextended_Frontend_Customer</Inchoo_Coreextended>
</modules>
</args>
</customer>
</routers>
</frontend>
</config>
but when i am accessing the http://localhost/magento1.9/index.php/customer/account/login/ it shows core login action and it is not switching from my module Can you please suggest where i am doing mistake .
I have fixed this by making changes in following file:
app/etc/modules/Inchoo_Coreextended.xml .
Before:
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codepool>local</codepool>
</Inchoo_Coreextended>
</modules>
</config>
After:
<config>
<modules>
<Inchoo_Coreextended>
<active>true</active>
<codePool>local</codePool>
</Inchoo_Coreextended>
</modules>
</config>
codepool should be codePool

setup installer script in magento module

Hi i am developing a little magento module.
below is my directory structure of module
app/code/local/Xyz/Total
/var/www/magext/app/code/local/Xyz/Total/Block/Prototal.php
/var/www/magext/app/code/local/Xyz/Total/controllers/IndexController.php
/var/www/magext/app/code/local/Xyz/Total/etc/config.xml
/var/www/magext/app/code/local/Xyz/Total/Model/Price/Observer.php
below is my config file code
<?xml version="1.0"?>
<config>
<global>
<blocks>
<total>
<class>Xyz_Total_Block</class>
</total>
</blocks>
<models>
<xyztotal>
<class>Xyz_Total_Model</class>
</xyztotal>
</models>
<events>
<sales_order_place_after>
<observers>
<xyz_total_price_observer>
<type>singleton</type>
<class>Xyz_Total_Model_Price_Observer</class>
<method>apply_discount_percent</method>
</xyz_total_price_observer>
</observers>
</sales_order_place_after>
</events>
</global>
<frontend>
<routers>
<total>
<use>standard</use>
<args>
<module>Xyz_Total</module>
<frontName>newcatalog</frontName>
</args>
</total>
</routers>
<layout>
<updates>
<total>
<file>total.xml</file>
</total>
</updates>
</layout>
</frontend>
</config>
now what i need to install a product attribute through my module
.But i cant understand understand where should i put my installer
script and how to modify config file.
please suggest.
I saw your config.xml i am not able to find xml vesrion.
So you need to give xml version like below example:
<config>
<modules>
<Xyz_Total>
<version>0.1.0</version> <!-- Version of module -->
</Xyz_Total>
</modules>
</config>
Now create the file app/code/local/Xyz/Total/sql/total_setup/mysql4-install-0.1.0.ph‌​p
After that reinstall the module And check it.
You also have to add below code in config.xml under config tag.
<global>
<resources> <!-- These are resource setting giving access to module, read/write permission on database -->
<total_setup>
<setup>
<module>Xyz_Total</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</total_setup>
<total_write>
<connection>
<use>core_write</use>
</connection>
</total_write>
<total_read>
<connection>
<use>core_read</use>
</connection>
</total_read>
</resources>
</global>
After that reinstall module.
Take a look # Adding custom product attributes in Magento using setup script
<modules>
<Xyz_Total>
<version>0.1.0</version>
</Xyz_Total>
</modules>
<global>
....
<models>
<xyztotal>
<class>Xyz_Total_Model</class>
<resourceModel>total_setup</resourceModel>
</xyztotal>
</models>
<resources>
<catalog_setup>
<setup>
<module>Mage_Catalog</module>
<class>Mage_Catalog_Model_Resource_Setup</class><!-- that line !-->
</setup>
</catalog_setup>
</resources>
...
In
app/code/local/Xyz/Total/sql/total_setup/mysql4-install-0.1.0.ph‌​p
$installer->addAttribute('catalog_product', 'offer_type', array(
'backend' => '',
...
));

magento event observer (magento1.7)

i am trying to implement an simple observer in my module (app/code/local/Foo/Bar).
My objective is to set the product names to '[product name] is cool' when a product is loaded in the frontend.
These are my files:
app/code/local/Foo/Bar/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<global>
<models>
<foo_bar>
<class>Foo_Bar_Model</class>
</foo_bar>
</models>
</global>
<frontend>
<events>
<catalog_product_load_after>
<observers>
<foo_bar>
<type>model</type>
<class>foo_bar/observer</class>
<method>catalogProductLoadAfter</method>
</foo_bar>
</observers>
</catalog_product_load_after>
</events>
</frontend>
</config>
app/code/local/Foo/Bar/Model/Observer.php
<?php
class Foo_Bar_Model_Observer{
public function catalogProductLoadAfter(Varien_Event_Observer $observer)
{
$product = $observer ->getProduct();
$product ->setName($product.getName().' '.'is cool');
}
}
?>
And i have also configured the module in app/etc/modules/Foo_Bar.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Foo_Bar>
<active>true</active>
<codePool>local</codePool>
</Foo_Bar>
</modules>
</config>
But it doenst work, does anyone have any suggestion?
You only need to module name for the model:
<foo_bar>
<type>model</type>
<class>bar/observer</class>
<method>catalogProductLoadAfter</method>
</foo_bar>
you don't need to specify Foo_Bar
also have you an error in your PHP
$product ->setName($product.getName().' '.'is cool');
should be
$product->setName($product->getName().' '.'is cool');