Showing posts with label Order. Show all posts
Showing posts with label Order. Show all posts

Friday 3 August 2018

How to add | update Shipping Price on existing order in Magento 1.9

$shippingamount = 10.00;
        try {
            $order = Mage::getModel('sales/order')->load(37881)
                    ->setShippingMethod('cstoreshipping_cstoreshipping')
                    ->setShippingAmount($shippingamount)
                    ->setBaseShippingAmount($shippingamount)
                    ->setShippingDescription('Shipping by third party');
            $order->setGrandTotal($order->getGrandTotal() + $shippingamount);
            $order->setBaseGrandTotal($order->getBaseGrandTotal() + $shippingamount);
            $order->save();
            pre($order->getData());
        } catch (Exception $e) {
            pre($e->getMessage());
        }

Wednesday 8 November 2017

Create custom order in magento programatically

function createorderAction(){
       
        $order = Mage::getModel('sales/order')->load(124);
       
        $ba = $order->getBillingAddress();
        $sa = $order->getShippingAddress();
       
        $customer_id = $order->getCustomerId();
       
        $store_id = 1;
        /*------------------------------------------------------------------------
        Payment and Shipping Method
        ------------------------------------------------------------------------*/
        $payment_method = 'checkmo';
        //$payment_method = 'stripe_cc';
        $shipping_method = 'flatrate_flatrate';
        $customer = Mage::getModel('customer/customer')->load($customer_id);
       
        /*------------------------------------------------------------------------
        Create a Quote
        ------------------------------------------------------------------------*/
        $quote = Mage::getModel('sales/quote');
        $quote->setStoreId($store_id);
        $quote->assignCustomer($customer);
        $quote->setSendCconfirmation(1);
        /*------------------------------------------------------------------------
        Add products to Quote
        ------------------------------------------------------------------------*/
        $items = $order->getAllItems();
       
        $block = Mage::app()->getLayout()->createBlock('sales/order_item_renderer_default');
       
        $quote->setCouponCode('20OFF');
       
        foreach ($items as $item):
           
            $block->setItem($item);
           
            $_options = $block->getItemOptions();
           
            $addproduct = array('product'=>$item->getProductId(), 'qty'=>1);
            foreach ($_options as $_option):
                $addproduct['options'][$_option['option_id']] = $_option['option_value'];
            endforeach;
           
            $product = Mage::getModel('catalog/product')->load($addproduct['product']);
            $quote->addProduct($product,
                new Varien_Object(
                    $addproduct
                )
            );
            //$item->setDiscountAmount($price);
            //$item->setBaseDiscountAmount($price);
        endforeach;
       
        /*------------------------------------------------------------------------
        Assign Address, Shipping and Payment methods
        ------------------------------------------------------------------------*/
       
        $address_data = array(
            'customer_address_id' => $ba->getId(),
            'firstname' => $ba->getFirstname(),
            'lastname' => $ba->getLastname(),
            'street' => array(
                '0' => $ba->getStreet()
            ),
            'city' => $ba->getCity(),
            'postcode' => $ba->getPostcode(),
            'telephone' => $ba->getTelephone(),
            'country_id' => $ba->getCountryId(),
            'region_id' => $ba->getRegionId(),
        );
       
        $s_address_data = array(
            'customer_address_id' => $sa->getId(),
            'firstname' => $sa->getFirstname(),
            'lastname' => $sa->getLastname(),
            'street' => array(
                '0' => $sa->getStreet()
            ),
            'city' => $sa->getCity(),
            'postcode' => $sa->getPostcode(),
            'telephone' => $sa->getTelephone(),
            'country_id' => $sa->getCountryId(),
            'region_id' => $sa->getRegionId(),
        );
       
        $billingAddress = $quote->getBillingAddress()->addData($address_data);
        $shippingAddress = $quote->getShippingAddress()->addData($s_address_data);
        $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
            ->setShippingMethod($shipping_method)
            ->setPaymentMethod($payment_method);
       
        $quote->getPayment()->importData(array('method' => $payment_method));
       
        //$quote->setDiscountAmount($discountAmount);
       
        $quote->collectTotals()->save();
       
        $discountAmount = 85.33;
        $paymentplantotal = 512.00;
        $downpayment = 42.67;
        $pymtfrequency = 1;
        $leftpayments = 11;
        $nextpayments = 42.67;
       
        $quote->setDiscountdue($discountAmount);
        $quote->setPaymentplantotal($paymentplantotal);
        $quote->setDownpayment($downpayment);
        $quote->setPymtfrequency($pymtfrequency);
        $quote->setLeftpayments($leftpayments);
        $quote->setNextpayments($nextpayments);
       
        $quote->setGrandTotal($quote->getBaseSubtotal() - $discountAmount)
            ->setBaseGrandTotal($quote->getBaseSubtotal() - $discountAmount)
            ->setSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount)
            ->setBaseSubtotalWithDiscount($quote->getBaseSubtotal() - $discountAmount);
       
        $quote->collectTotals()->save();
        //$quote->setCouponCode("20OFF")->save();
               
        $canAddItems = $quote->isVirtual() ? ('billing') : ('shipping');
        foreach ($quote->getAllAddresses() as $address) {
           
            if ($address->getAddressType() == $canAddItems) {
                $address->setSubtotalWithDiscount((float) $address->getSubtotalWithDiscount() - $discountAmount);
                $address->setGrandTotal((float) $address->getGrandTotal() - $discountAmount);
                $address->setBaseSubtotalWithDiscount((float) $address->getBaseSubtotalWithDiscount() - $discountAmount);
                $address->setBaseGrandTotal((float) $address->getBaseGrandTotal() - $discountAmount);
                /*************************/
                $address->setDiscountdue($discountAmount);
                $address->setPaymentplantotal($paymentplantotal);
                $address->setDownpayment($downpayment);
                $address->setPymtfrequency($pymtfrequency);
                $address->setLeftpayments($leftpayments);
                $address->setNextpayments($nextpayments);
                $address->save();
            }
        }
       
        foreach ($quote->getAllItems() as $item) {
            //We apply discount amount based on the ratio between the GrandTotal and the RowTotal
            $rat = $item->getPriceInclTax() / $quote->getBaseSubtotal();
            $ratdisc = $discountAmount * $rat;
            $item->setDiscountAmount(($item->getDiscountAmount() + $ratdisc) * $item->getQty());
            $item->setBaseDiscountAmount(($item->getBaseDiscountAmount() + $ratdisc) * $item->getQty())->save();
        }
       
        /*----------------------------------------------------------------------*/
        /*------------------------------------------------------------------------
        SECTION : CHECKOUT
        ------------------------------------------------------------------------*/
        $convertQuote = Mage::getModel('sales/convert_quote');
        if ($quote->getIsVirtual()) {
            $order = $convertQuote->addressToOrder($quote->getBillingAddress());
        } else {
            $order = $convertQuote->addressToOrder($quote->getShippingAddress());
        }
        // assign payment method
        $quotePayment = $quote->getPayment();
        $quotePayment->setMethod($quote->getPayment()->getMethod());
        $quote->setPayment($quotePayment);
        $orderPayment = $convertQuote->paymentToOrderPayment($quotePayment);
        $order->setBillingAddress($convertQuote->addressToOrderAddress($quote->getBillingAddress()));
        $order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
        if (!$quote->getIsVirtual()) {
            $order->setShippingAddress($convertQuote->addressToOrderAddress($quote->getShippingAddress()));
        }
        // set payment options
        $order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
        // order products
        $items = $quote->getAllItems();
        foreach ($items as $item) {
            //@var $item Mage_Sales_Model_Quote_Item
            $orderItem = $convertQuote->itemToOrderItem($item);
            if ($item->getParentItem()) {
                $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
            }
            $order->addItem($orderItem);
        }
       
       
        $order->setCanShipPartiallyItem(false);
        $order->sendNewOrderEmail();
        $order->place();
        $order->save();
        $quote->setIsActive(0);
        $quote->save();
        //pre($order->getData());exit;
        //pre($quote->getData());
    }

Friday 9 June 2017

Test email in order email notification in magento

Add following function into any frontend controller and check via URL

public function sendtestAction()
    {
        $order = Mage::getModel('sales/order');
        $incrementId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
        $order->loadByIncrementId($incrementId);
        echo $incrementId;
        try {
            echo $order->sendNewOrderEmail();
        } catch (Exception $ex) {
            echo "Email Not Sent...";
        }
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $email = $customer->getEmail(); //End Email Sending
        echo '<pre>Done';
        echo $email;exit;
    }

Thursday 15 September 2016

How to change the sort order for shipping in admin panel in magento



Step 1 : You have to copy this two files to your Theme in the adminhtml

app/design/adminhtml/default/default/template/sales/order/shipment/create/tracking.phtml
app/design/adminhtml/default/default/template/sales/order/invoice/create/tracking.phtml


Step 2 : Open : app/design/adminhtml/default/default/template/sales/order/shipment/create/tracking.phtml
(you will find this code at Line number 93)

Replace with

<select name="tracking[__index__][carrier_code]" id="trackingC__index__" class="select carrier" style="width:110px;" disabled="disabled">
    <?php foreach ($this->getCarriers() as $_code=>$_name): ?>
    <option value="<?php echo $_code ?>"><?php echo $this->escapeHtml($_name) ?></option>
    <?php endforeach; ?>
</select>

Step 3 : Open : app/design/adminhtml/default/default/template/sales/order/invoice/create/tracking.phtml
(you will find this code at Line number 93)

Replace with

<select name="tracking[__index__][carrier_code]" id="trackingC__index__" class="select carrier" style="width:110px;" disabled="disabled">
    <?php foreach ($this->getCarriers() as $_code=>$_name): ?>
    <option <?php if ($_code=="ups"): ?>selected<?php endif; ?> value="<?php echo $_code ?>"><?php echo $this->escapeHtml($_name) ?></option>
    <?php endforeach; ?>
</select>

Thursday 4 August 2016

Export csv after place order in magento

config.xml


<?xml version="1.0"?>
<config>
  <modules>
    <Test_Test>
      <version>0.1.0</version>
    </Test_Test>
  </modules>
  <global>
    <helpers>
      <test>
        <class>Test_Test_Helper</class>
      </test>
    </helpers>
<models>
 <test>
<class>Test_Test_Model</class>
<resourceModel>test_mysql4</resourceModel>
 </test>
</models>
    <events>
 <checkout_onepage_controller_success_action> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_onepage_controller_success_action_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>test/observer</class> <!-- observers class alias -->
            <method>exportCsvOnCheckout</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_onepage_controller_success_action_handler>
        </observers>
      </checkout_onepage_controller_success_action>
    </events>
  </global>
</config>


Observer.php

<?php
class Test_Test_Model_Observer
{
public function exportCsvOnCheckout(Varien_Event_Observer $observer)
{
$orderIds = $observer->getData('order_ids');
$order     = Mage::getModel('sales/order')->load($orderIds);
$customer = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
// echo '<pre>';print_r($customer->getName());exit;
$file_path = "order.csv";
$mage_csv = new Varien_File_Csv();
$data = array();
$data['Email'] = $customer->getEmail();
$data['name'] = $customer->getName();
$products_row[] = $data;
$mage_csv->saveData($file_path, $products_row);
}
} }

Thursday 5 May 2016

How to Shopify API Product / Order / Collection get sorted when passing in an created_at / updated_at filter

What would be ideal would be to be able to get results in ascending/descending order one way or another, but without having to pass in since_id, because we want to be able to pull in new data on orders on an ongoing basis as they may be updated.

Its possible to sort the Products/Orders/Collection by passing an "order" parameter. The "order" parameter should contain the field to sort by (supported fields are "created_at", "updated_at" and "processed_at"), followed by a space and then by an "asc" or "desc".

Please check following Example for solution.

https://example.myshopify.com/admin/orders.json?limit=2&fields=id,created_at&order=created_at%20asc

Saturday 13 February 2016

Get customer addresses with admin formated

 $order = $this->getOrder();
 $address = $order->getShippingAddress()->getFormated(true);
 echo $address;