You are on page 1of 16

TI LIU HNG VIT MODULE V WEBSERVICE CHO MAGENTO 1.

7 Ti liu k thut 06/2012

Lch s chnh sa
Ngy thng 06/2012 Version 1.7 M t Ngi vit DVMS

Trang 2 /16

MC LC

Trang 3 /16

1.

To module:

Mt module s bao gm mt trong cc thnh phn sau :


Settings Database schemas Rendering objects Utility helpers Data models Action controllers

Cc module c th c xc nh l ON hay OFF thng qua file XML configuration ti th mc app/etc/modules/ directory. Mi module c file config cuar ring n ti th mc etc/ Ci t Module creator extension: Vo link sau download Module creator v: http://www.magentocommerce.com/wiki/_media/modulecreator0.0.9.1.zip => Gii nn vo thu mc cha source Magento ta c th mc "moduleCreator". => Tip n m trnh duyt ln v chy link: http://www.your_site.com/moduleCreator/index.php s thy giao din ging nh bn di:

Trang 4 /16

=> nhp cc thng tin v module ca bnV trong folder code s sinh ra th mc [root]/moduleCreator Ch : phn "Magento Root Directory" nu khng nhp g -> th code s c to trong [root]/moduleCreator/new. Da vo giao din ny s in thng tin to ra module News (in thng tin Namespace: Packt, Module: News , Design: default, Design: default - phn ny code design s nm trong folder default/default) => nhn Create to. Lc ny mt lot folder c sinh ra ging nh hnh bn di:

=> Sau khi to xong bn vo copy ton b source mi to ra trong th mc: new/app vo th mc app ca Magento. Khi trong giao din admin ta s thy c mt menu mi => click vo menu ny ta s c giao din c bn nh sau:

Trang 5 /16

=> By gi gii quyt trng tm chnh l Controller. Controller gi vai tr trng tm y, n c nhim v chnh l map mt URL vi function x l logic cho n. Vo folder "app/code/local/ tn Namespace mi to/tn module mi to/controllers" edit file IndexController.php" thay i logic business ca n. V d: module ti to c Namespace l: NguyenLinh v tn module l: Example th ti vo sa file: app/code/local/NguyenLinh/Example/controllers/IndexController.php

Nu bn mun to module bng tay m khng dng module Creator th c th tham kho cch lm ti link sau: http://www.webspeaks.in/2010/08/create-your-first-adminbackend-module.html http://www.webspeaks.in/2010/07/create-your-first-magento-module.html 2. To thm webservice: - Cu trc c bn ca mt module API nh sau:
app/code/ |- community/ or local/ or core/ | |- COMPANYNAME/ | |- MODULENAME/ | |- etc/ | |- api.xml | |- config.xml | |- Helper/

Trang 6 /16

| |- Data.php | |- Model/ | |- OBJECT1/ | |- Api.php | |- Object2/ | |- Api.php |- etc/modules/ |- COMPANYNAME_MODULE.xml

- v d ti to API danh sch cc sn phm bn chy, cu trc module ca ti nh sau:


app/ |- code/community/ | |- GrelaDesign/ | |- Tutorial/ | |- etc/ | |- api.xml | |- config.xml | |- Helper/ | |- Data.php | |- Model/ | |- Core/ | |- Api.php | |- DashBoard/ | |- Api.php |- etc/modules/ |- GrelaDesign_Tutorial.xml

- GrelaDesign_Tutorial.xml: Tp tin ny ch th cho Magento tm thy API

<!-- GrelaDesign_Tutorial.xml --> <config> <modules> <GrelaDesign_Tutorial> <active>true</active> <codePool>community</codePool><!-- this could be also 'local' --> <depends> <Mage_Api /> </depends> </GrelaDesign_Tutorial> </modules> </config> - config.xml: tp tin cu hnh module: <!-- config.xml -->

Trang 7 /16

<config> <modules> <GrelaDesign_Tutorial><!-- COMPANYNAME_MODULE --> <version>0.0.0.1</version> </GrelaDesign_Tutorial> </modules> <global> <models> <tutorial><!-- MODULE, just first letter is lowercased, i.e. module CustomAPI would be placed here as customAPI --> <class>GrelaDesign_Tutorial_Model</class> </tutorial> </models> <helpers> <tutorial> <class>GrelaDesign_Tutorial_Helper</class> </tutorial> </helpers> </global> </config>
Trong hng dn ny, s to ra ba phng thc cho API. u tin s l getVersion trong Core, th hai s l getBestsellers v getTotals th ba cng c trong DashBoard. Trong v d ny s c 2 nhm truy cp: "allaccess" v "owneracess".

<!-- api.xml --> <config> <api> <resources> <tutorial_core translate="title" module="tutorial"> <title>GrelaDesign tutorial Core API calls</title>

Trang 8 /16

<model>tutorial/core_api</model> <acl>greladesign/tutorial</acl> <methods> <getVersion translate="title" module="tutorial"> <title>Returns version of this API</title> <acl>greladesign/tutorial/allaccess</acl> </getVersion> </methods> </tutorial_core> <tutorial_dashboard translate="title" module="tutorial"> <title>GrelaDesign tutorial DashBoard app API calls</title> <model>tutorial/dashBoard_api</model> <acl>greladesign/tutorial</acl> <methods> <getSalesTotals translate="title" module="tutorial"> <title>Get sales totals</title> <method>getTotals</method><!-- here we specify the method name if we have a conflict with built in method or we want to change the name i.e. because it is lengthy --> <acl>greladesign/tutorial/owneraccess</acl> </getSalesTotals> <getBestsellers translate="title" module="tutorial"> <title>Get best selling product list</title> <acl>greladesign/tutorial/allaccess</acl> </getBestsellers> </methods> </tutorial_dashboard> </resources>

Trang 9 /16

<resources_alias><!-- here we can put aliases, shortened calls for our api resource, I haven't checked how alias behaves when it collides with different resource... --> <dashboard>tutorial_dashboard</dashboard> <core>tutorial_core</core> </resources_alias> <acl><!-- Access Control List to our resources, this tree structure is displayed in "Resource Roles" panel when you open role to edit --> <resources> <greladesign translate="title" module="tutorial"> <title>GrelaDesign</title> <sort_order>100</sort_order> <tutorial translate="title" module="tutorial"> <title>Tutorial</title> <sort_order>100</sort_order> <allaccess translate="title" module="tutorial"> <title>Core functionality required by all users.</title> <sort_order>10</sort_order> </allaccess> <owneraccess translate="title" module="tutorial"> <title>Functions accessible only for owner.</title> <sort_order>50</sort_order> </owneraccess> </tutorial> </greladesign> </resources> </acl> </api>

Trang 10 /16

</config>

<!-- app/code/community/GrelaDesign/Tutorial/Model/Core/Api.php --> <?php class GrelaDesign_Tutorial_Model_Core_Api { /** * Returns version of the installed magento * @return String */ public function getVersion() { return Mage::getVersion(); } } ?>

<!-- app/code/community/GrelaDesign/Tutorial/Model/Core/Api.php --> <?php class GrelaDesign_Tutorial_Model_Core_Api { /** * Returns version of the installed magento * @return String */ public function getVersion() { return Mage::getVersion();

Trang 11 /16

} } ?>

<!-- app/code/community/GrelaDesign/Tutorial/Model/DashBoard/Api.php --> <?php class GrelaDesign_Tutorial_Model_DashBoard_Api { /** * Returns list of best selling products, returned list is limited to the number items specified in argument. * @param int $limit */ public function getBestsellers($limit=5) { //filter $visibility = array( Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG ); $_arrayOfBestsellers = Mage::getResourceModel('reports/product_collection') ->addAttributeToSelect('*') ->addOrderedQty() >addAttributeToFilter('visibility', $visibility) ->setOrder('ordered_qty', 'desc') ->getSelect()->limit($limit)>query(); $bestProducts = array(); $details = Mage::getModel('catalog/product'); foreach ($_arrayOfBestsellers as $product) { $details->load($product['entity_id']); $bestProduct[] = array( "qty" => $product["ordered_qty"] , "price" => $details->getPrice() , "name" => $details->getName() ); } return $bestProduct; }

Trang 12 /16

/** * Get sales totals * Returns totals for : Revenue, Tax, Shipping and Quantity for given time range, default is last 24 hours. * @param String $period - default '24h', possible values are: 24h, ,1y, 2y */ public function getTotals($period='24h') { $collection = Mage::getResourceModel('reports/order_collection') ->addCreateAtPeriodFilter($period) //->getSelectCountSql() ->calculateTotals(1) ; /* $collection->addFieldToFilter('store_id', array('eq' => Mage::app()>getStore(Mage_Core_Model_Store::ADMIN_CODE)->getId()) ); */ $collection->load(); $totals = $collection->getFirstItem(); return array( 'Revenue' => $totals->getRevenue() , 'Tax' => $totals->getTax() , 'Shipping' => $totals->getShipping() , 'Quantity' => $totals->getQuantity() * 1 ); } } ?>
<!-- app/code/community/GrelaDesign/Tutorial/Helper/Data.php --> <?php class GrelaDesign_Tutorial_Helper_Data extends Mage_Core_Helper_Abstract { } ?>

- gi ta vo admin ca website v to ra cc user test th API

Trang 13 /16

rule: owner user: admin pass: 1admin rule: client user: johndoe pass: j0hn

- To ra tp tin test c ni dung nh sau: <?php /* Create a file called, 'test.php' in the root of your Magento development environment. Copy the code below into your test.php file. Browse to http://yourstore/test.php. */

// Tell this file to use the Mage libraries $mageFilename = 'app/Mage.php'; require_once $mageFilename; umask(0);

// Note we are using the 'app' directive not the 'run' directive here // Also note the store is named 'default' by, well, default. But as Zeke pointed out if your store is // 'en' then you would need to edit the following line to say Mage::app('en') // Let's go with 'default' for simplicity ... Mage::app('default');

// Your test code goes here

echo "<pre>";

$client = new Zend_XmlRpc_Client('http://127.0.0.1/magento/api/xmlrpc/');

Trang 14 /16

// If some stuff requires api authentication,

// we should get session token //login $session = $client->call('login', array('admin', '1admin'));

echo var_dump($client->call('call', array($session, 'tutorial_dashboard.getBestsellers'))), "\n"; echo var_dump($client->call('call', array($session, 'dashboard.getBestsellers'))), "\n"; echo var_dump($client->call('call', array($session, 'tutorial_dashboard.getSalesTotals'))), "\n"; echo var_dump($client->call('call', array($session, 'dashboard.getSalesTotals'))), "\n";

echo var_dump($client->call('call', array($session, 'tutorial_core.getVersion'))), "\n"; echo var_dump($client->call('call', array($session, 'core.getVersion'))), "\n"; //logout echo $client->call('endSession', array($session)), "\n";

//login $session = $client->call('login', array('johndoe', 'j0hn'));

echo var_dump($client->call('call', array($session, 'tutorial_dashboard.getBestsellers'))), "\n"; echo var_dump($client->call('call', array($session, 'dashboard.getBestsellers'))), "\n"; try { //this will throw an exception ("access denied") echo var_dump($client->call('call', array($session, 'tutorial_dashboard.getSalesTotals'))), "\n"; echo var_dump($client->call('call', array($session, 'dashboard.getSalesTotals'))), "\n"; } catch (Exception $e) { echo $e->getMessage(), "\n"; }

Trang 15 /16

echo var_dump($client->call('call', array($session, 'tutorial_core.getVersion'))), "\n"; echo var_dump($client->call('call', array($session, 'core.getVersion'))), "\n"; //logout echo $client->call('endSession', array($session)), "\n";

echo "</pre>";

?>

Ti liu tham kho: http://www.magentocommerce.com/api/soap/introduction.html

CHC THNH CNG!

THNG TIN LIN H H TR

DVMS (08) 360 289 37 info@dvms.vn www.DVMS.vn

Trang 16 /16