Category Archives: Magento

How to configure Magento REST and oAuth settings

Configuring Magento REST and oAuth settings for REST access • Inchoo

How to configure Magento REST and oAuth settings

magento_rest_admin_config

winchild@sxc.hu

(This article is for Magento 1.7.0 and greater . Older Magento versions does not have implemented these features.)

In my last article, I wrote about the REST and oAuth to explain the terms that are used in Magento admin area for Magento REST configuration.

This article will show the steps necessary in order to be able to consume REST services provided by Magento:

 

     

  1. Creating oAuth Consumer
  2. Creating and configuring Admin roles
  3. Assigning configured Admin REST Role to admin user
  4. Configuring resource attributes and access permissions
  5.  

     

If you didn’t read my previous article about Magento REST and oAuth, I strongly suggest you to do so and then return here again.

Let’s Log-in into our Magento admin dashboard before start.

1. Creating oAuth Consumer

Since Magento REST service is working only with oAuth authentication, we need to create oAuth consumer application first.

Navigate to System->Web Services ->REST oAuth Consumers. OAuth Consumers grid is shown. We can register any number of Consumers in Magento that can be used by various third party oAuth clients to access our Magento resources.

  1. Click on “Add New” button to add new Consumer Application.
  2. On “New Consumer” screen insert some custom name.
  3. Key and Secret fields are disabled, and we just need to copy their values somewhere in text file for later usage (We need them for our oAuth authorization in order to be able to consume REST from Magento).
  4. We can leave the Callback URL and Rejected Callback URL fields empty for now.
  5. Save Consumer app.

NewConsumer

2. Creating and configuring Admin roles

Like I mentioned in last article about this topic, we need to create permissions for specific user type in order user to be able to consume our Magento REST service and access necessary resources.

Let’s navigate to System -> Web Services -> REST Roles. REST Roles grid is shown with two user types by default:

  • Customer
  • Guest

Let’s say, for example, that we are going to use the Magento REST service for updating Products and Customers, and we need Admin permissions for that. We don’t want to enable neither Guest or Customer user to be able to do that.

Let’s create new Admin role.

  1. Click on “Add admin Role” button in top right corner. “Add new Role” screen is shown.
  2. In the field “Role name” enter for example “Administrator”.
  3. Click on “Role API resources” tab on the left.

    Role Resources” screen is shown and here we need to check specific permissions that our Administrator user will have on specific resource. Of course, we have an option to select “All”, but be careful with that.

  4. Click on “Save Role” button in top right corner of the screen.

AddNewRole

AddNewRole2

3. Configuring resource attributes and access permissions

Navigate to System->Web Services ->REST attributes.

Here we have grid with three user types listed:

     

  • Admin
  • Customer
  • Guest
  •  

     

  1. Let’s click on “Admin” in order to configure REST resource attributes that Admin will be allowed to access.
  2. Under “User type resources” screen, select resources that Admin user type can access or select “All“.
  3. Click on “Save” button in the top-right corner.

Edit_Attributes

4. Assigning configured Admin REST Role to existing admin user

Ok, we configured everything to be able to use Magento REST services. Or not?

Hey, we did everything to configure resources for Admin user type, but we didn’t assign any user to this roles.

  1. Navigate to System->Permissions->Users. “Users” grid is shown with list of registered Magento site Administrators
  2. Click on some admin user from list in order to open “Edit user” screen.
  3. There is a tab named “REST role” on the left. Click on it and a list of Admin type roles is shown on the screen.
  4. Click on “Assigned” radio box near that role name.
  5. Save User.
  6. You have successfully assigned the admin user to be able to access REST resources on our Magento.

Edit_Attributes

EditUserAdmin

In some next articles I will describe how to consume REST services from PHP and authenticate using Zend_OAuth_Consumer.

Cheers :-)

How to create a custom grid from scratch

How to create a custom grid from scratch • Inchoo

How to create a custom grid from scratch

inchoo orders

We received a request from client, they wanted expanded order grid but still keep the default magento’s grid. For this example we’ll create new module named Inchoo Orders.

 

Step 1.

Create new module in etc/modules called Inchoo_Orders.xml

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

Step 2.

Next thing we’ll do is create folder Inchoo/Orders inside app/code/community and inside we’ll make few folders: Block, controllers, etc, Helper.

inchoo_orders

Step 3.

Create config.xml inside etc folder.

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
<?xml version="1.0"?>
<config>
    <modules>
        <Inchoo_Orders>
            <version>0.0.0.1</version>
        </Inchoo_Orders>
    </modules>
    <global>
        <models>
            <inchoo_orders>
                <class>Inchoo_Orders_Model</class>
                <resourceModel>inchoo_orders_resource</resourceModel>
            </inchoo_orders>
        </models>
        <resources>
            <inchoo_orders_setup>
                <setup>
                    <module>Inchoo_Orders</module>
                </setup>
            </inchoo_orders_setup>
        </resources>
        <blocks>
            <inchoo_orders>
                <class>Inchoo_Orders_Block</class>
            </inchoo_orders>
        </blocks>
        <helpers>
            <inchoo_orders>
                <class>Inchoo_Orders_Helper</class>
            </inchoo_orders>
        </helpers>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <inchoo_orders before="Mage_Adminhtml">Inchoo_Orders_Adminhtml</inchoo_orders>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

Step 4

Create adminhtml.xml file inside etc folder which will add a link to our orders page in magento admin panel.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>
<config>
    <menu>
        <sales>
            <children>
                <inchoo_orders translate="title" module="inchoo_orders">
                    <sort_order>10</sort_order>
                    <title>Orders - Inchoo</title>
                    <action>adminhtml/order/</action>
                </inchoo_orders>
            </children>
        </sales>
    </menu>
</config>

Step 5

Create blank helper class.

1
2
3
4
<?php
class Inchoo_Orders_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Step 6

Next step is to create controller for our grid.

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
<?php
class Inchoo_Orders_Adminhtml_OrderController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->_title($this->__('Sales'))->_title($this->__('Orders Inchoo'));
        $this->loadLayout();
        $this->_setActiveMenu('sales/sales');
        $this->_addContent($this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order'));
        $this->renderLayout();
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->getResponse()->setBody(
            $this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order_grid')->toHtml()
        );
    }
    public function exportInchooCsvAction()
    {
        $fileName = 'orders_inchoo.csv';
        $grid = $this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getCsvFile());
    }
    public function exportInchooExcelAction()
    {
        $fileName = 'orders_inchoo.xml';
        $grid = $this->getLayout()->createBlock('inchoo_orders/adminhtml_sales_order_grid');
        $this->_prepareDownloadResponse($fileName, $grid->getExcelFile($fileName));
    }
}

Step 7

Next thing we do is create grid container in Block/Adminhtml/Sales/Order.php

1
2
3
4
5
6
7
8
9
10
11
12
<?php
class Inchoo_Orders_Block_Adminhtml_Sales_Order extends Mage_Adminhtml_Block_Widget_Grid_Container
{
    public function __construct()
    {
        $this->_blockGroup = 'inchoo_orders';
        $this->_controller = 'adminhtml_sales_order';
        $this->_headerText = Mage::helper('inchoo_orders')->__('Orders - Inchoo');
        parent::__construct();
        $this->_removeButton('add');
    }
}

Step 8

Last step is making grid class in Block/Adminhtml/Sales/Order/Grid.php

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
class Inchoo_Orders_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('inchoo_order_grid');
        $this->setDefaultSort('increment_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
    protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('sales/order_collection')
            ->join(array('a' => 'sales/order_address'), 'main_table.entity_id = a.parent_id AND a.address_type != \'billing\'', array(
                'city'       => 'city',
                'country_id' => 'country_id'
            ))
            ->join(array('c' => 'customer/customer_group'), 'main_table.customer_group_id = c.customer_group_id', array(
                'customer_group_code' => 'customer_group_code'
            ))
            ->addExpressionFieldToSelect(
                'fullname',
                'CONCAT({{customer_firstname}}, \' \', {{customer_lastname}})',
                array('customer_firstname' => 'main_table.customer_firstname', 'customer_lastname' => 'main_table.customer_lastname'))
            ->addExpressionFieldToSelect(
                'products',
                '(SELECT GROUP_CONCAT(\' \', x.name)
                    FROM sales_flat_order_item x
                    WHERE {{entity_id}} = x.order_id
                        AND x.product_type != \'configurable\')',
                array('entity_id' => 'main_table.entity_id')
            )
        ;
        $this->setCollection($collection);
        parent::_prepareCollection();
        return $this;
    }
    protected function _prepareColumns()
    {
        $helper = Mage::helper('inchoo_orders');
        $currency = (string) Mage::getStoreConfig(Mage_Directory_Model_Currency::XML_PATH_CURRENCY_BASE);
        $this->addColumn('increment_id', array(
            'header' => $helper->__('Order #'),
            'index'  => 'increment_id'
        ));
        $this->addColumn('purchased_on', array(
            'header' => $helper->__('Purchased On'),
            'type'   => 'datetime',
            'index'  => 'created_at'
        ));
        $this->addColumn('products', array(
            'header'       => $helper->__('Products Purchased'),
            'index'        => 'products',
            'filter_index' => '(SELECT GROUP_CONCAT(\' \', x.name) FROM sales_flat_order_item x WHERE main_table.entity_id = x.order_id AND x.product_type != \'configurable\')'
        ));
        $this->addColumn('fullname', array(
            'header'       => $helper->__('Name'),
            'index'        => 'fullname',
            'filter_index' => 'CONCAT(customer_firstname, \' \', customer_lastname)'
        ));
        $this->addColumn('city', array(
            'header' => $helper->__('City'),
            'index'  => 'city'
        ));
        $this->addColumn('country', array(
            'header'   => $helper->__('Country'),
            'index'    => 'country_id',
            'renderer' => 'adminhtml/widget_grid_column_renderer_country'
        ));
        $this->addColumn('customer_group', array(
            'header' => $helper->__('Customer Group'),
            'index'  => 'customer_group_code'
        ));
        $this->addColumn('grand_total', array(
            'header'        => $helper->__('Grand Total'),
            'index'         => 'grand_total',
            'type'          => 'currency',
            'currency_code' => $currency
        ));
        $this->addColumn('shipping_method', array(
            'header' => $helper->__('Shipping Method'),
            'index'  => 'shipping_description'
        ));
        $this->addColumn('order_status', array(
            'header'  => $helper->__('Status'),
            'index'   => 'status',
            'type'    => 'options',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));
        $this->addExportType('*/*/exportInchooCsv', $helper->__('CSV'));
        $this->addExportType('*/*/exportInchooExcel', $helper->__('Excel XML'));
        return parent::_prepareColumns();
    }
    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }
}

Our new custom orders page can be accessed by going to Sales -> Orders – Inchoo in admin panel.

link in admin

End result should be something like this:

inchoo orders grid

Create unique coupon code in Magento

Create unique coupon code in Magento • Inchoo

Toni from ICG wrote a post how to create simple A/B split testing for offline promotions for online store. Now we will show you how to manage coupon codes trough Magento and prepare for your next “offline” campaign.

Under Magento administration, first go to System -> Configuration -> Customer -> Promotions and create initial settings which will be used later for generating coupon codes, this can be changed later on creating new Shopping Cart Price Rules. Consider this as a starting template.

Basic Settings

Now we will create new Shopping Cart Price Rules using Toni’s idea.

Lets say you’re giving a 10% discount to customers that buy your products in December using a code for example “December10?. Lets include different promo codes in catalog version A and version B. So the A would have a promo code “December10a” and B would have “December10b”.

To build on his idea, lets say we wanna track every catalog individually, how would we do that?

Go to Promotions -> Shopping Cart Price Rules -> Add New Rule.

Now we will use Specific Coupon -> Use Auto Generation.

Hit Save and Continue Edit, now under Conditions we will leaved blank to apply on all products, now under Actions, we will give 10% of product price discount.

Now we need to generate Coupon Codes. Go to Manage Coupon Codes. There is few basic settings, Coupon quantity, code format etc. After we have made desired adjustments, hit Generate button.

Now we have list of 20 unique coupon codes, which we can export to CSV or Excel XML format. Each coupon code we can use on Catalog version A and Catalog version B.

Ok, we will use one coupon code to create test order and see how it will apply on shopping cart.

Now we will create report regarding to this promotion. Reports -> Sales -> Coupons -> and now we can create custom report regarding to coupon usage.

There are numerous way to engage potential customers using custom generated codes. For example you are going to business meeting and you  simply print 20 flyers with unique coupon codes and spread out as a gift after successful  meeting :-).