HTML/JavaScript

Thursday, February 11, 2016

Magento: Delete All Products & Categories via SQL

FOR PRODUCTS

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`;
TRUNCATE TABLE `catalog_product_super_attribute_label`;
TRUNCATE TABLE `catalog_product_super_attribute_pricing`;FOR CATEGORY
TRUNCATE TABLE `catalog_product_super_link`;
TRUNCATE TABLE `catalog_product_enabled_index`;
TRUNCATE TABLE `catalog_product_website`;
TRUNCATE TABLE `catalog_product_entity`;
TRUNCATE TABLE `cataloginventory_stock`;
TRUNCATE TABLE `cataloginventory_stock_item`;
TRUNCATE TABLE `cataloginventory_stock_status`;
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');




FOR CATEGORY
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;

Tuesday, February 9, 2016

Get Order Details from Order ID in Magento

In Magento quite often you need to get order details from order ID. So in this tutorial I am going to show you how to get order details from order ID. There are two types of order ID’s, one is real order ID and the other is order increment ID. So first I will let you know how to get real order ID from order increment ID.

To Get real order ID from order increment id in Magento:
let’s assume your order increment id is #10001222 and you need to get real order ID, you can use following code:

$orderIncrementId = 10001222;
$orderId = Mage::getModel('sales/order')
             ->loadByIncrementId($orderIncrementId)
             ->getEntityId();

You can get order details by order increment ID as follow:
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

If you know the real order ID you can get order details by using following code:
$order = Mage::getModel("sales/order")->load($orderId);

To get order total value:
$orderTotalValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');

To get order items collection:

$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item){

    $product_id = $item->product_id;
    $product_sku = $item->sku;
    $product_price = $item->getPrice();
    $product_name = $item->getName();
    $_product = Mage::getModel('catalog/product')->load($product_id);
    $cats = $_product->getCategoryIds();
    $category_id = $cats[0]; // just get the first id
    $category = Mage::getModel('catalog/category')->load($category_id);
    $category_name = $category->getName();
}

To get shipping method from order:
$shipping_method = $order->getShippingMethod();

To get payment method code from order you can use following code:
$payment_method_code = $order->getPayment()->getMethodInstance()->getCode();

I hope this tutorial will help you to get order details from order ID in Magento.

Tuesday, January 12, 2016

Get Filterable attribute by Category ID

<?php
$category_ids = $this->getCategoryid(); // HERE YOU CAN PUT THE STATIC CATEGORY ID

$layer = Mage::getModel("catalog/layer");
    $categoryid = $category_ids;
    $category = Mage::getModel("catalog/category")->load($categoryid);
    $layer->setCurrentCategory($category);
    $attributes = $layer->getFilterableAttributes();

    foreach ($attributes as $attribute) {
        if ($attribute->getAttributeCode() != 'price') {
           
            $filterBlockName = 'catalog/layer_filter_attribute';
       
        echo '<br/>---'.$attribute->getFrontendLabel();
        echo '---<br/>';
        $result = $this->getLayout()->createBlock($filterBlockName)->setLayer($layer)
->setAttributeModel($attribute)->init();
       
        foreach($result->getItems() as $option) {
            echo $option->getLabel().'<br/>';
            echo $option->getValue();
        }
        }
}

?>

Thursday, January 7, 2016

How to override controller in magento/Override controller’s action in magento

Some times you may run into situation where you want to override the core functionality of Magento core controllers.

So in this case, The best practise is override the controller then override actions or add new actions for custom requirement.

For example you want to override OnepageController.php file and overwride indexAction.

In this example my custom module name space is “MyBlog” and i am going to overwride indexAction

Mage_Checkout_OnepageController


Base File Path : app/code/core/Mage/Checkout/controllers/OnepageController.php
To Do the same we have to follow bellow steps.

Step1: We need to create a custom module


File Path : app/etc/modules/
File Name: MyBlog_Checkout.xml
File Content:

<?xml version="1.0"?>
<config>
<modules>
<MyBlog_Checkout>
<active>true</active>
<codePool>local</codePool>
</MyBlog_Checkout>
</modules>
</config>
Step2: Now we have to create bellow files in our custom module


File Path: app/code/local/MyBlog/Checkout/etc/config.xml
Bellow is content for config.xml file

<?xml version="1.0"?>
<config>
<modules>
<MyBlog_Checkout>
<version>0.0.1</version>
</MyBlog_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyBlog_Checkout before="Mage_Checkout">MyBlog_Checkout</MyBlog_Checkout>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Bellow is content for OnepageController.php


File Path : app/code/local/MyBlog/Checkout/controllers/OnepageController.php

<?php
require_once 'Mage/Checkout/controllers/OnepageController.php';

class MyBlog_Checkout_OnepageController extends Mage_Checkout_OnepageController {

public function indexAction()
{
echo "Great! You are in MyBlog checkout controller section";
exit;
}
}
?>

Wednesday, December 30, 2015

Fixing Magento Catalog Price Rules Issue

try {
Mage::getConfig()->init()->loadEventObservers('crontab');
Mage::app()->addEventArea('crontab');
Mage::dispatchEvent('default');
$observ = Mage::getModel('catalogrule/observer');
$observ->dailyCatalogUpdate("0 1 * * *");
} catch (Exception $e) {
Mage::printException($e);
}

Custom column layout

Learn how easy it is to create a custom column layout for Magento. The tutorial is based on the Magento Enterprise Edition but also applies to the Magento Community Edition. A custom column layout is helpful if you want for instance a "left" column to only appear on certain pages.

To create a new custom column layout, you need to place a new file in the Magento theme. With the Magento default theme, you could use the folder app/design/frontend/default/default/template/page, but of course - when building your own site - it is recommended to have your own theme-directory.

In the tutorial, the file 1column.phtml is copied to the file 1columnfp.phtml (where "fp" stands for the front page). To make this new page reckognizable you wll need to add a new XML-definition. In the tutorial the following code is added to the file app/code/core/Mage/Page/etc/config.xml this needs to be copied into app/code/local/Mage/Page/etc/config.xml


<one_column_fp module="page" translate="label">
    <label>1 column (frontpage)</label>
    <template>page/1columnfp.phtml</template>
    <layout_handle>page_one_column_fp</layout_handle>
</one_column_fp>

However, with a Magento upgrade this change could be gone. The same XML could also be added to your app/etc/local.xml file instead:

<config>
    <global>
        <page>
            <layouts>
                <one_column_fp module="page" translate="label">
                    <label>1 column (frontpage)</label>
                    <template>page/1columnfp.phtml</template>
                    <layout_handle>page_one_column_fp</layout_handle>
                </one_column_fp>
            </layouts>
        </page>
    </global>
</config>

Monday, December 28, 2015

Remove maintenance mode in magento 2

Removing Maintenance mode in Magento 2 is extremely simple as in previous Magento versions.

The .maintenance.flag file is located under var folder in Magento 2. Delete this file to remove the maintenance mode.