Friday 29 April 2016

Adding products to cart via url in magento

If your Planning to do a website that opens directly on the cart page, in the cart it needs to have some products already selected and it should be ready to go to the checkout page.

Every time the user goes to the page, some products.

<a href="http://example.com/index.php/checkout/cart/add/product/1/form_key/<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>/">My cart</a>

Wednesday 27 April 2016

How to add form alt message/text on custom payment method like Paypal in magento


When you Create a Custom Payment Method Module in Magento. Add form alt message/text on custom payment method like Paypal(You will be redirected to the PayPal website).

in app/code/local/Companyname/Custompayment/Model/Custompayment.php

class Companyname_Custompayment_Model_Custompayment extends Mage_Payment_Model_Method_Abstract
{
    .
    .
    .
    .
    protected $_formBlockType = 'custompayment/standard_form';
    .
    .
    .
    .
    public function __construct($params = array())
    {
        ............
    }
}

Create new form.php and paste the following contents in that file.
app/code/local/Companyname/Custompayment/Block/Standard/Form.php

class Companyname_Custompayment_Block_Standard_Form extends Mage_Payment_Block_Form
{
    protected function _construct()
    {
        $config = Mage::getModel('custompayment/config');
        $this->setTemplate('custompayment/payment/redirect.phtml')
            ->setRedirectMessage(
                Mage::helper('custompayment')->__('You will be redirected to the Custompayment website when you place an order.')
            )
            ->setMethodTitle('')
            ->setMethodLabelAfterHtml($config->getFieldValue('title'));
        parent::_construct();
    }
}

In this file,  we've set up a template file path which will be used when Magento show our payment method related Message/info. Let's create the related template file.

Create new redirect.phtml in app/design/frontend/base/default/template/custompayment/payment/redirect.phtml

<ul class="form-list" id="payment_form_<?php echo $this->getMethodCode() ?>" style="display:none;">
    <li class="form-alt"><?php echo $this->getRedirectMessage() ?></li>
</ul>

Tuesday 26 April 2016

Create dropdown/select box in magento system.xml for payment/shipping configuration

Create drop down in system.xml and show custom values in it, rather than using default magento classes only

Directory : app/code/local/Companyname/Modulename/config/system.xml

In system.xml

<config>
  <sections>
<payment>
  <groups>
<Modulename translate="label" module="Modulename">
<label>Payment Module</label>
       <sort_order>670</sort_order>
       <show_in_default>1</show_in_default>
       <show_in_website>1</show_in_website>
       <show_in_store>0</show_in_store>
<fields>
<paymentmethod translate="label comment">
   <label>Payment method</label>
   <comment>Omni Payment method.</comment>
   <frontend_type>select</frontend_type>
   <source_model>Modulename/Modelname</source_model>
   <sort_order>30</sort_order>
   <show_in_default>1</show_in_default>
   <show_in_website>0</show_in_website>
   <show_in_store>0</show_in_store>
</paymentmethod>
</fields>
</Modulename>
            </groups>
        </payment>
    </sections>
</config>

If you want to create your custom source_Model, you need to do the following steps: Create a Pmethod.php in app/code/local/Companyname/Modulename/Model/Pmethod.php Companyname_Modulename is our module name. You need to create a function name toOptionArray() as mention below, the value and label can be anything you want to be display in dropdown

In Pmethod.php 

class Companyname_Modulename_Model_Pmethod
{
    public function toOptionArray()
    {
        return array(
            array(
                'value' => 'key1',
                'label' => 'Label 1',
            ),
            array(
                'value' => 'key2',
                'label' => 'label 2',
            )
        );
    }
}

CURL Work in magento code using Varien_Http_Adapter_Curl with PHP and Magento example


Magento Example

PHP CURL library is used to fetch third party contents, transfer files and post data. Magento Wraps the CURL Functions in its library with its own wrapper functions. There is no Hard and Fast Rule that we need to only use this function, but the magento core code makes use of this library. Varien_Http_Adapter_Curl class is responsible for providing the wrapper functions.

$clienturl = 'https://www.omnipaymentapi.com/backoffice/tools/status.php';
$jsonData = array(
               "firstdata"=>'firstdata',
               "seconddata"=>'seconddata'
               );
$http = new Varien_Http_Adapter_Curl();
$config = array('timeout' => 30,'header'=>false); # Or whatever you like!
$http->setConfig($config);
$http->write(Zend_Http_Client::POST, $clienturl, '1.1', array(), $jsonData);
$response = $http->read();
$http->close();
$xml  = new SimpleXMLElement($response);
echo '<pre>';print_r($xml);exit;

The above code fetches the lastest information on magento blog in xml format. The $curl->write function accepts the method name (GET,POST), URL,HTTP version,headers,body etc. The $curl->read function will execute the curl_exec() internally.

 PHP Example


$ch = curl_init(); // use curl to connect
$targetURL = "https://www.omnipaymentapi.com/backoffice/tools/status.php";
$data = array(
               "firstdata"=>'firstdata',
               "seconddata"=>'seconddata'
               );
//echo 'test';exit;
curl_setopt($ch, CURLOPT_URL, $targetURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);

Wednesday 20 April 2016

How to enable error and exception logging log in magento

By default, Magento does not enable messages about errors and exceptions logging; so if something bad happens, you don't even know.
To turn on the logging, you can change to
System > Configuration > Developer > Log Settings and set the «Enabled» option to «Yes»
Don't forget to set full access permissions (777) of following folder: /var/log, since that is the place where those log files are stored.

Monday 18 April 2016

How to get user ip in PHP/Codeigniter

$_SERVER['REMOTE_ADDR'] may not actually contain real client IP addresses, as it will give you a proxy address for clients connected through a proxy, for example. That may well be what you really want, though, depending what your doing with the IPs. Someone's private RFC1918 address may not do you any good if you're say, trying to see where your traffic is originating from, or remembering what IP the user last connected from, where the public IP of the proxy or NAT gateway might be the more appropriate to store.

There are several HTTP headers like X-Forwarded-For which may or may not be set by various proxies. The problem is that those are merely HTTP headers which can be set by anyone. There's no guarantee about their content. $_SERVER['REMOTE_ADDR'] is the actual physical IP address that the web server received the connection from and that the response will be sent to. Anything else is just arbitrary and voluntary information. There's only one scenario in which you can trust this information: you are controlling the proxy that sets this header. Meaning only if you know 100% where and how the header was set should you heed it for anything of importance.

For Codeigniter


$this->input->ip_address();

For PHP

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}
echo $ip;

Saturday 16 April 2016

Hide first default load div (Avoiding expressions) shown on page load in angularjs

As the others mentioned, use ng-cloak but also add the following to your CSS if it is the first to load in your page.

[ng\:cloak],[ng-cloak],.ng-cloak{display:none !important}
This will ensure that your div template is hidden. Below that div template, add something like

Loading...
The assignment of the $root.initializing.status with cause a true value for ng-hide.

<tr class="ng-cloak" dir-paginate="product in products">
             .........
             .........
             .........
</tr>