Posts

Posts

Create An External Database Connection in Magento

Magento

Creating The Connection

To create your connection, create a custom module and add the following to your config.xml. The code below is the bare minimum needed to get the external database connection working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?xml version="1.0"?>
<config>
    <modules>
        <Fishpig_Externaldb>
            <version>0.1.0</version>
        </Fishpig_Externaldb>
    </modules>
    <global>
        <resources>
            <externaldb_write>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_write>
            <externaldb_read>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_read>
            <externaldb_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </externaldb_setup>
            <externaldb_database>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[db_username]]></username>
                    <password><![CDATA[db_password]]></password>
                    <dbname><![CDATA[db_name]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </externaldb_database>
        </resources>
    </global>
</config>

Clear the cache after creating your module and from now on, each time you load Magento, a second database connection will be created

Accessing The Database

When I initially found out how to create an external database connection, I was using Zend_Db to retrieve all of my information. You can use the following to test your database connection is working:

1
2
3
4
5
6
7
<?php
    $resource   = Mage::getSingleton('core/resource');
    $conn       = $resource->getConnection('externaldb_read');
    $results    = $conn->query('SELECT * FROM tblName');
    print_r($results)

This works fine, however kind of takes the point away from having this connection available in Magento.

Accessing The External Database Using Models

Using models to access the database keeps our code style uniform throughout Magento. Also, it means we can integrate any other CMS or database driven application without learning it’s coding practices. To achieve this, simply add models to your custom module like you would for any other module.

I will attempt to demonstrate how to set up the models and config.xml files now. For this example I will pretend I am integrating Magento with a system that lists books (reading is cool!).

Creating The Model Class Files

Create the following files:

code/local/Fishpig/Externaldb/Model/Book.php

1
2
3
4
5
6
7
8
9
<?php
class Fishpig_Externaldb_Model_Book extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        $this->_init('externaldb/book');
    }
}

code/local/Fishpig/Externaldb/Model/Mysql4/Book.php

1
2
3
4
5
6
7
8
9
<?php
class Fishpig_Externaldb_Model_Mysql4_Book extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('externaldb/book', 'book_id'); // book_id refers to the primary key of the book table
    }
}

code/local/Fishpig/Externaldb/Model/Mysql4/Book/Collection.php

1
2
3
4
5
6
7
8
<?php
class Fishpig_Externaldb_Model_Mysql4_Book_Collection extends Mage_Core_Model_Mysq4_Collection_Abstract
{
    public function _construct()
    {
        $this->_init('externaldb/book');
    }
}

That’s the bare minimum needed for your models to be able to access the database!

Adding the Models to The Config

To inform Magento about our models, we need to register them in config.xml. Below is an updated version of config.xml with the models for Book registered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?xml version="1.0"?>
<config>
    <modules>
        <Fishpig_Externaldb>
            <version>0.1.0</version>
        </Fishpig_Externaldb>
    </modules>
    <global>
        <models>
            <externaldb>
                <class>Fishpig_Externaldb_Model</class>
                <resourceModel>externaldb_mysql4</resourceModel>
            </externaldb>
            <externaldb_mysql4>
                <class>Fishpig_Externaldb_Model_Mysql4</class>
                <entities>
                    <book>
                        <table>library_book</table>
                    </book>
                </entities>
            </externaldb_mysql4>
        </models>
        <resources>
            <externaldb_write>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_write>
            <externaldb_read>
                <connection>
                    <use>externaldb_database</use>
                </connection>
            </externaldb_read>
            <externaldb_setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </externaldb_setup>
            <externaldb_database>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[db_username]]></username>
                    <password><![CDATA[db_password]]></password>
                    <dbname><![CDATA[db_name]]></dbname>
                    <model>mysql4</model>
                    <type>pdo_mysql</type>
                    <active>1</active>
                </connection>
            </externaldb_database>
        </resources>
    </global>
</config>

That’s it, the models should now be registered in Magento!

Testing The Models

Testing them is easy enough, just treat them like normal Magento models.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
    // Load the book with a primary key value of 4
    $_book = Mage::getModel('externaldb/book')->load(4);
    // This would print out the value in the field isbn in the external database
    echo $_book->getIsbn();
    //You can even update records!
    $_book->setName('1984');
    $_book->setAuthor('George Orwell');
    try {
      $_book->save();
    } catch (Exception $e) {
      exit($e->getMessage());
    }

Conclusion

This is one of my first blogs so I probably haven’t written this up as well as I could have, however, I think that using a second database in Magento can be extremely useful. I’ve written a very good WordPress/Magento plugin which gives access to all WordPress post/category/image/link etc information. If you would like to discuss this plugin or would like to use it in one of your sites then please let me know!

Reference: http://fishpig.co.uk/blog/create-an-external-database-connection-in-magento.html

Magento Custom Module Development

Magento

Magento is a new and powerful eCommerce system offering rich customization possibilities by extensions and modules. Magento is built on a fully modular model that influences an unlimited scalability and flexibility for your store. Part of customizing Magento is, creating Magento custom Modules. These allow you to inject functionality anywhere, whether in a “static” block fashion that’s more than static, or a shipping/payment module, or large module to do something as large as integrating a 3rd party system (or multiple systems).

Basically, magento module is an extension which extends the features and functionality of Magento. Some concrete examples of modules are additional payment gateway integrations, or a featured items promotional tool. With Magneto’s fully object-oriented programming, all modules are immediately accessible via templates tags from any template files. And because Magento is feature-rich right out of the box, you’ll never again have to be dependent on a programmer to finish the simplest tasks for you. Magento also thrives on an extensive network of knowledgeable community members (including the official Magento Team), so you will never need to think twice about where to get guidance should you need it along the way.
Ads by Google

Part of customizing Magento is, of course, creating custom Modules. These allow you to inject functionality anywhere, whether in a “static” block fashion that’s more than static, or a shipping/payment module, or large module to do something as large as integrating a 3rd party system (or multiple systems).

Creating a magento module involve several steps. This article describes the steps that need to be followed to create a new module.

1) Create directory app/code/local/mymodulepack. Here ‘mymodulepack’ will be the new directory folder where you can create modules.

2) Create module named ‘My Module’ in above defined directory path. This will be your module directory which will contain required files like models, blocks, controllers etc.

3) Now add your new module xml file in app/etc/modules/mymodulepack _All.xml. Consider that _All.xml will be used to add all new module definitions to the same file instead of cluttering the etc/modules folder with many files.

4) Following defined code is used to inform Magento that there is an active module (you can turn it off from here by setting ‘active’ to false). Also that it is located in the ‘local’ code pool.

<?xml version=”1.0??>

<config>

<modules>

< Mymodulepack _MyModule>

<active>true</active>

<codePool>local</codePool>

</ Mymodulepack _MyModule>

</modules>

</config>

5) Now you can enable or disable this module at Admin -> System -> Advanced. Here you can see your new module ‘Mymodulepack_MyModule’ listed there with status enable.

6) And now create the block php class file at app/code/local/Schogini/Example/Block/Myview.php which contains a function that returns the string ‘Hello World’.

class Mymodulepack_MyModule_Block_Myview extends Mage_Core_Block_Template {

public function myFunction(){

return “Hello World!”;

}

}

?>

7) Now  make a config.xml file in app/code/local/Mymodulepack/MyModule/etc directory. This is done to enter configuration information for the module.

<?xml version=”1.0? ?>
<config>
<modules>
<Mymodulepack_MyModule>
<version>0.1.0</version>
</Mymodulepack_MyModule>
</modules>
<global>
<blocks>
<mymodule>
<class>Mymodulepack_MyModule_Block</class>
</mymodule>
</blocks>
</global>
</config>

8) Now we need to create a template file which uses the View Block Class to display the message “Hello World”. Create a template (phtml) called myview.phtml in the following directory: appdesignfrontenddefaultdefaulttemplatemymodulemyview.phtml.

9)The module is now ready for use. You can view the output by two ways.

1) {{block type=”mymodulepack_mymodule/view”  template=”mymodule/myview.phtml” }}

2) <reference name=”right”>
<block type=”mymodule/view” name=”mymodule”        template=”mymodule/myview.phtml”/>
</reference>

At Biztech Consultancy – India based website Development Company, we offer an efficient Magento Development & Magento customization services for ecommerce website development at competitive price. Our Magento developer helps in Magento Themes development, Magento Design Integration, Magento module development, magento custom module development, Magento Custom Development, etc.

Reference: http://www.articlesbase.com/programming-articles/magento-custom-module-development-1679280.html

How to Install Free or Premium Magento Themes

Magento

How to Install Free or Premium Magento Themes

Magento is becoming one of the best E-commerce store which is gaining popularity by leaps and bounds. With this rate of increasing popularity, many existing and new stores are bound to migrate or start with Magento pretty soon. Although, Magento is quite good but it requires proper knowledge before you indulge yourself into Magento customization. If you want to create a new Magento theme by yourself that’s a different story altogether but if want to leverage the functionality and layout provided by any premium or free Magento theme then you should know how to install the theme soon after successful download.

There are two ways to install a Magento theme

  1. Install via Magento Connect
  2. Install via placing appropriate theme template folders in your Magento Installation.

It entirely depends upon the theme availability and theme quality but if the theme is available in Magento Connect (it has a good collection of free & premium themes) then you can install it very easily without any technical knowledge. Following is a breakdown of how to install a Magento theme via Magento Connect and via inserting new theme template in your existing or new Magento installation.

How To Install a Magento Theme Via Magento Connect

To install a theme from Magento Connect you need to follow certain steps which are explained below (Advance users may skip few steps):

1. Select the theme by browsing the huge Magento theme archive. OR, If you know the direct URL of your favourite theme just open it’s Magento connect URL

2. Now, you will see a button right under the theme image called “Get Extension Key”. You need to click “Get Extension Key” and agree to extension license agreement.

magento-connect-themes

3. After you select agree checkbox and click “Get Extension Key” you will see the Magento extension key right there itself (displayed in the box). You need to paste this extension key in your store’s Magento connect tab. Keep it safe.

4. Copy this Magento extension key. E.g. It will look like “magento-community/Magik_Autocomplete”

5. In order to install this theme into your Magento store you need to login to your store admin panel and go to System->Magento Connect->Magento Connect Manager. Magento connect manager will ask you to login again. You need to use your store admin login credentials to get past the login screen.

magento-connect-manager

You need to paste the theme extension key copied from the Magentocommerce.com’s Magento connect here.

Magento connect checks for the extension key validity and downloads the theme on your system. Depending upon your internet connection speed (bandwidth) and theme size it will take time to install. These extension keys are specifically created with a unique combination so that there is no confusion over which extension to download. If you have correct extension key and there are no errors in theme or your internet bandwidth you will not see any error messages. In case there are any error messages try downloading and installing again. If you do not succed at all then you can contact the theme owner or ask questions in the theme documentation tab.

6. Soon after you see “Theme successfully installed” message go to System->Configuration->Design->Themes and provide the theme name in Default field (which will change your default theme to this new theme) and click “Save Config” button at the top right corner of your screen.

magento-system-design-configuration

7. A Magento theme revolves around different columns in your theme layout. In case your store home page doesn’t load properly and shows weird design then go to CMS->Manage Pages. You will notice that there are two different layouts for the home page (may be more depending upon how many themes you have tried in the past). Based on your new theme layout disable all the other home page layouts. You can easily disable/enable the layouts by clicking “Enabled” or “Disabled” links provided in the status field.

magento-cms-manage-pages

8. Check your home page if the store is looking just like your new theme, you are done. If you still see problems chances are that you haven’t disabled the correct home page layout. Repeat step 7 until you see a correct store home page (not required if you have only two home page layouts showing in your “Manage Pages” section.

This was the easiest and fastest way of installing a Magento theme using Magento Connect manager.

Many Magento theme sites don’t publish their theme template in Magento connect (sometimes their themes do not pass through Magento connect guidelines). These themes are provided directly by the theme owner sites. If you are interested in downloading a Magento theme from such sites then following are the steps required to install a Magento theme within your existing Magento installation.

How To Install A Downloaded Magento Theme

Unlike single template themes (WordPress) Magento themes are quite different as its layout depends on four elements i.e.

  1. Layout – Present is (app/design/frontend/your_interface/new_theme/layout/)
  2. Templates – Present in (app/design/frontend/your_interface/new_theme/template/)
  3. Skins – Present in (skin/frontend/your_interface/new_theme/)
  4. Locale – Present in (app/design/frontend/your_interface/new_theme/locale/)

In order to install and integrate a new theme you need to follow few steps more than what you are accustomed to follow in case of wordpress themes.

If you have already downloaded your preferred Magento theme then here is how you can install and integrate this new theme into your existing Magento installation.

 

  1. Uncompress the zip file containing theme templates (you can use winrar or winzip to uncompress files on windows machine or use unzip zipfilename.zip to uncompress files on Linux server)
  2. Copy entire app folder of your new theme under Your_Magento_Installation/app/design/frontend/default/ so it becomes like Your_Magento_Installation/app/design/frontend/default/new_theme
  3. Copy entire skin folder of your new theme under Your_Magento_Installation /skin/frontend/default/ so it becomes like Your_Magento_Installation/ skin/frontend/default/ new_theme
  4. Now, as you have added your newly downloaded theme in your Magento store you need to login to your Magento store admin to tell Magento which theme to use as layout.
  5. Sometimes Magento Cache doesn’t let you see your changes instantly. In order to test your new theme it would be wise to disable cache for the time being. Go to System > Cache Management and select “Disable” then click “Save Cache”. Once you are done with all the steps Enable the cache again.
  6. Now go to System -> Configuration and select the Design Tab
  7. Type the name of your new theme which you have copied in your Magento installation in front of skin (Images/CSS) input box and click “Save Config”.
  8. Optional – If you have downloaded a widget ready Magento template then you need to install the widget provided with the theme. In order to install Magento theme widget you have to go to CMS -> Static Blocks and click on “Add New Block”. Based on your widget installation instructions you have to add your new widget and activate that using this new widget block. Finally select “Enabled” and click “Save Block”. Go and select “Custom Design” Tab and in Layout select “Homepage” otherwise your widget won’t show on home page.
  9. Optional – Home page of default Magento store is a CMS block so you have to provide custom code there in order to display something on home page. To do so you have to go to CMS- > Manage Pages and select “Home page” and in the content area type the home page content code which comes up with your new theme. You can change this area anytime you want.
  10. All set. Open up a new page on your browser and type in your store name to see the new theme. Your new Magento theme is ready to be used.

I have installed a New Magento Theme but i can’t see any products showing up, why?
Well, It can only happen in case of a new store. You need to follow few steps to fix this problem

 

  1. Go to Catalog -> Manage Categories
  2. Select the category you want to show on your Homepage and look for its ID.
  3. Go back to CMS -> Manage Pages and select Homepage
  4. Paste the code for showing Products on home page with the selected category ID.

I hope this is quite comprehensive tutorial for installing Magento themes. I would love to hear your thoughts, views and experiences. Please leave me a comment and let me know.

jQuery Mobile

Mobile Framewok

Touch-Optimized Web Framework for Smartphones & Tablets

A unified, HTML5-based user interface system for all popular mobile device platforms, built on the rock-solid jQuery and jQuery UI foundation. Its lightweight code is built with progressive enhancement, and has a flexible, easily themeable design.

 

here is a sample page

<!DOCTYPE html> <html> <head> <title>Page Title</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script> </head> <body> <div data-role="page"> <div data-role="header"> <h1>Page Title</h1> </div><!-- /header --> <div data-role="content"> <p>Page content goes here.</p> </div><!-- /content --> <div data-role="footer"> <h4>Page Footer</h4> </div><!-- /footer --> </div><!-- /page --> </body> </html> 

Div width issue on ipad

Linux, Magento

 

When the site loads on my I pad the red menu bar is red and turns black along the right side of the page?Actually menu bar is inside a  div.And i have given bg color as red  with 100%.

Problem is on the iPad  viewport .iPad default viewport is   980 pixels .So even if you are giving 100% width  we will not get the actual result.He when the site loads on my I pad the red menu bar is red and turns black along the right side of the pagere we are not setting content /element width .So by  resetting the width of the view port , we can solve the issue.I added the following code on my style sheet and solved the issue.Thanks to all the div available on net.

 

Solutions:

body{
min-width:1024px;
}

MySQL Importing with linux command

Linux, MySQL

PhpMyAdmin is a great tool to manage the mysql database.But if we have large database and table to handle,it will be great if we able to do the same with the help of the linux command.

$ mysql -u username -p -h localhost Data-Base-Name < myfile.sql

Top