• Magento 1.6.2 API Access Role - Bug

    Posted on December 22, 2012 by Nimrod Techn

    Granting API custom/full access role in Magento 1.6.2 (Web Services > Roles) simply doesn't work.
    How to we fix it?

    Copy app/code/core/Mage/AdminHtml/Block/Api/Tab/RolesEdit.php

    To: app/code/local/Mage/AdminHtml/Block/Api/Tab/RolesEdit.php

     

    Inside, place the following:

    class Mage_Adminhtml_Block_Api_Tab_Rolesedit extends Mage_Adminhtml_Block_Widget_Form {
    
        public function __construct() {
            parent::__construct();
    
            $rid = Mage::app()->getRequest()->getParam('rid', false);
    
            $resources = Mage::getModel('api/roles')->getResourcesList();
    
            $rules_set = Mage::getResourceModel('api/rules_collection')->getByRoles($rid)->load();
    
            $selrids = array();
    
            foreach ($rules_set->getItems() as $item) {
    			if (array_key_exists(strtolower($item->getResource_id()), $resources)
                               && $item->getApiPermission() == 'allow') {
                    $resources[$item->getResource_id()]['checked'] = true;
                    array_push($selrids, $item->getResource_id());
                }
            }
    
            $this->setSelectedResources($selrids);
    
            $this->setTemplate('api/rolesedit.phtml');
        }
    }

    This post was posted in Working With Magento Collections

  • When Magento's API doesn't work properly...

    Posted on December 22, 2012 by Nimrod Techn

    Write your own "API":

    1. app/code/local/Engineering/Extapi/controllers/ExtapiController.php

    <?php
    
    class Engineering_Extapi_ExtapiController extends Mage_Core_Controller_Front_Action
    {
    
    	protected function _initAction() {
    		return $this;
    	}
    
    	public function indexAction() {
    		$this->loadLayout();
    	}
    
    	public function getnumberofordersAction()
    	{
    		if($this->getRequest()->getParam('pass')=="123")
    		{ //check password
    			$query = "SELECT count(*) FROM `sales_flat_order`";
    
    			$resource = Mage::getSingleton('core/resource');
    			$readConnection = $resource->getConnection('core_read');
    			$result = $readConnection->fetchOne($query);	
    
    			echo $result;
    		}
    	}
    
    	public function getnumberofcustomersAction() {
    		if($this->getRequest()->getParam('pass')=="123")
    		{ //check password
    			$query = "SELECT count(DISTINCT email) FROM customer_entity";
    
    			$resource = Mage::getSingleton('core/resource');
    			$readConnection = $resource->getConnection('core_read');
    			$result = $readConnection->fetchOne($query);		
    
    			echo $result;
    		}
    	}
    
    }

    2. app/code/local/Engineering/Extapi/etc/config.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Engineering_Extapi>
                <version>0.1.0</version>
            </Engineering_Extapi>
        </modules>
    
        <frontend>
            <routers>
    			<extapi_router>
    				<use>standard</use>
    				<args>
    					<module>Engineering_Extapi</module>
    					<frontName>extapi</frontName> <!-- URI -->
    				</args>
    			</extapi_router>
            </routers>
        </frontend>
    </config>
    
    Now - Don't forget to create the app/etc/modules/Engineering_Extapi.xml file:
    <?xml version="1.0"?>
    <config>
        <modules>
    	    <Engineering_Extapi>
    	       <active>true</active>
                   <codePool>local</codePool>
    	    </Engineering_Extapi>
        </modules>
    </config>

    Now you can reach the actions via:
    http://yoursite.com/extapi/getnumberoforders/?pass=123
    http://yoursite.com/extapi/getnumberofcustomers/?pass=123


    This post was posted in Writing a Magento Extension

  • Send a transactional email in Magento

    Posted on December 22, 2012 by Nimrod Techn

    Sometimes we would like to send a customized transactional email right after a transaction has been made.
    How would we do it? Simple. Create an Observer and hook it up with Magento's checkout -success event (checkout_onepage_controller_success_action).

    Let's create and extension dir (Transemail) with 2 files in it:

    1. app/code/local/Engineering/Transemail/Model/Observer.php:
    <?php
    class Engineering_Transemail_Model_Observer extends Mage_Core_Model_Abstract {
        public function sendMail($observer)
        {
    		$event = $observer->getEvent();
    		if(!$order = $event->getOrder())
    		{
    			$order_Id = Mage::getSingleton('checkout/type_onepage')->getCheckout()
                                      ->getLastOrderId();
    			$order = Mage::getModel('sales/order')->load($order_Id);
    		}
    		$payment = $order->getPayment()->getMethod();
    
    			$loginfos = false;	//shows informations on the progress data
    			$addhistoryactive = false; //history and statuschange is saved in orders
    
    			// Transactional Email Template's ID
    			$templateId  = 1; //choise template
    
    			// Set sender information
    			$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
    			$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
    			$sender = array('name' => $senderName,'email' => $senderEmail);
    
    			// Get Store ID
    			$storeid = Mage::app()->getStore()->getId();
    			$billing_details = $order->getBillingAddress();
    			$payment_data = $order->getData();
    			$gender = $payment_data['customer_gender'];//$billing_details->getGender();
    			$customer_name = $billing_details->getLastname();
    
    			if($addhistoryactive)
    			{
    				//add history comment
    				$comment = 'blabla';
    				$order->addStatusHistoryComment($comment);
    				$order->save();
    			}
    
    			if($gender==1)
    			{ //1-male, Magento standard
    				$gender_html = "Mr. ".$customer_name;
    			}
    			else
    			{ //0-female, Magento standard
    				$gender_html = "Mrs. ".$customer_name;
    			}					
    
    			// Set recepient information
    			$recepientEmail = $billing_details->getEmail();
    			$recepientName = $billing_details->getFirstname()." "
                           .$billing_details->getLastname();
    
    			$payment = $order->getPayment();
    			$method = $payment->getMethodInstance();
    			$addinfo = $order->getPayment()->getAdditionalInformation();
    			$grandtotal = $order->getGrandTotal();
    
    			$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
                           ->setIsSecureMode(true);
                            $paymentBlock->getMethod()->setStore($storeId);
                            $paymentBlockHtml = $paymentBlock->toHtml();
    			 // Set variables that can be used in email template
    			$vars = array('customerName' => 'test',
    					'customerEmail' => $recepientEmail,
    					'gender_html' => $gender_html,
    					'order' => $order,
    					);
    
    			// Send Transactional Email
    			$mailTemplate = Mage::getModel('core/email_template');
    			$mailTemplate->setTemplateSubject('subject');
    			$mailTemplate->setDesignConfig(array('area' => 'frontend'));
    			$mailTemplate->sendTransactional($templateId, $sender, $recepientEmail,
                            $recepientName, $vars, $storeId);
    
            return true;
        }
    }
    2. app/code/local/Engineering/Transemail/etc/config.xml:

    <?xml version="1.0"?>
    <config>
    <modules>
    <Engineering_Transemail>
    <version>0.1.0</version>
    </Engineering_Transemail>
    </modules>
    <frontend>
    <events>
    <checkout_onepage_controller_success_action>
    <observers>
    <transemail_observer>
    <type>singleton</type>
    <class>Engineering_Transemail_Model_Observer</class>
    <method>sendMail</method>
    </transemail_observer>
    </observers>
    </checkout_onepage_controller_success_action>
    </events>
    </frontend>
    </config>

    Now - Don't forget to create the app/etc/modules/Engineering_Transemail.xml file:
    <?xml version="1.0"?>
    <config>
        <modules>
    	    <Engineering_Transemail>
    	       <active>true</active>
                   <codePool>local</codePool>
    	    </Engineering_Transemail>
        </modules>
    </config>

    This post was posted in Writing a Magento Extension

Items 4 to 6 of 22 total

Page:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. ...
  7. 8
Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, Engineer-ing.com was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.