first time customer free shipping on your first order - magento

My Question:
How to logical and programmatic develop for this requirement.
My requirement that : I need free shipping on customer first order. I have not any used coupon code or discount.
I have directly set free shipping when customer sign up and place order.

I have create one custom shipping methods then add my custom code for above requirment.
Shipping methods URL: following this url
http://inchoo.net/magento/custom-shipping-method-in-magento/
then added my code in carrier.php file like below.
$session = Mage::getSingleton('customer/session');
if ($session->isLoggedIn()) {
$customer = Mage::getSingleton('customer/session')->getCustomer();
//echo '<pre>';
//print_r(get_class_methods($customer));
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $customer->getId());
if (!$orders->getSize())
{
$result->append($this->_getFreeRate());
return $result;
}
}else{
if ($expressAvailable) {
$result->append($this->_getExpressRate());
}
$result->append($this->_getStandardRate());
return $result;
}

Related

Magento Collection get clients by payment method

I would like to know if anyone already coded a Magento Collection to get the customer name by the payment method? I used the code bellow and now I have the payment method that I need, now I just have to get the client's first and last name. Thank you all for the help.
$collection = Mage::getResourceModel('sales/order_payment_collection')
->addFieldToSelect('*');
foreach ($collection as $method) {
if ($method->getMethod() == "mundipagg_boleto") {
print $method->getMethod()."<br>";
}
}
$collection = Mage::getResourceModel('sales/order_payment_collection')
->addFieldToSelect('*')
->addFieldToFilter('method', "mundipagg_boleto");
foreach ($collection as $orderPayment) {
$orderId = $orderPayment->getParentId();
$order = Mage::getModel('sales/order')->load($orderId);
$customerId = $order->getCustomerId();
}
After that you can load customer's model by customerId

How to restrict other shipping methods - Magento

By $result = Mage::getModel('shipping/rate_result'); method I can create a new shipping rate. But How can I restrict other shipping methods in this action?
Grab All active shipping methods, loop through each one, and see if it matches your desired one
$activeCarriers = Mage::getModel('shipping/config')->getActiveCarriers();
foreach($activeCarriers as $code => $method) {
if($code == 'yourcode') {
$result = Mage::getModel('shipping/rate_result');
}
}

Magento how to create shipping label dynamically

I need to generate shipping label dynamically once order is placed.
I have fedex shipping method configured and works fine for setting the order status to shipping once order placed and from admin am able to create shipping label manually and it is giving pdf when i click Print Shipping label after shipping label created.
Now this process needs to be automated - how i can able to create hipping labels dynamically? Is there any observer or class overwriting examples. Please help me to create shipping label dynamically
If you want an official Shipping Label with bar code you will need to purchase an extension like this one:
http://www.cobbconsulting.net/magento-fedex-extension.html
The extension has a cahcing feature where it will store all of your shipping labels so you can easily re-print them at anytime.
As know, you can print shipping label only when have invoice. We want let you know extension can help you solved. Or you can check our code for create
Magento 1: http://www.mlx-store.com/magento-extensions/shipping/print-shipping-label.html
Magento 2: http://www.mlx-store.com/magento2-extensions/shipping/print-shipping-label-for-magento-2.html
or use code
Below is the controller.
public function printShippingLabelAction(){
$ids= $this->getRequest()->getPost('order_ids');
if (!empty($invoicesIds)) {
$orders = Mage::getResourceModel('sales/order')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $ids))
->load();
if (!isset($pdf)){
$pdf = Mage::getModel('sales/order_pdf_order')->getPdf($orders );
} else {
$pages = Mage::getModel('sales/order_pdf_order')->getPdf($orders );
$pdf->pages = array_merge ($pdf->pages, $pages->pages);
}
return $this->_prepareDownloadResponse('order'.Mage::getSingleton('core/date')->date('Y-m-d_H-i-s').
'.pdf', $pdf->render(), 'application/pdf');
}
$this->_redirect('*/*/');
}
Create model
class Mage_Sales_Model_Order_Pdf_Order extends Mage_Sales_Model_Order_Pdf_Invoice
{
public function getPdf($orders = array())
{
$this->_beforeGetPdf();
$this->_initRenderer('order');
$pdf = new Zend_Pdf();
$this->_setPdf($pdf);
$style = new Zend_Pdf_Style();
$this->_setFontBold($style, 10);
foreach ($orders as $order) {
$page = $this->newPage();
$this->insertLogo($page, $order->getStore());
$this->insertAddress($page, $order->getStore());
$this->insertOrder(
$page,
$order,
Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId())
);
$this->insertDocumentNumber(
$page,
Mage::helper('sales')->__('Order # ') . $order->getIncrementId()
);
$this->_drawHeader($page);
foreach ($order->getAllItems() as $item){
if ($item->getOrderItem()->getParentItem()) {
continue;
}
$this->_drawItem($item, $page, $order);
$page = end($pdf->pages);
}
$this->insertTotals($page, $order);
if ($order->getStoreId()) {
Mage::app()->getLocale()->revert();
}
}
$this->_afterGetPdf();
return $pdf;
}}

Add extra item to the cart (observer)

I try to add a extra product to the cart. I have created a observer for this.
<?php
class WP_Plugadapter_Model_Observer
{
public function hookToControllerActionPostDispatch($observer)
{
if($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')
{
Mage::dispatchEvent("add_to_cart_after", array('request' => $observer->getControllerAction()->getRequest()));
}
}
public function hookToAddToCartAfter($observer)
{
$request = $observer->getEvent()->getRequest()->getParams();
$_product = Mage::getModel('catalog/product')->load($request['product']);
$extra_functions = $_product->getExtra_functions();
if(!empty($extra_functions)){
$extra_functions = explode(',', $extra_functions);
if(array_search('121', $extra_functions)){
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
if (!$cart->getQuote()->getHasError()){
Mage::log("Product ADD TO CART is added to cart.");
}else{
Mage::log("BOEM");
}
}
}
}
}
When i check mine system log i see the following log message. Product ADD TO CART is added to cart.
I have no clue what i'm doing wrong. When a load the script standalone it's working fine.
For example:
<?php
include_once '../app/Mage.php';
Mage::app();
umask(0);
$session = Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
$cart->save();
Is it possible that in a observer you have it to do it in a different way?
The problem is that cart's quote object is not saved to the database and later in the request processing is overwritten by the quote object from the session. Why the cart quote is not saved is quite confusing. The save method of the quote model expects that the internal property _hasDataChanges is set to true. This property is, however, remains at false, even though a product was added to the quote.
You can force that property to be set to true by adding some data (any property would do) to the quote using the setData method:
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct(10934, 1);
//force _hasDataChanges to true
$cart->getQuote()->setData('updated', true);
$cart->save();
Alternatively you can use the checkout session quote object to add a product to the cart
if(array_search('121', $extra_functions)){
$cart = Mage::getSingleton('checkout/cart');
$qty = 1;
$quote = Mage::getSingleton('checkout/session')->getQuote()
->addProduct(
Mage::getModel('catalog/product')->load(10934),
$qty)
->save();
$cart->save();
if (!$cart->getQuote()->getHasError()){
Mage::log("Product ADD TO CART is added to cart.");
}else{
Mage::log("BOEM");
}
}

How can I retrieve shopping cart contents, shipping details and ancillary fees (tax, discounts, etc) for my custom payment method?

I'm tasked to write a custom payment method for Magento CE, and tinkered with it for the last couple of weeks. Although I'm an experienced developer, this was my first serious brush with php and Magento itself.
Please note this a web payment gateway, so I'm using
public function getOrderPlaceRedirectUrl() { ... }
In my Payment Method Model to redirect the customer to the external url successfully.
The issue that kept me stuck for a full day is how to retrieve checkout shopping cart contents, shipping details and ancillary fees (tax, discounts, etc). This info needs to be sent to the payment method API.
The code I've been using in my Payment Method Model is something like this:
$order_id = Mage::getSingleton("checkout/session")->getLastRealOrderId();
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$oBillingAddress = $order->getBillingAddress(); //this works ok
$total = number_format($order->getBaseGrandTotal(), 2, '', ''); //this too
/* The following code won't work */
$oShippingAddress = $order->getShippingAddress(); // is unset!?
$oShippingAddress->getSameAsBilling(); //HOW can I check this?
$amount = array();
$quantity = array();
$sku = array();
$description = array();
$cart_items = $order()->getAllVisibleItems();
foreach ($cart_items as $item) {
$amount[] = number_format($item->getPrice(), 2, '', ''); //ok
$quantity[] = $item->getQtyToInvoice(); // is empty...
$sku[] = $item->getSku(); // nothing either??
$description[] = $item->getName(); //this is working
}
Please, wizards of Magento, tell what am I doing wrong here?
Magento dev has been very frustrating, mainly for its lack of straightforward documentation. I'm sure it's very customizable and what not, but the abuse of php's magic functions and it's cumbersome structure has been challenging - at the least.
I think you need to get the quote. Something like this should work:
$quote = Mage::getSingleton('checkout/session')->getQuote();
$items = $quote->getAllVisibleItems();
foreach ($items as $item) {
$amount[] = number_format($item->getPrice(), 2, '', ''); //ok
$quantity[] = $item->getQtyToInvoice(); // is empty...
$sku[] = $item->getSku(); // nothing either??
$description[] = $item->getName(); //this is working
}
If you still need the order, let me know..

Resources