Sunday 4 October 2015

Reset admin password

Dive in the code of password validation

Open app/code/core/Mage/Admin/Model/Session.php and find the function called 'login'. And the code below should be found in the function.
$user->login($username, $password);

Open app/code/core/Mage/Admin/Model/User.php and find the login function. The following code will be spotted.
if ($this->authenticate($username, $password)) {

Follow the 'authenticate' method and you will find the code
Mage::helper('core')->validateHash($password, $this->getPassword()

Open app/code/core/Mage/Core/Helper/Data.php and move to 'validateHash' method. There is only one sentence of code:
$this->getEncryptor()->validateHash($password, $hash);

Open app/code/core/Mage/Core/Model/Encryption.php which is literally the password validation method.

$hashArr should be an array comprised of two elements. The first element is the hashed password and the last element is the random hash key, and the hash method follows the equation below:
Hashed Password = md5(Actual Password + Hash Key)

The password value in the database is a string which consist of Hashed Password as the first part and Hash Key as the second part and these two parts are delimited by a colon symbol(:)

Update the password by sql query

select password from admin_user where user_id=1; #user_id 1 should be replaced by the user_id whose password needs to be updated

A result, which is similar as ceccc111cf1c3600a7ad5d464934ea24:HH6gy344GGuoZzDm3U6dKSoXPcpuqIsQ, will be found.

If you want to use 123 as your password, execute the following sql query in the database:
select md5('ceccc111cf1c3600a7ad5d464934ea24123');# the result is 6711eaa5577250a42aefdb9c04ad90ed

Use the sql query below to update the password:
update admin_user set password='6711eaa5577250a42aefdb9c04ad90ed:HH6gy344GGuoZzDm3U6dKSoXPcpuqIsQ' where user_id=1 limit 1;

Here we are and the new password can be used to log in the admin


Tuesday 7 July 2015

Magento website SEO skills/tips

Understand your audience with google trends

In google trends, any keywords can be put into the search box and google will display the search interests, related searches etc which can help you identify your audience, the potential search keywords the contents.

Use informative URLs

Magento has the functionality of generating the informative URLs by re-indexing the catalog_url index.
But it's only helpful for English categories and products. If your website is in other languages, you might need to specify the URL keys for categories and products, and the cms pages manually in admin pane or programmatically.

Add your site to major search engines

There are some common used search engines listed as below:
1. Google
2. Bing
3. Baidu
4. Sougou
Add your site to these search engines and submit the sitemap or URLs. The methods of adding your site vary from different engines. However, what you need to do is just to follow the instructions which shouldn't be difficult.

The method of checking whether you website has been indexed by search engines

For both google and baidu, you can just type 'site:yourdomaim' and the indexed pages will show up. Since in Magento the 404 pages are easy to occur if there is anything wrong with Magento index, you need to make sure the index system is correct and check and fix the 404 pages found in the search results.




Wednesday 1 July 2015

Magento Remove firstname and lastname for registration

Set the firstname and lastname fields in database not required

SELECT * FROM `eav_entity_type`;#find the customer entity_type_id
SELECT * FROM `eav_attribute` WHERE `attribute_code` IN ('lastname','firstname') and `entity_type_id`=1; #find firstname and lastname attributes;#find firstname and lastname attributes
UPDATE `eav_attribute` SET `is_required`=0  WHERE `attribute_code` IN ('lastname','firstname') and `entity_type_id`=1; #set is_required 0 which means it's not necessary

Copy and modify template file to remove the classes related to validation

Copy \app\design\frontend\base\default\template\customer\widget\name.phtml to your theme folder
Remove the following code for firstname and lastname field:
class='required'
<?php echo $this->helper('customer/address')->getAttributeValidationClass

Create a custom extension to override Mage_Customer_Model_Customer

Create a custom extension.
Add the following xml in config.xml
     <global>
        <models>
            <customer>
              <rewrite>
               <customer>Yourcompany_Yourmodule_Model_Customer</customer>
              </rewrite>
            </customer>
        </models>
     </global>
Copy the validate method from magento core to your module and remove the validation code of firstname and lastname

Hide firstname and lastname in registration form if needed

Add the following code in less file:
//hide first name and last name in registration form by bargainoverseas dev
.customer-account-create {
    .customer-name{
         display: none;
    }
}

Magento Add a link to top links or remove a link from top links

Add a link to top links via layout xml, for instance in the theme's local.xml

   <customer_logged_out>
        <reference name="top.links">
            <action method="addLink" translate="label title" module="customer"><label>Create an Account Imediately</label><url helper="customer/getRegisterUrl"/><title>Create an Account Imediately</title><prepare/><urlParams/><position>10</position></action>
        </reference>
        <remove name="reorder"></remove>
    </customer_logged_out>
note: this is for unsigned-in user, if you search addLink in the app/design folder, there will be more for specific pages. 

Remove a link from top links via layout xml

    <customer_logged_out>
        <reference name="top.links">
            <action method="removeLinkByUrl"><url helper="customer/getAccountUrl"/></action>
        </reference>
    </customer_logged_out>

Tuesday 30 June 2015

Override Magento varien product.js in an extension

Add layout updates in the module's etc/config.xml file

    <frontend>
        <layout>
          <updates>
            <yourcompany_yourmodule>
              <file>yourcompany_yourmodule.xml</file>
            </yourcompany_yourmodule>
          </updates>
        </layout>
    </frontend>

Create a layout xml file named yourcompany_yourmodule.xml,corresponding to the file name configured at the first step, in your current package and current theme folder, and add js in the layout file

<layout>
    <yourcompany_yourmodule_handle><!-- declare a custom handle so you won't duplicate the code -->
        <reference name="head">
            <action method="addJs">
                <script>yourcompany/yourmodule/product.js</script>
            </action>
            <action method="addJs">
                <script>yourcompany/yourmodule/configurable.js</script>
            </action>
        </reference>
    </yourcompany_yourmodule_handle>
    <PRODUCT_TYPE_configurable><!-- layout handle for simple products -->
        <update handle="yourcompany_yourmodule_handle" /> <!-- include the handle you declared above -->
    </PRODUCT_TYPE_configurable>
</layout>

Create js files in js folder. In my case, there are two files that needs to be created: product.js and configurable.js

If you want to override the reload method of Product.OptionsPrice, just put the following code in product.js
Product.OptionsPrice.prototype.reload
   = Product.OptionsPrice.prototype.reload.wrap(function(){
    {
         alert('override reload');
    }
});

If you want to override the getOptionLabel method of Product.Config, just do as the code below:
Product.Config.prototype.getOptionLabel
   = Product.Config.prototype.getOptionLabel.wrap(function(parent, option, price){
    {
         alert('override getOptionLabel');
    }
});

How to make a magento directory structure for publishing a extension with JS

In Magento, if you want to publish an extension after having developed it, it's quite complicated and boring to copy the files to a separate folder and zip it as the following, but it's a have-to-do thing.

Create the extension folder: 

mkdir ~/tmp/yourcompany_yourmodule

Copy the module's php code and configuration under app/code folder

mkdir ~/tmp/yourcompany_yourmodule/app/code/local/Yourcompany -p
cp -r app/code/local/Yourcompany/Yourmodule ~/tmp/yourcompany_yourmodule/app/code/local/Yourcompany/

Copy the module's layout files under app/design folder




mkdir ~/tmp/yourcompany_yourmodule/app/design/frontend/rwd/default/layout -p
cp app/design/frontend/rwd/default/layout/yourcompany_yourmodule.xml ~/tmp/yourcompany_yourmodule/app/design/frontend/rwd/default/layout/


Copy the main configuration under app/etc folder

mkdir ~/tmp/yourcompany_yourmodule/app/etc/modules -p
cp app/etc/modules/Yourcompany_Yourmodule.xml ~/tmp/yourcompany_yourmodule/app/etc/modules/

Copy the js files under js folder

mkdir ~/tmp/yourcompany_yourmodule/js/yourcompany -p
cp -r js/yourcompany ~/tmp/yourcompany_yourmodule/js/
note: the js files should be added in the layout xml files

Thursday 25 June 2015

Magento How to change/define a new layout for category pages(Eg. change from 3 columns to 2 columns)

Changing individual category page layout

This can be done via Magento admin pane.
Go to Catalog->Manage Categories and select the category whose layout you want to change.
Click Custom Design tab on the right.
Select a value for Page Layout from the options.
Save the change by clicking save category and the layout will take effect when you visit the page of this category.

Changing category page layout for all of the pages in your theme's layout/local.xml

Go to the theme folder under the package folder, and open layout/local.xml file or create this file if not exists.
Add the configuration xml below in the file.
<catalog_category_default>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
</catalog_category_default>
Go to System->Configuration and click Design on the left menu.
On the right,choose Themes section and change the Layout input to the theme name you are using.

Changing the page layout for all the pages which include category pages

Open layout/page.xml which might located in /app/design/frontend/base/default/layout/page.xml or your own theme layout folder.
Find the xml configuration below in the xml default node:
<block type="page/html" name="root" output="toHtml" template="page/2columns-left.phtml">.
Here you can change the template to what you want.

Changing multiple category layouts in database which comes from changing individual category page layout in admin pane

Open the table named 'eav_entity_type', and find the entity_type_id where entity_type_code  equal 'catalog_product'. In my example, the entity_type_id is 3.
Open the table named 'eav_attribute' and search the records with the entity_type_id of 3 and find the record whose attribute code is 'page_layout'. And then keep the attribute_id. It is 61 in my database.
Open the table of 'catalog_category_entity_varchar', search by attribute_id of 61. The values of the category layouts will show up. You can change the values and insert new record for categories not in this table as well.