Wednesday 21 January 2015

Create the Custom Attribute to Customer

This Post is used for Create the custom drop down attribute to customer and these following files are placed in existing module.
Required files are:
1.Namespace/module/sql/install-1.1.10.php
2.Namespace/module/Model/Attribute/Source/warehouse.php

1.Namespace/module/sql/install-1.1.10.php

<?php
//$installer = new Mage_Customer_Model_Entity_Setup('core_setup');
$installer = $this;
$installer->startSetup();

$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$setup = Mage::getModel('customer/entity_setup', 'core_setup');
$installer->addAttribute('customer', 'warehouse_name', array(
        'label' => 'Warehouse Name',
        'input' => 'select',
        'type'  => 'varchar',
        'forms' => array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'),
        'required' => 1,
        'user_defined' => 1,
        'source' => 'namespace_module/attribute_source_warehouse',
       // 'source' => NULL,
));

$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, 'warehouse_name', 0);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'warehouse_name');
$oAttribute->setData('used_in_forms', array('customer_account_edit','customer_account_create','adminhtml_customer','checkout_register'));
$oAttribute->save();

$installer->endSetup();
?>

2.Namespace/module/Model/Attribute/Source/warehouse.php

<?php
class Namespace_module_Model_Attribute_Source_Warehouse extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;

    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();

            $this->_options[] = array('label'=> 'Option 1', value=>1);
            $this->_options[] = array('label'=> 'Option 2', value=>3);
            $this->_options[] = array('label'=> 'Option 3', value=>3);
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
}
?>

Thursday 8 January 2015

Magento2 Features and Changes

Component changes:
  • Removed Some payment Method and bundled Community Modules
  • Removed/Ceased: Mage_Oscommerce,Mage_XMlConnect,Mage_DataFlow,Mage_Compiler
Functional Changes:
  • Theme configuration: simple into one field
  • Enabling Profiler: handled by bootstrap
  • Customizing Email Template : no more relation to locale
  • Session Time out : Cookie lifetime(No more cookie life time)
File Structure Modularity:
In Magento 1.x
In Magento 2.x
app/deisgn/<area>/baase/default
app/code/<pool>/<module>/view/<area>
skin/<area>/base/default
same (no distinction)
js/
same
app/locale/en_Us/template/email/*.html
app/code/<pool>/<Module>/view/email/*.html
File structure:
Root ItemsWas in Magento 1.x
Root ItemsWas in Magento 2.x
errors
pub/errors
Includes
n/a
js
pub/js
media
pub/media
pkginfo
n/a
shell
dev/shell
skin
app/design/<appropriate theme>
index.php,cron.php,get.php
index.php,pub/index.php,pub/cron.php
bootstrap logic scartted across entry points
app/bootstrap.php
Frame Work Naming:
            Lot of Difference is there for Naming Conversion in Magento1.x and Magento2.x.
Magento2.x uses Real name and Fully Qualify module names across the board.

            Magento1.x:

·         Mage::getModel(‘catalog/product’);
·         getTableName(‘admin/role’)
·         customer, checkout

Magento2.x:
·         Mage:getModel(‘Mage_Catalog_Model_Product’) (real class name)
·         getTableName(‘admin_role’) (real table Name)
·         Mage_Customer,Mage_Checkout (Fully Qualify module names across the board

Frame Work Configuration:

·         Eliminated Excessive Section (up to 20% of all config.xml)
·         Move out some of the file to specialized files
·         simplified rewrites mechanism
·         Added merging mechanism that allows XMl-Schema validation (implemented for        new types of configuration)
               
Frame Work Significant Other:

1.      Controller : formally separated routing of application areas
2.      View: Unlimited theme fall back hierarchy
3.      Introduced container (Structural elements in layout in addition to blocks)
4.      Eliminating skins (only themes remain) eliminating packages physically from the file
5.      Managing themes and design packages in entities in database
6.      decoupling controllers from layout through universal “context” engine
7.      Migrating to Jquery
8.      Model service Layer
9.      Improving Performance for API routing and granular loading of WSDL for only requested resources

User Experience:

A.      Information Architecture of the menus
B.      Product creation flow
C.      Taxes UX
D.      Navigation ACL
E.   Backend usability and UI
Magento tools:
a.       Migration tools to assists data and code from 1.x to 2.x
b.      applications/ components /themes deployment (installation/ upgrade as new tools)

c.       Zend Framework 2

Wednesday 7 January 2015

Magento2 Installation Steps

Step1: Download the Magento2 from https://github.com/magento/magento2  (or)
Step2: Download the composer.exe file from https://getcomposer.org/download/ and install it.
Step3: Copy the all composer files in magento2 root folder
Step4: Open the command prompt and go to magento2 folder and install the Composer Install
            Ex:c:\xampp\htdocs\magento2-master>composer install
If you got the any error like ‘php’ is not existing in external memory, we are create the Environment variables  in Advanced settings.
Step5:Run the magento2 in url like
             http://localhost/magento2-master/setup
Step6: Click on the Agree and Setup Magento Button
Step7: Click on Start Readiness Check button
Step8: Add the Database Details like
Database server hostname -- Mandatory
Database server username – Mandatory
Database server password – Not always Necessary
Database Name – Mandatory
Table Prefix – Optional
Step9: Select the Web configuration details
Step10: Customize your store Details like Time Zone, Currency, Language
Step11: Create the Admin Account Details with Username, Email, Password
Step12: Last step is Install the magento2 go to Homepage and Backend
               Homepage: http://localhost/magento2-master/

                Backend/admin: http://localhost/magento2-master/admin/

Magento Delete Products using CSV

<?php

require_once '../app/Mage.php';
Mage :: app("default") -> setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID );
$skuAll =array();
$file_handle = fopen("skus.csv", "r");
 while (!feof($file_handle) ) {
    $line_of_text = fgetcsv($file_handle, 1024);
$allSku = $line_of_text[0];

}
$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter(
        'sku', array('in' => $allSku)
    )
    ->load();

    if(is_array($products))
    {
        foreach ($products as $key => $pId)
        {
            try
            {
                $product = Mage::getModel('catalog/product')->load($pId)->delete();
                echo "successfully deleted product with ID: ". $pId ."<br />";
            }
            catch (Exception $e)
            {
                echo "Could not delete product with ID: ". $pId ."<br />";
            }
        }
    }
?>

Display The BestSelling Products In Magento

 Display The BestSelling Products In Magento


Step 1: Create a file app/code/local/Mage/Catalog/Block/Product/Bestseller.php and the following lines of code in it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract{
    public function __construct(){
        parent::__construct();
        $storeId = Mage::app()->getStore()->getId();
        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            ->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); // most best sellers on top
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
 
        $products->setPageSize(3)->setCurPage(1);
        $this->setProductCollection($products);
    }
}
Step2:Create a file app/design/frontend/default/YourTheme/template/catalog/product/bestseller.phtml file and add the following lines of code in it
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
<?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="page-title">
    <h2><?php echo $this->__('Best Seller Products') ?></h2>
</div>
<?php $_collectionSize = count($_products->getItems()) ?>
<table class="products-grid" id="products-grid-table">
<?php $i=1; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i%1!==0): ?>
    <tr>
    <?php endif ?>
        <td id="td_<?php echo $i;?>" <?php if($i%3==0 or $i==$_collectionSize){echo 'class="last"';} ?> >
        <?php contentBlock('top') ?>
        <div id="cont_<?php echo $i;?>">
            <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
              <?php echo $this->htmlEscape($_product->getName()) ?></a></h3>
            <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" 
             title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
                <img src="<?php echo $this->helper('catalog/image')
                          ->init($_product, 'small_image')->resize(122, 109); ?>" width="122" height="109" 
                          alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" 
                          title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
            </a>
            <div class="a-center">                        
                <?php if($_product->getRatingSummary()): ?>
                    <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
                <?php endif; ?>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <?php if($_product->isSaleable()): ?>
                    <button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span>
                    <?php echo $this->__('Add to Cart') ?></span></span></span></button>
                    <div class="clear"></div>
                <?php else: ?>
                    <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span></p>
                    <div class="clear"></div>
                <?php endif; ?>
                <ul class="add-to-links">
                    <?php if ($this->helper('wishlist')->isAllow()) : ?>
                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>">
                        <?php echo $this->__('Add to Wishlist') ?></a></li>
                    <?php endif; ?>
                    <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                        <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>">
                        <?php echo $this->__('Add to Compare') ?></a></li>
                    <?php endif; ?>
                </ul>
                <?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
            </div>
        </div>
    </td>
    <?php if ($i%3==0 or $i==$_collectionSize): ?>
   </tr>
    <?php endif ?>
  <?php $i++; endforeach; $kol = $_collectionSize; ?>
</table>
<?php endif; ?>
Step 3: This above files will create a list of best selling products which can be shown anywhere on your Magento store. All you have to do is place the following line of code block in your template to show the best selling products.
1
{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}
Sweet, now we are able to show best selling products and most viewed products anywhere in our Magento store. If you run into any problem implementing the code, please feel free to comment and let me know. Subscribe our RSS To receive latest Magento development updates.

Product Collection with out of stock and In Stock in Magento 2

Product collection with out of stock and In stock. use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; $collection = $th...