Saturday, May 14, 2016

Delete all Sample Data in Magento 1.x

Magento used ORM structure for developing e-commerce website so it's faster and easier to fetch and manage data from database. so Magento also stores some unnecessary data which is not much useful for store so you can remove that from the database. Here to display the SQL queries that run in your mysql database. It's only remove junk data not effects on the site..
Here below also provide some other SQL queries for deleting products, orders and users. In below all examples use Magento database without any table prefix.If your database table has any prefix than writes table query as prefix..

Remove Magento junk and unnecessary data from Database

SET FOREIGN_KEY_CHECKS = 0; //Use for ignore foreign key if any.
-- Compare
TRUNCATE `catalog_compare_item`;

-- Wishlist
TRUNCATE `wishlist_item_option`;
TRUNCATE `wishlist_item`;
TRUNCATE `wishlist`;

-- Admin notification
TRUNCATE `adminnotification_inbox`;

-- Dataflow
TRUNCATE `dataflow_batch_export`;
TRUNCATE `dataflow_batch_import`;

-- Report
TRUNCATE `report_event`;
TRUNCATE `report_viewed_product_index`;
TRUNCATE `report_compared_product_index`;

-- Search
TRUNCATE `catalogsearch_fulltext`;
TRUNCATE `catalogsearch_query`;
TRUNCATE `catalogsearch_result`;

-- FLAT CATALOG (EDIT TABLE NAMES)
TRUNCATE `catalog_category_flat_store_1`;
TRUNCATE `catalog_product_flat_1`;

-- Logs
TRUNCATE `log_customer`;
TRUNCATE `log_quote`;
TRUNCATE `log_summary`;
TRUNCATE `log_summary_type`;
TRUNCATE `log_url`;
TRUNCATE `log_url_info`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
TRUNCATE `log_visitor_online`;

-- Session
TRUNCATE `core_session`;
TRUNCATE `api_session`;

-- Cache
TRUNCATE `core_cache`;
TRUNCATE `core_cache_option`;
TRUNCATE `core_cache_tag`;

-- Index
TRUNCATE `index_event`;
TRUNCATE `index_process_event`;

-- Captcha
TRUNCATE `captcha_log`;

-- Sent to friend
TRUNCATE `sendfriend_log`;

-- Temp and index tables
TRUNCATE `catalog_category_anc_categs_index_tmp`;
TRUNCATE `catalog_category_anc_products_index_tmp`;
TRUNCATE `catalog_category_product_index_enbl_tmp`;
TRUNCATE `catalog_product_index_eav_decimal_tmp`;
TRUNCATE `catalog_product_index_eav_tmp`;
TRUNCATE `catalog_product_index_price_bundle_opt_tmp`;
TRUNCATE `catalog_product_index_price_bundle_sel_tmp`;
TRUNCATE `catalog_product_index_price_bundle_tmp`;
TRUNCATE `catalog_product_index_price_cfg_opt_agr_tmp`;
TRUNCATE `catalog_product_index_price_cfg_opt_tmp`;
TRUNCATE `catalog_product_index_price_downlod_tmp`;
TRUNCATE `catalog_product_index_price_final_tmp`;
TRUNCATE `catalog_product_index_price_opt_agr_tmp`;
TRUNCATE `catalog_product_index_price_opt_tmp`;
TRUNCATE `catalog_product_index_price_tmp`;
TRUNCATE `cataloginventory_stock_status_tmp`;
TRUNCATE `catalog_category_anc_categs_index_idx`;
TRUNCATE `catalog_category_anc_products_index_idx`;
TRUNCATE `catalog_category_product_index_enbl_idx`;
TRUNCATE `catalog_category_product_index_idx`;
TRUNCATE `catalog_product_index_eav_decimal_idx`;
TRUNCATE `catalog_product_index_eav_idx`;
TRUNCATE `catalog_product_index_price_bundle_idx`;
TRUNCATE `catalog_product_index_price_bundle_opt_idx`;
TRUNCATE `catalog_product_index_price_bundle_sel_idx`;
TRUNCATE `catalog_product_index_price_cfg_opt_agr_idx`;
TRUNCATE `catalog_product_index_price_cfg_opt_idx`;
TRUNCATE `catalog_product_index_price_downlod_idx`;
TRUNCATE `catalog_product_index_price_final_idx`;
TRUNCATE `catalog_product_index_price_idx`;
TRUNCATE `catalog_product_index_price_opt_agr_idx`;
TRUNCATE `catalog_product_index_price_opt_idx`;
TRUNCATE `cataloginventory_stock_status_idx`;
 
SET FOREIGN_KEY_CHECKS = 1; 

Remove Magento all 'Products' from Database


SET FOREIGN_KEY_CHECKS = 0; 

TRUNCATE TABLE `catalog_product_bundle_option`;
TRUNCATE TABLE `catalog_product_bundle_option_value`;
TRUNCATE TABLE `catalog_product_bundle_selection`;
TRUNCATE TABLE `catalog_product_entity_datetime`;
TRUNCATE TABLE `catalog_product_entity_decimal`;
TRUNCATE TABLE `catalog_product_entity_gallery`;
TRUNCATE TABLE `catalog_product_entity_int`;
TRUNCATE TABLE `catalog_product_entity_media_gallery`;
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`;
TRUNCATE TABLE `catalog_product_entity_text`;
TRUNCATE TABLE `catalog_product_entity_tier_price`;
TRUNCATE TABLE `catalog_product_entity_varchar`;
TRUNCATE TABLE `catalog_product_link`;
TRUNCATE TABLE `catalog_product_link_attribute`;
TRUNCATE TABLE `catalog_product_link_attribute_decimal`;
TRUNCATE TABLE `catalog_product_link_attribute_int`;
TRUNCATE TABLE `catalog_product_link_attribute_varchar`;
TRUNCATE TABLE `catalog_product_link_type`;
TRUNCATE TABLE `catalog_product_option`;
TRUNCATE TABLE `catalog_product_option_price`;
TRUNCATE TABLE `catalog_product_option_title`;
TRUNCATE TABLE `catalog_product_option_type_price`;
TRUNCATE TABLE `catalog_product_option_type_title`;
TRUNCATE TABLE `catalog_product_option_type_value`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;
TRUNCATE TABLE `catalog_product_super_attribute`;
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_category_product_index`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `rating_option_vote`; 
TRUNCATE TABLE `rating_option_vote_aggregated`;
TRUNCATE TABLE `review`; 
TRUNCATE TABLE `review_detail`; 
TRUNCATE TABLE `review_entity_summary`; 
TRUNCATE TABLE `review_store`;

INSERT  INTO `catalog_product_link_type`(`link_type_id`,`code`) VALUES (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell');
INSERT  INTO `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) VALUES (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal');
INSERT  INTO `cataloginventory_stock`(`stock_id`,`stock_name`) VALUES (1,'Default');
TRUNCATE TABLE `catalog_product_entity`;

SET FOREIGN_KEY_CHECKS = 1;

Remove Magento all 'Categories' from Database

SET FOREIGN_KEY_CHECKS = 0; 

TRUNCATE TABLE `catalog_category_entity`;
TRUNCATE TABLE `catalog_category_entity_datetime`;
TRUNCATE TABLE `catalog_category_entity_decimal`;
TRUNCATE TABLE `catalog_category_entity_int`;
TRUNCATE TABLE `catalog_category_entity_text`;
TRUNCATE TABLE `catalog_category_entity_varchar`;
TRUNCATE TABLE `catalog_category_product`;
TRUNCATE TABLE `catalog_category_product_index`;

INSERT  INTO `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`POSITION`,`level`,`children_count`) VALUES (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);
INSERT  INTO `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,32,0,2,1),(2,3,32,1,2,1);
INSERT  INTO `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) VALUES (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');

SET FOREIGN_KEY_CHECKS = 1;

Remove Magento all 'Customers' from Database

SET FOREIGN_KEY_CHECKS = 0; 

TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;

SET FOREIGN_KEY_CHECKS = 1;

Remove Magento all 'Orders' from Database

SET FOREIGN_KEY_CHECKS = 0; 

TRUNCATE `sales_order`;
TRUNCATE `sales_order_datetime`;
TRUNCATE `sales_order_decimal`;
TRUNCATE `sales_order_entity`;
TRUNCATE `sales_order_entity_datetime`;
TRUNCATE `sales_order_entity_decimal`;
TRUNCATE `sales_order_entity_int`;
TRUNCATE `sales_order_entity_text`;
TRUNCATE `sales_order_entity_varchar`;
TRUNCATE `sales_order_int`;
TRUNCATE `sales_order_text`;
TRUNCATE `sales_order_varchar`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_order_item`;

SET FOREIGN_KEY_CHECKS = 1;

Tuesday, May 10, 2016

Install ElasticSearch on Window(wamp) Server

Elasticsearch is an open-source search server based on Lucene. It provides a broadly-distributed, readily-scalable, multitenant-capable full-text search engine with an HTTP web interface as JSON schema documents. Elasticsearch can power extremely fast searches that support your data discovery applications.
ElasticSearch  is mostly use for searching functionality implementation first it's store huge data as local storage and than after give data according to our search as JSON format.It's easy to use so mostly people use this for faster search functionality.
For installation of ElasticSearch with PHP based application than it's some complicated for installation in window server. Here describe simple and basic steps of the installation process of ElasticSearch.
  • First download ElasticSearch latest version from Here
  • Than after download Zip or Tar file package.
  • First check in your system java is installed or not.for that open cmd and write `java -version` that display version if installed.
  • If not installed than find latest version of Java from Here and install into your system.
  • Set new "Environment Variables" path as 'JAVA_HOME' and path 'C:\Program Files\Java\jre1.8.0_77' (Where you install java).
  • Extract all files of ElasticSearch downloaded from site to system drive.
  • Now open 'elasticsearch.bat' file in 'C:\elasticsearch-2.3.2\bin\' directory.
  • If there is any error than it's display otherwise it's execute all process. 
  • After complete all process don't close it.Now open http://localhost:9200
  • It's display  ElasticSearch information.
For use ElasticSearch in PHP based application download from Here and follow steps describe there and also describe basic exampled for how to use.Install elastic search in PHP through Composer.
If you want to install  ElasticSearch on live server than follow Here.and must set `elasticsearch.yml` file(in C:\elasticsearch-2.3.2\config\ folder) variable according to server.

Saturday, May 7, 2016

Set Custom Order Increment id in Magento

Magento is most uses PHP based platform for e-commerce website. In Magento website adds products from admin panel and in front any user buy that product and after complete payment method or check out it's generated new order in the Magento database with unique order id.In default Magento order id(as increment id) starts from '100000001' and then after increment one for the next order.
Magento all orders saves in sales_flat_order table with unique entity_id as primary key. so you can get order information by order entity id or increment value. In order table entity_id is the primary key so you can't modify it. but you can change the order increment id that displays also in fronted.
When you want to develop a website and want to change the increment id that from a database you can change the order increment id prefix only. for that use below SQL query in your MySql command.


UPDATE `eav_entity_store` SET `increment_prefix` = 'NEWS-' WHERE `entity_store_id` = '1' AND `entity_type_id` = '5'

Here you can add own prefix in `increment_prefix` column.`entity_store_id` is your store id and `entity_type_id` is order type(different for invoice,shipment).
Sometimes you want to change the order increment id not only prefix or want to add a different prefix using some condition then you must change in core Magento files with creating a simple module as below.
First create new Newsinfo_Order.xml file in app/etc/modules/ as below.

<config>
  <modules>
    <newsinfo_order>
      <active>true</active>
      <codepool>local</codepool>
    </newsinfo_order>
  </modules>
</config>
 
Than create new config.xml file in app/code/local/Newsinfo/Order/etc/ as below.

<?xml version="1.0"?>
<config>
  <modules>
    <Newsinfo_Order>
      <version>1.0.0</version>
    </Newsinfo_Order>
  </modules>
  <global>
    <models>
      <sales>
        <rewrite>
          <order>Newsinfo_Order_Model_Order</order>
        </rewrite>
      </sales>
    </models>
  </global>
</config>
 
Here in below file just rewrite magento 'Mage_Sales_Model_Order' model with custom extension or you can directly changes in core file from app/code/core/Mage/Sales/Model/Order.php
Than after create new model file in app\code\local\Newsinfo\Order\Model named Order.php with below code. 

class Newsinfo_Order_Model_Order extends Mage_Sales_Model_Order 
{
 protected function _beforeSave()
 {
  parent::_beforeSave();
  $this->_checkState();
  $this->setIncrementId('NEWSINFO-'.$this->getIncrementId());
  return $this;
 }
}
Here below function you can change your order prefix and also add some condition for different prefix or dynamic prefix or order increment id.order increment id is unique value so here must use unique value otherwise order not created.

Add Layered Navigation to Advance Search Result Page in Magento


Magento Layered Navigation is used for search products according to categorize, price or different attribute which are assigned from Magento admin panel. So when users want to product by color, size than they can using Layered Navigation.
In Magento Layered Navigation display in left side of each category listing page if there are some products in category. It's also display in the left side column in search results page. Layered Navigation is auto generated according to a listing of products. if all products on listings attribute price and category are same then it's not displayed.
You can call layer navigation block on category listing page using catalog.xml file with below code.

<reference name="left">
  <block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
</reference>

That's define according to anchor and non anchor category in Magento.But it's not display in advance search results.so here describe simple way for display Layered Navigation in advance search result page by modifying core Magento file or you can rewrite that using custom module.
Change prepareProductCollection function as below in app/code/core/Mage/CatalogSearch/Model/Layer.php file.

public function prepareProductCollection($collection)
{
    if(Mage::helper('catalogsearch')->getQuery()->getQueryText())
    {
        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addSearchFilter(Mage::helper('catalogsearch')->getQuery()->getQueryText())
        ->setStore(Mage::app()->getStore())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addStoreFilter()
        ->addUrlRewrite();
    }
    else
    {
        $collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes());
        $attributes = Mage::getSingleton('catalog/product')->getAttributes();
        foreach($attributes as $attribute)
        {
            $attribute_code = $attribute->getAttributeCode();
            if($attribute_code == "price")
            continue;
            if (empty($_REQUEST[$attribute_code])){continue;}
            if(!empty($_REQUEST[$attribute_code]) && is_array($_REQUEST[$attribute_code]))
                $collection->addAttributeToFilter($attribute_code, array('in' => $_REQUEST[$attribute_code]));
            else
            if(!empty($_REQUEST[$attribute_code]))
            $collection->addAttributeToFilter($attribute_code, array('like' => "%" . $_REQUEST[$attribute_code] . "%"));
        }
        $collection->setStore(Mage::app()->getStore())
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addStoreFilter()
        ->addUrlRewrite();
        Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection($collection);    
    }
    Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
    Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
    return $this;
}

Than after change getProductCollection and getSearchCriterias function as below in app/code/core/Mage/CatalogSearch/Model/Advanced.php file.

public function getProductCollection()
{
    if (is_null($this->_productCollection)) {
        $this->_productCollection = Mage::getResourceModel('catalogsearch/advanced_collection')
        ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
        ->addMinimalPrice()
        ->addStoreFilter();
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($this->_productCollection);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($this->_productCollection);
        if(isset($_GET['cat']) && is_numeric($_GET['cat']))
        $this->_productCollection->addCategoryFilter(Mage::getModel('catalog/category')->load($_GET['cat']),true);
    }
    return $this->_productCollection;
}
public function getSearchCriterias()
{
    $search = $this->_searchCriterias;
    if(isset($_GET['cat']) && is_numeric($_GET['cat'])){
        $category = Mage::getModel('catalog/category')->load($_GET['cat']);
        $search[] = array('name'=>'Category','value'=>$category->getName());
    }
    return $search;
}

Here in below layer navigation is prepared for advance search product collection now must display that layer navigation using theme xml file so edit your theme's catalogsearch.xml file and add below code in <catalogsearch_advanced_result> tag.
 
<reference name="left">
  <block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml"/>
</reference>
 
here add that navigation in left sidebar you can put in content or right side according to theme content or template.