Tuesday, April 25, 2017

How to create Guest Wishlist For Magento

Add to wishlist without customer login


Add three tables

CREATE TABLE `wishlist` (
`wishlist_id` INT(11) NOT NULL AUTO_INCREMENT,
`customer_id` INT(11) NULL DEFAULT NULL,
`shared` INT(11) NULL DEFAULT NULL,
`sharing_code` VARCHAR(50) NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL,
`cookie` VARCHAR(255) NULL DEFAULT NULL,
PRIMARY KEY (`wishlist_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0
;


CREATE TABLE `wishlist_item` (
`wishlist_item_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Wishlist item ID',
`wishlist_id` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Wishlist ID',
`product_id` INT(10) UNSIGNED NOT NULL DEFAULT '0' COMMENT 'Product ID',
`store_id` SMALLINT(5) UNSIGNED NULL DEFAULT NULL COMMENT 'Store ID',
`added_at` TIMESTAMP NULL DEFAULT NULL COMMENT 'Add date and time',
`description` TEXT NULL COMMENT 'Short description of wish list item',
`qty` DECIMAL(12,4) NOT NULL COMMENT 'Qty',
PRIMARY KEY (`wishlist_item_id`),
INDEX `IDX_WISHLIST_ITEM_WISHLIST_ID` (`wishlist_id`),
INDEX `IDX_WISHLIST_ITEM_PRODUCT_ID` (`product_id`),
INDEX `IDX_WISHLIST_ITEM_STORE_ID` (`store_id`)
)
COMMENT='Wishlist items'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=0
;

CREATE TABLE `wishlist_item_option` (
`option_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Option Id',
`wishlist_item_id` INT(10) UNSIGNED NOT NULL COMMENT 'Wishlist Item Id',
`product_id` INT(10) UNSIGNED NOT NULL COMMENT 'Product Id',
`code` VARCHAR(255) NOT NULL COMMENT 'Code',
`value` TEXT NULL COMMENT 'Value',
PRIMARY KEY (`option_id`)
)
COMMENT='Wishlist Item Option Table'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=3
;


..........................................................................
After That :

edit - app/code/core/Mage/Wishlist/controllers/IndexController.php

Replace these functions:
protected $_skipAuthentication = true;

 public function skipAuthentication()
    {
        $this->_skipAuthentication = true;
        return $this;
    }


 protected function _getWishlist($wishlistId = null)
    {
        $wishlist = Mage::registry('wishlist');
        if ($wishlist) {
            return $wishlist;
        }

        try {
            if (!$wishlistId) {
                $wishlistId = $this->getRequest()->getParam('wishlist_id');
            }
            $customerId = Mage::getSingleton('customer/session')->getCustomerId();
            /* @var Mage_Wishlist_Model_Wishlist $wishlist */
            $wishlist = Mage::getModel('wishlist/wishlist');
            if ($wishlistId) {
                $wishlist->load($wishlistId);
            } else {
                $wishlist->loadByCustomer($customerId, true);
            }

            if (!$wishlist->getId() ) {
                $wishlist = null;
                Mage::throwException(
                    Mage::helper('wishlist')->__("Requested wishlist doesn't exist")
                );
            }

            Mage::register('wishlist', $wishlist);
        } catch (Mage_Core_Exception $e) {
            Mage::getSingleton('wishlist/session')->addError($e->getMessage());
            return false;
        } catch (Exception $e) {
            Mage::getSingleton('wishlist/session')->addException($e,
                Mage::helper('wishlist')->__('Wishlist could not be created.')
            );
            return false;
        }

        return $wishlist;
    }

Solved :)

Friday, April 7, 2017

Add Dynamic data on magento admin form

<?php
class Ankur_Banners_Block_Adminhtml_Banners_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{

$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset("banners_form", array("legend"=>Mage::helper("banners")->__("Item information")));

$fieldset->addField('lable', 'hidden', array(
'label'     => Mage::helper('megamenu')->__('Add Images'),

'name' => 'label',
'after_element_html' => '<div id="add_more"></div><button id="add" class="add_field_button"  type="button" >Add Images</button>'
));



if (Mage::getSingleton("adminhtml/session")->getBannersData())
{
$form->setValues(Mage::getSingleton("adminhtml/session")->getBannersData());
Mage::getSingleton("adminhtml/session")->setBannersData(null);
}
elseif(Mage::registry("banners_data")) {
   $form->setValues(Mage::registry("banners_data")->getData());
}
return parent::_prepareForm();
}
}
 ?>
 <script type="text/javascript">

jQuery(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = jQuery("#add_more"); //Fields wrapper
    var add_button      = jQuery(".add_field_button"); //Add button ID
 
    var x = 1; //initlal text box count
    jQuery(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
             //text box increment
index=x;
            jQuery(wrapper).append('\
    <div id="banner' + index + '" >\
       <tr>\
  <a   class="remove_field">remove form</a></br\
        <td class="label"><label for="image">Image</label></td>\
<td class="value">\
         <input id="image' + index + '" name="image' + index + '" value="" type="file" class="input-file">\
<p class="note" id="note_image"><span>(*.jpg, *.png, *.gif)</span></p> </td>\
  </tr>\
     <tr>\
        <td class="label">\
<label for="image_path">Image Path</label></td>\
<td class="value">\
<input id="image_path' + index + '" name="image_path' + index + '" value="" type="text" class=" input-text">\
</td>\
    </tr></br>\
    <tr>\
        <td class="label"><label for="image_link">Image Link</label></td>\
<td class="value">\
       <input id="image_link' + index + '" name="image_link' + index + '" value="" type="text" class=" input-text"> </td>\
    </tr></br>\
    <tr>\
        <td class="label"><label for="short_order">Short Order</label></td>\
<td class="value">\
        <input id="short_order' + index + '" name="short_order' + index + '" value="" type="text" class=" input-text">            </td>\
    </tr></br>\
    </br>\
    </div>\
');

        x++;}
    });
 
    jQuery(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); jQuery(this).parent('div').remove(); x--;
    })

});

</script>

Wednesday, March 1, 2017

Get Attribute Value , type by attribute id


 $attribute = Mage::getModel('eav/entity_attribute')->load($attr);
$type=$attribute->getFrontendInput();
$attrLabel=$attribute->getFrontendLabel();
$attrName=$attribute->getAttributeCode();
//echo $attribute->getData($attrName);die;
 if($type=='multiselect'){
$html.='<div class="field"><label for='.$attrName.'"  class="required">'.$attrLabel.': </label><div class="input-box"><select multiple id="'.$attrName.'"  name="product['.$attrName.'][]" >';
//$values=$attribute->getSource()->getAllOptions(true, true);
$values=$attribute->getAttributeText($attrName);
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$attrName);
$collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
                ->setStoreFilter(Mage::app()->getStore()->getId())
                ->load();
           
             

foreach($collection->toOptionArray() as $option){
$html.='<option value="'.$option['value'].'">'.$option['label'].'</option>';
}
$html.='</select></div></br>';
}
if($type=='select'){
$html.='<label for='.$attrName.'"  class="required">'.$attrLabel.': </label><div class="input-box"><select id="'.$attrName.'"  name="product['.$attrName.']" >';
$values=$attribute->getSource()->getAllOptions(true, true);
foreach($values as $option){
$html.='<option value="'.$option['value'].'">'.$option['label'].'</option>';
}
$html.='</select></div></br>';
}
if($type=='text') {

$html.='<label for='.$attrName.'" class="required">'.$attrLabel.'</label>: <div class="input-box"> <input id="'.$attrName.'" required type="'.$type.'" name="product['.$attrName.']" /></div></br>';
}

Friday, February 24, 2017

Magento Update product status programmatically

public function updateproductstausAction(){
$productid=$this->getRequest()->getParam("product_id");
if($this->getRequest()->getParam("val")==0){
// product id which you want to change status;
$storeid=0 ;// your store id 0 is for default store id
Mage::getModel('catalog/product_status')->updateProductStatus($productid, $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
}
elseif($this->getRequest()->getParam("val") =='1'){
$storeid=0 ;// your store id 0 is for default store id
Mage::getModel('catalog/product_status')->updateProductStatus($productid, $storeid, Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
}
}

Monday, February 20, 2017

Handle exception in magento

try{
write your code in try block
  $imgSrc = $_product->getImageUrl();
}
catch(Exception $e) {
// catch your exception here
  $imgSrc = '0';
}

Thursday, February 9, 2017

Step by Step validating jquery-steps form with bootstrap validator

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://formvalidation.io/vendor/formvalidation/css/formValidation.min.css" />
<link rel="stylesheet" href="http://formvalidation.io/vendor/jquery.steps/css/jquery.steps.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="http://formvalidation.io/vendor/formvalidation/js/formValidation.min.js"></script>
<script src="http://formvalidation.io/vendor/formvalidation/js/framework/bootstrap.min.js"></script>
<script src="http://formvalidation.io/vendor/jquery.steps/js/jquery.steps.min.js"></script>

<div id="wrapper">
    <!-- main container div-->
    <div id="container" class="container">
        <div class="row">
            <div id="maincontent" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class="row">
                    <div id="" class="col-lg-12">
                        <form id="multiphase" role="form" class="form-horizontal" action="" method="post">
                             <h2>Step</h2>

                            <section data-step="0">
                                 <h2>Student's Details:</h2>

                                <hr>
                                <div class="form-group">
                                    <label for="sd_lastname" class="col-lg-2 control-label">Last Name:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="sd_lastname" id="sd_lastname" class="form-control" placeholder="Last Name" value="" tabindex="1">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="sd_firstname" class="col-lg-2 control-label">First Name:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="sd_firstname" id="sd_firstname" class="form-control" placeholder="First Name" value="" tabindex="2">
                                    </div>
                                </div>
                            </section>
                             <h2>Step</h2>

                            <section data-step="1">
                                 <h2>Parent's Details:</h2>

                                <hr>
                                <div class="form-group">
                                    <label for="pd_lastname" class="col-lg-2 control-label">Last Name:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="pd_lastname" id="pd_lastname" class="form-control" placeholder="Last Name" value="" tabindex="1">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="pd_firstname" class="col-lg-2 control-label">First Name:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="pd_firstname" id="pd_firstname" class="form-control" placeholder="First Name" value="" tabindex="2">
                                    </div>
                                </div>
                            </section>
                             <h2>Step</h2>

                            <section data-step="2">
                                 <h2>Reference Details:</h2>

                                <hr>
                                <div class="form-group">
                                    <label for="rd_schoolrefname" class="col-lg-3 control-label">School Principal's Name:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="rd_schoolrefname" id="rd_schoolrefname" class="form-control" placeholder="First Name Last Name" value="" tabindex="1">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="rd_schoolrefmobile" class="col-lg-3 control-label">Phone or Mobile No.:</label>
                                    <div class="col-lg-2">
                                        <input type="text" name="rd_schoolrefmobile" id="rd_schoolrefmobile" class="form-control" placeholder="Phone or Mobile Number" data-mask="+99-99999-99999" value="" tabindex="2">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="rd_schoolrefemail" class="col-lg-3 control-label">E-Mail ID:</label>
                                    <div class="col-lg-3">
                                        <input type="text" name="rd_schoolrefemail" id="rd_schoolrefemail" class="form-control" placeholder="E-Mail ID" value="" tabindex="3">
                                    </div>
                                </div>
                            </section>
                            <!-- end of wizard-->
                        </form>
                        <!-- end of form-->
                    </div>
                </div>
                <!-- end of row-->
                <div class="modal fade" id="welcomeModal">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                 <h1 class="modal-title text-center">Add new last name</h1>

                            </div>
                            <div class="modal-body">
                                <form method="POST" name="add_lastname">
                                    <input type="text" name="add_lastname" id="add_lastname" class="form-control" placeholder="Enter your last name here" value="">
                                    <p class="">The first alphabet of the last name <strong>MUST</strong> be in upper case.</p>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
                                <input name="addlastname" type="submit" value="Add" class="btn btn-primary">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row hidden-print">
            <div id="footer" class="col-lg-12"></div>
        </div>
    </div>
</div>


<script type="text/javascript">
$(document).ready(function () {
    function adjustIframeHeight() {
        var $body = $('body'),
            $iframe = $body.data('iframe.fv');
        if ($iframe) {
            // Adjust the height of iframe
            $iframe.height($body.height());
        }
    }
    $("#multiphase").steps({
        headerTag: "h2",
        bodyTag: "section",
        saveState: true,
        onStepChanged: function (e, currentIndex, priorIndex) {
            // You don't need to care about it
            // It is for the specific demo
            adjustIframeHeight();
        },
        // Triggered when clicking the Previous/Next buttons
        onStepChanging: function (e, currentIndex, newIndex) {
            var fv = $('#multiphase').data('formValidation'), // FormValidation instance
                // The current step container
                $container = $('#multiphase').find('section[data-step="' + currentIndex + '"]');

            // Validate the container
            fv.validateContainer($container);

            var isValidStep = fv.isValidContainer($container);
            if (isValidStep === false || isValidStep === null) {
                // Do not jump to the next step
                return false;
            }

            return true;
        },
        // Triggered when clicking the Finish button
        onFinishing: function (e, currentIndex) {
            var fv = $('#multiphase').data('formValidation'),
                $container = $('#multiphase').find('section[data-step="' + currentIndex + '"]');

            // Validate the last step container
            fv.validateContainer($container);

            var isValidStep = fv.isValidContainer($container);
            if (isValidStep === false || isValidStep === null) {
                return false;
            }

            return true;
        },
        onFinished: function (e, currentIndex) {
            // Uncomment the following line to submit the form using the defaultSubmit() method
            $('#multiphase').formValidation('defaultSubmit');

            // For testing purpose
            $('#welcomeModal').modal("show");
        }
    }).formValidation({
        excluded: ':disabled',
        message: 'This value is not valid',
        container: 'tooltip',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            //last name validation  
            sd_lastname: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The Last Name is required and cannot be empty'
                    }
                }
            },

            //first name validation
            sd_firstname: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The First Name is required and cannot be empty'
                    },
                    stringLength: {
                        min: 3,
                        max: 30,
                        message: 'The First Name must be more than 7 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[A-Z]+$/i,
                        message: 'The First Name can only consist of alphabetical characters'
                    }
                }
            },

            //validation of Parent's details step start
            //last name validation  
            pd_lastname: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The Last Name is required and cannot be empty'
                    }
                }
            },

            //first name validation
            pd_firstname: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The First Name is required and cannot be empty'
                    },
                    stringLength: {
                        min: 3,
                        max: 30,
                        message: 'The First Name must be more than 7 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[A-Z]+$/i,
                        message: 'The First Name can only consist of alphabetical characters'
                    }
                }
            },

            // Validation for Reference details starts
            //School reference name
            rd_schoolrefname: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The School Reference Name is required and cannot be empty'
                    },
                    stringLength: {
                        min: 7,
                        max: 40,
                        message: 'The School Reference Name must be more than 7 and less than 40 characters long'
                    },
                    regexp: {
                        regexp: /^[A-Z\s]+$/i,
                        message: 'The School Reference Name can only consist of alphabetical characters'
                    }
                }
            },
            //School reference phone
            rd_schoolrefmobile: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The Phone or Mobile is required and cannot be empty'
                    }
                }
            },
            rd_schoolrefemail: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The E-Mail ID is required and cannot be empty'
                    },
                    regexp: {
                        regexp: /[a-zA-Z0-9]+(?:(\.|_)[A-Za-z0-9!#$%&'*+\/=?^`{|}~-]+)*@(?!([a-zA-Z0-9]*\.[a-zA-Z0-9]*\.[a-zA-Z0-9]*\.))(?:[A-Za-z0-9](?:[a-zA-Z0-9-]*[A-Za-z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/g,
                        message: 'The E-Mail ID is not a valid E-Mail'
                    }
                }
            },
        }

    })

});
</script>

Wednesday, February 8, 2017

Call Controller by Ajax in Magento Admin 1.9

<form action=" id="mass_forms">
<input name="form_key" type="hidden" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
<select id="mainStatus" onChange="getSubStatus()">
<option value=''>Select Status</option>
<?php
foreach($item_Status as $itemstatus){?>
<option value='<?php echo $itemstatus['id'] ?>'><?php echo $itemstatus['value'] ?></option>
<?php }
?>
</select>
</form>

////////////////////////////////////////////////////////////////////////////////
Script:
<script>



function getSubStatus(){
var posdata = jQuery("#mass_forms").serializeArray();
var url =  "<?php echo $this->getUrl('marketplaceadmin/adminhtml_orderview/getsubstaus');?>";
jQuery.ajax({
type: "POST",
url: url,
datatype: "text",
data: posdata,
success: function(data)
{

alert(data);
}
});


}

</script>
//////////////////////////////////////////////////////////////////////////
controller:
public function getsubstausAction(){
// $statusId=$this->getRequest()->getParam("statusId");
echo 'ankur';
}