>> In magento to complete an order from admin you need to create an Invoice then a Shipment. If you want to make it by once click on shipment button. you can easily try this code to get the result.
$order = $orderObj; // get your order object.
if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {
try {
if(!$order->canInvoice()) {
$order->addStatusHistoryComment('Order cannot be invoiced.', false);
$order->save();
}
//START Handle Invoice
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically INVOICED.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
//END Handle Invoice
//START Handle Shipment
$shipment = $order->prepareShipment();
$shipment->register();
$order->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically SHIPPED.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
//END Handle Shipment
} catch (Exception $e) {
$order->addStatusHistoryComment('Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
$order->save();
}
}
Now see I've implemented it in shipment save action, means when you'll click on the shipment button to create the shipment, it'll automatically generate the invoice also. Go to app/code/core/Mage/Adminhtml/controllers/Sales/Order/ShipmentController.php
find the saveAction() method and replace it with the following code :
public function saveAction()
{
$data = $this->getRequest()->getPost('shipment');
if (!empty($data['comment_text'])) {
Mage::getSingleton('adminhtml/session')->setCommentText($data['comment_text']);
}
try {
$shipment = $this->_initShipment();
if (!$shipment) {
$this->_forward('noRoute');
return;
}
$shipment->register();
$order = $shipment->getOrder();
if(!$order->canInvoice()) {
$order->addStatusHistoryComment('Order cannot be invoiced.', false);
$order->save();
}
//START Handle Invoice
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);
$order->addStatusHistoryComment('Automatically INVOICED.', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
//END Handle Invoice
$comment = '';
if (!empty($data['comment_text'])) {
$shipment->addComment(
$data['comment_text'],
isset($data['comment_customer_notify']),
isset($data['is_visible_on_front'])
);
if (isset($data['comment_customer_notify'])) {
$comment = $data['comment_text'];
}
}
if (!empty($data['send_email'])) {
$shipment->setEmailSent(true);
}
$shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email']));
$responseAjax = new Varien_Object();
$isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label'];
if ($isNeedCreateLabel && $this->_createShippingLabel($shipment)) {
$responseAjax->setOk(true);
}
$this->_saveShipment($shipment);
$shipment->sendEmail(!empty($data['send_email']), $comment);
$shipmentCreatedMessage = $this->__('The shipment has been created.');
$labelCreatedMessage = $this->__('The shipping label has been created.');
$this->_getSession()->addSuccess($isNeedCreateLabel ? $shipmentCreatedMessage . ' ' . $labelCreatedMessage
: $shipmentCreatedMessage);
Mage::getSingleton('adminhtml/session')->getCommentText(true);
} catch (Mage_Core_Exception $e) {
if ($isNeedCreateLabel) {
$responseAjax->setError(true);
$responseAjax->setMessage($e->getMessage());
} else {
$this->_getSession()->addError($e->getMessage());
$this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
}
} catch (Exception $e) {
Mage::logException($e);
if ($isNeedCreateLabel) {
$responseAjax->setError(true);
$responseAjax->setMessage(
Mage::helper('sales')->__('An error occurred while creating shipping label.'));
} else {
$this->_getSession()->addError($this->__('Cannot save shipment.'));
$this->_redirect('*/*/new', array('order_id' => $this->getRequest()->getParam('order_id')));
}
}
if ($isNeedCreateLabel) {
$this->getResponse()->setBody($responseAjax->toJson());
} else {
$this->_redirect('*/sales_order/view', array('order_id' => $shipment->getOrderId()));
}
}
Hope it'll help you.......... :)
Monday, November 26, 2012
Friday, November 23, 2012
Create stylish file upload button in jquery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Stylish file input</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
// JavaScript Document
jQuery(document).ready(function(){
jQuery('input:file').each(function(){
jQuery(this)
.wrap('<div class="file-wrapper"></div>')
.css({'z-index':1000,'position':'relative','opacity':0,'-khtml-appearance':'none'})
.attr('size', 1)
.before('<span class="file"><label>No file choosen</label></span>')
.change(function(){
var val = jQuery(this).val();
var str = val.split(/[\s/\\]+/);
jQuery(this).prev('span.file').html("<label>"+str[str.length-1]+"</label>");
});
});
});
</script>
<style type="text/css">
.file-wrapper {
clear: both;
display: block;
height: 23px;
margin-bottom: 10px;
position: relative;
}
.file {
background: url("choose.png") no-repeat scroll 0 0 transparent;
font-family: Arial;
font-size: 13px;
height: 30px;
left: 0;
position: absolute;
text-align: left;
width: 400px;
}
.file label {
color: #353535;
float: left;
font-family: Arial;
font-size: 13px;
line-height: 17px;
margin: 0;
font-weight: normal;
padding-left: 110px;
}
.file-input-type{
margin-top:10%;
margin-left:10%;
}
</style>
</head>
<body>
<div class="file-input-type">
<input name="image1" type="file" class="file1">
</div>
</body>
</html>
see the demo here.
Create custom pagination in magento
Hope you known about block, xml in Magento.
Here is an extracted code that would help you to create pagination with custom collection in magento.
>> First paste the following code in your block class (e.g. product.php)
<?php
class example extends parentclass
{
private $_itemPerPage = 2;
private $_pageFrame = 8;
private $_curPage = 1;
public function getCollection($collection = 'null')
{
if($collection != 'null'){
$page = $this->getRequest()->getParam('p');
if($page) $this->_curPage = $page;
$collection->setCurPage($this->_curPage);
$collection->setPageSize($this->_itemPerPage);
return $collection;
}
}
public function getPagerHtml($collection = 'null')
{
$html = false;
if($collection == 'null') return;
if($collection->count() > $this->_itemPerPage)
{
$curPage = $this->getRequest()->getParam('p');
$pager = (int)($collection->count() / $this->_itemPerPage);
$count = ($collection->count() % $this->_itemPerPage == 0) ? $pager : $pager + 1 ;
$url = $this->getPagerUrl();
$start = 1;
$end = $this->_pageFrame;
$html .= '<ol>';
if(isset($curPage) && $curPage != 1){
$start = $curPage - 1;
$end = $start + $this->_pageFrame;
}else{
$end = $start + $this->_pageFrame;
}
if($end > $count){
$start = $count - ($this->_pageFrame-1);
}else{
$count = $end-1;
}
for($i = $start; $i<=$count; $i++)
{
if($i >= 1){
if($curPage){
$html .= ($curPage == $i) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}else{
$html .= ($i == 1) ? '<li class="current">'. $i .'</li>' : '<li><a href="'.$url.'&p='.$i.'">'. $i .'</a></li>';
}
}
}
$html .= '</ol>';
}
return $html;
}
public function getPagerUrl() // You need to change this function as per your url.
{
$cur_url = mage::helper('core/url')->getCurrentUrl();
$new_url = preg_replace('/\&p=.*/', '', $cur_url);
return $new_url;
}
}
?>
>> Now you have to modify your frontend phtml page. (e.g. custom.phtml)
<?php
$_collections = $this->getProductCollection(); // Default Collection on which you'll implement pagination.
$_productCollection = $this->getCollection($_collections); // calling the function that have been created in block page.
foreach ($_productCollection as $_product):
------------------ Your code here ------------
endforeach;
?>
<?php if($this->getPagerHtml($this->getProductCollection())):?> // pass the default collection as parameter
<div class="pages">
<span><?php echo $this->__('Page : ');?></span>
<?php echo $this->getPagerHtml($this->getProductCollection());?>
</div>
<?php endif;?>
Friday, November 9, 2012
get product quantity/qty in Magento
>> $_product is your product object. = Mage::getModel('catalog/product')->load('product_id');
<?php
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
$qty = $stock->getQty();
print_r( $stock->getData()); // To get other stock information.
?>
Thursday, November 8, 2012
How to redirect in magento
>> Pass the url in this function :
public function redirect($url)
{
return Mage::app()->getFrontController()
->getResponse()
->setRedirect($url);
}
Tuesday, November 6, 2012
Change default sort by order for one category in Magento
>> If you want to change default sort by value "position" to "name", change admin category setting. see the screen-shot.
you can change default direction by providing the following code in Custom Layout Update section in admin category "Custom Design" Tab
<reference name="product_list_toolbar">
<action method="setDefaultDirection"><dir>desc</dir></action>
</reference>
>> Add "created_at" in sort by dropdown.
update eav_attribute set frontend_label = "Date" where attribute_code = "created_at" AND entity_type_id = 4; // Ensure about the entity_type_id
update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id = (select attribute_id from eav_attribute where attribute_code = "created_at" AND entity_type_id = 4);
Now You can see the Date in admin default sort by configuration.
Admin=>System=>Configuration=>Catalog=>Frontend=> change value here "Product Listing Sort by"
>> If you want to add another attribute, go to the attribute setting and set "yes" for use for default sort by, see the screen-shot.
Admin=>Attributes=>Manage Attributes
Thursday, October 25, 2012
add custom product view page for custom product type in magento
>> Suppose you've created a new Product Type "custom". I've posted how to add new product type in magento. You want to use a different template for those product. follow the instruction ::
You can directly update in the catalog.xml or you can create a custom module by using Module Creator.
<layout>
<PRODUCT_TYPE_custom>
<reference name="product.info">
<action method="setTemplate">
<template>path/to/your/view.phtml</template>
</action>
</reference>
</PRODUCT_TYPE_custom>
</layout>
Subscribe to:
Comments (Atom)