In this article we would like to show you how to add a new custom category attribute in Magento 1. For doing this in Magento 2, read this post. Let’s say, this attribute is needed to display some content on the category page. First of all, we need to create a new module for adding custom category attribute, we will call it “Custom Category Attribute”.

Step 1. Create new module. We should let Magento know about our new module. Initial configuration file is located in ‘app/etc/modules/Atwix_CustomCategoryAttribute.xml’. Atwix_CustomCategoryAttribute.xml

1
2
3
4
5
6
7
8
9
<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_CustomCategoryAttribute>
            <active>true</active>
            <codePool>community</codePool>
        </Atwix_CustomCategoryAttribute>
    </modules>
</config>

It means that the module is active and it is located in the community code pool.

Step 2. Configure module. Module configuration file is located in ‘app/code/<code pool>/<name space>/<module name>/etc’ and its name is config.xml – note, that in our case this looks like ‘ app/code/community/Atwix/CustomCategoryAttribute/etc/config.xml’. config.xml

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
<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_CustomCategoryAttribute>
            <version>0.0.1</version>
        </Atwix_CustomCategoryAttribute>
    </modules>
    <global>
        <resources>
            <add_category_attribute>
                <setup>
                    <module>Atwix_CustomCategoryAttribute</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute>
            <add_category_attribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_write>
            <add_category_attribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_read>
        </resources>
    </global>
</config>

As you can see, configuration file is not large, there are two nodes only: module version and resources for install script. Install script helps us to create a new attribute. In the node <resources> we defined the class for our install script which will be used for the extension. Working with methods of this class helps to create, update, remove attribute (etc). And the node <add_category_attribute> says that script must be located in the folder with the same name (in our case path will be ‘app/code/community/Atwix/CustomCategoryAttribute/sql/add_category_attribute’)

Step 3. Create attribute. Another important thing is to create install script file in the folder ‘add_category_attribute’, and the file name depends on the module version, so it looks like ‘mysql4-install-x.x.x.php’, where x.x.x is the version of the module. mysql4-install-0.0.1.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_attribute', array(
    'group'         => 'General Information',
    'input'         => 'textarea',
    'type'          => 'text',
    'label'         => 'Custom attribute',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$this->endSetup();

As a result, we created a new attribute in the category with label ‘Custom attribute ‘. This attribute is a text area and it must be visible on the frontend. Check this out!

Step 4. Check result. Here, let’s clear cache and after this, go to Manage Categories – in ‘General Information’ tab you will see a new attribute: ‘Custom attribute’.Moreover, to see the content from ‘Custom attribute’ on the frontend in the category page – you need to use the helper for reading attribute in the template of this page (‘app/design/frontend/<package> /<theme>/template/catalog/category/view.phtml’), check please the following code:

1
2
3
4
5
...
<?php if($_customAttribute = $this->getCurrentCategory()->getCustomAttribute()): ?>
    <?php echo $_helper->categoryAttribute($_category, $_customAttribute, 'custom_attribute') ?>
<?php endif; ?>
...

As this code was put before ‘product list’ output, so after refreshing a page we will see the result:Following all these steps, it will be great to enable wysiwyg editor for a new attribute, that will give us an opportunity to place different content with an inline styles in simple way.

Step 5. Update attribute. For each version of the module, it is also possible to have upgraded scripts which filenames are of the form mysql4-upgrade-0.0.1-0.0.2.php. It seems to be the best solution to update the attribute.mysql4-upgrade-0.0.1-0.0.2.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_attribute', array(
    'group'         => 'General Information',
    'input'         => 'textarea',
    'type'          => 'text',
    'label'         => 'Custom attribute',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'wysiwyg_enabled' => true,
    'visible_on_front' => true,
    'is_html_allowed_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
));
$this->endSetup();

As well, method addAttribute can not only create, but also update the attribute – that’s why, you may also use method updateAttribute.

Also need to check this version at config.xml

<version>0.0.2</version>

Step 6. Check updated result. At last, we clear cache and see the button ‘ WYSIWYG Editor ‘ near ‘Custom attribute’. Also, it is necessary to use editor to change the content in this attribute, insert some image, format text and at the end – to save the changes. After all, you should refresh category page and see the result:

Laravel Cheat Sheet

Posted: December 9, 2016 in PHP

Please refer below PDF.
Download Laravel Cheatsheet

We always need ask confirmation for remove items. I mean we always ask before deleting items. So if you need to ask confirm before do something like delete, change status, change value etc as you require. If you want to do with better layout then you have to chooes bootbox.js and if you already use bootstrap then it’s very better so, let’s see bellow example and use it.

Example:

<html lang="en">

<head>

    <title>Delete confirm modal using bootbox example</title>  

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

     src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js">

     src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js">  

</head>

<body>

<button class="btn btn-danger remove">Delete</button>

<!-- bootbox code -->

 src="https://raw.githubusercontent.com/makeusabrew/bootbox/gh-pages/bootbox.js">  



    $(document).on("click", ".remove", function(e) {

        bootbox.confirm("Are you sure you want to delete?", function(result) {

            if(result){

              console.log('write code of remove item.');

            }

        }); 

    });



</body>

</html>

Whenever you require to download file or image from URL using php curl. then you can see that example. we can download image or file from given url and save in over local server. you can do that using get_file_contents() in php too, but i think it is good if you are doing that using PHP curl. let’s see following example :

Example

$url = 'http://www.xyz.com/test-image.png';

$curlCh = curl_init();

curl_setopt($curlCh, CURLOPT_URL, $url);

curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curlCh, CURLOPT_SSLVERSION,3);

$curlData = curl_exec ($curlCh);

curl_close ($curlCh);

$downloadPath = "upload/flower10.jpg";

$file = fopen($downloadPath, "w+");

fputs($file, $curlData);

fclose($file);

Git remove all current changes, if you did some wrong code in your project and you want to remove from git. then you can simply remove by git checkout command.i mean you did changes in your local system from git. you can use this command.

git checkout

 
Try this..

Sometimes you need to undo your git file before you git comment command. Because you made a some misteck or wrong code and you did git add using “git add .” command then you think how to undo your code before git commit command. But you can do that using git reset command, if you need to undo all file Or you may need to undo just one then you can do using git reset command, you can see bellow command.

Example:

// For Spesific file

git reset

 
// For all files

git reset

Git remove last commit from local

Posted: December 8, 2016 in GIT

Git is distributed version control system that is a awesome. when ever you are working with git repository. if you did commit your code in your local system by mistake, then if you want to remove last commit from your project. so let’s see following example:

Example:

git add .

git commit -m "change for test"

git reset --soft HEAD~

How to git force pull from remote branch ?

Posted: December 8, 2016 in GIT

When i was working on my Laravel application using git bitbucket repository, i fatch problem when first i did commit on my master branch and i think i did 3 or 4 commits, and my project partner pull also in his local system ,but we did wrong code in this commit so we required to remove that 3 or 4 commit from my bitbucket repository. he did remove that commits from his system but when i did pull but nothing to change, i also remove cache but same.

But At last i though i thing i have to hard pull that’s why i fired following command. if you also fatch problem like this then let’s see this command:

Example:

git fetch --all

git reset --hard origin/master

How to set git config username and email?

Posted: December 8, 2016 in GIT

When you first time create your git repo or clone project from git repo then you must have to set username and email. You can also set global username email and also you can set per repository username email. So, in this post in bellow command through you can set global git username and email.

Global Set:

git config --global user.name "Haresh Patel"

git config --global user.email "itsolutionstuff@gmail.com"

git config --list

 
You can also set username and email per project repo using bellow command. First you have to go on your project root directory.

Per Repo Set:

git config user.name "Haresh Patel"

git config user.email "itsolutionstuff@gmail.com"

git config --list

Onetime i was need to check which files i deleted before, When i was working on my laravel application. I was try to find how to show deleted files log from git command. But at last found git command from google search. we can view all deleted file log from our git repository. So, let’s fire bellow command.

Command:

git log --diff-filter=D --summary