Create custom WordPress form and handle submission?

Standard
<?php

/**
 * Plugin Name: Custom Form
 * Plugin URI: http://https://tahiryasin.wordpress.com/
 * Description: Creates a custom form through shortcode and handles the submit event.
 * Version: 1.0
 * Author: Tahir Yasin
 * Author URI: https://tahiryasin.wordpress.com/about/
 * License: GPL2
 */

class CustomForm
{

    public function __construct()
    {
        add_action('init', array($this, 'init'));
        add_shortcode('custom_form', array($this, 'shortcode'));
    }

    public function init()
    {
        if (!empty($_POST['nonce_custom_form']))
        {
            if (!wp_verify_nonce($_POST['nonce_custom_form'], 'handle_custom_form'))
            {
                die('You are not authorized to perform this action.');
            } else
            {
                $error = null;
                if (empty($_POST['name']))
                {
                    $error = new WP_Error('empty_error', __('Please enter name.', 'tahiryasin'));
                    wp_die($error->get_error_message(), __('CustomForm Error', 'tahiryasin'));
                }
                else
                    die('Its safe to do further processing on submitted data.');
            }
        }
    }

    function shortcode($atts)
    {
        return "<form method='post' action=''>
    <input name='name' type='text' value='' />
    " . wp_nonce_field('handle_custom_form', 'nonce_custom_form') . "
        <input type='submit' value='Submit'/>
    </form>";
    }

}

$CustomForm = new CustomForm();
?>

Get custom attribute value in Magento

Standard

Simplest way:

echo $_product->getAttributeText('custom_attribute_code');

Problem:

You will get a fetal error if the attribute is not already registered.

Fatal error: Call to a member function getSource() on a non-object in D:\..\app\code\core\Mage\Catalog\Model\Product.php on line 1389

Solution:

$attribute = $_product->getResource()->getAttribute('custom_attribute_code');
if ($attribute)
{
	echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
}

This code will first check if the attribute code exists and gets its value if its there.

Keeping your Yii Models Lean – Use Behaviors

Standard

Active Record models are fantastic for consolidating “black box” logic and keeping your models self-aware of their business logic, but what do you do when the business rules and object specific operations keep adding up?

Do yourself a favor, and get into the habit of putting those business logic methods into attachable behaviors.
Continue reading

How to save multiple related models in yii [Complete Solution]

How to save multiple related models in yii
Standard

Scenario:
You are in a situation where you have two related tables a Parent and a child table. You need to create a user experience in which user presses Save button only once, meaning by, user enters all the data in parent model fields, then enters data in all child model fields, optionally generates more child model fields and then presses the save button.
Continue reading

Authorize.net integration with Magento

Standard

1- Create a test account on Authorize.net (https://developer.authorize.net/testaccount/)

2- On successful signup you will see following info, save them at safe place.

  1. API Login ID
  2. Transaction Key

3- Login to your Magento Admin panel and navigate to

  1. System -> Configuration -> Payment Methods
  2. Expand Authorize.net
  3. Insert “API Login ID” and “Transaction Key” into this form and make other settings as shown below

authorize.net

6. Save Configuration and you are DONE!