Magento – All payment methods

Is there a function to get all available payment methods in magento ? YES

app/code/core/Mage/Payment/Model/Config.php

public function getActiveMethods($store=null)
    {
        $methods = array();
        $config = Mage::getStoreConfig('payment', $store);
        foreach ($config as $code => $methodConfig) {
            if (Mage::getStoreConfigFlag('payment/'.$code.'/active', $store)) {
                if (array_key_exists('model', $methodConfig)) {
                    $methodModel = Mage::getModel($methodConfig['model']);
                    if ($methodModel && $methodModel->getConfigData('active', $store)) {
                        $methods[$code] = $this->_getMethod($code, $methodConfig);
                    }
                }
            }
        }
        return $methods;
    }

Magento – get client’s IP

To get customer’s IP, behind a proxy or not, you can use this build in function in magento

 Mage::app()->getRequest()->getClientIp(true);

refers to lib/Zend/Controller/Request/Http.php

 /**
     * Get the client's IP addres
     *
     * @param  boolean $checkProxy
     * @return string
     */
    public function getClientIp($checkProxy = true)
    {
        if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
            $ip = $this->getServer('HTTP_CLIENT_IP');
        } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
            $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
        } else {
            $ip = $this->getServer('REMOTE_ADDR');
        }

        return $ip;
    }

Magento – test abandoned cart email

A test script to test abandoned cart email

//test email abandonned cart
$storeId = 13;
$store = Mage::getSingleton('core/store')->load($storeId);
$quote = Mage::getModel('sales/quote')->setStore($store)->load(347453);
$email = "test@adin.pro";
// send email
$senderid = Mage::getStoreConfig(Ebizmarts_AbandonedCart_Model_Config::SENDER, $storeId);
$sender = array('name' => Mage::getStoreConfig("trans_email/ident_$senderid/name", $storeId), 'email' => Mage::getStoreConfig("trans_email/ident_$senderid/email", $storeId));



$name = $quote->getCustomerFirstname() . ' ' . $quote->getCustomerLastname();
$unsubscribeUrl = Mage::getModel('core/url')->setStore($storeId)->getUrl() . 'ebizautoresponder/autoresponder/unsubscribe?list=abandonedcart&email=' . $email . '&store=' . $storeId;
$couponcode = '';

// if days have passed proceed to send mail
if (true) {
    $mailsubject = "Abandonned cart relance";
    $templateId = 63;

    //$templateId = Mage::getStoreConfig(Ebizmarts_AbandonedCart_Model_Config::EMAIL_TEMPLATE_XML_PATH);
    $vars = array('quote' => $quote, 'url' => $url, 'unsubscribeurl' => $unsubscribeUrl, 'tags' => array("mandrilltag"));


    $customer = Mage::getModel('customer/customer')
        ->setStore(Mage::app()->getStore($storeId))
        ->loadByEmail($email);
    if ($customer->getId() && Mage::helper('sweetmonkey')->enabled()) {
        $tbtPoints = Mage::helper('ebizmarts_abandonedcart')->getTBTPoints($customer->getId());
        foreach ($tbtPoints as $key => $field) {
            if ($key == 'points') {
                if ($field >= Mage::getStoreConfig('sweetmonkey/general/email_points', $storeId)) {
                    $vars[$key] = $field;
                }
            } else {
                $vars[$key] = $field;
            }
        }
    }

    Mage::app()->getTranslator()->init('frontend', true);
    $translate = Mage::getSingleton('core/translate');
    $mail = Mage::getModel('core/email_template')
        ->setTemplateSubject($mailsubject)
        ->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);
    echo "==> tpl". $templateId." email: ".$email." name: ".$name." store: ".$storeId;
    $translate->setTranslateInLine(true);

    die('does it send ?'); 

Magento add order history

There is several ways to add order history, with less or more data

Quick way :

 $message = 'Comment to add in order history';
 $order->addStatusHistoryComment($message);
 $order->save();

With all informations :

$history = Mage::getModel('sales/order_status_history')
  ->setStatus($order->getStatus())
  ->setComment('Comment to add in order history')
  ->setEntityName(Mage_Sales_Model_Order::HISTORY_ENTITY_NAME)
  ->setIsCustomerNotified(false)
  ->setCreatedAt(date('Y-m-d H:i:s', time()));

  $order->addStatusHistory($history);
  $order->save();

Magento – Emulate store

It’s that simple :

$appEmulation = Mage::getSingleton('core/app_emulation');
 
//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

 
//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);