How to restrict other shipping methods - Magento - 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');
}
}

Related

Allow only one product in cart

Running joomla 3.4.8 and VM 3.0.12
I need to allow only one product in cart. I mean, if the client add a product to the cart and then if he/she wants to add another product don't allow it showing error message like " you can't have more than one product in cart"
You can alter the add() function in the cart controller page to achieve it. You can write code something like this
$cart = VirtueMartCart::getCart();
$prdata = $cart->cartProductsData;
$qty = 0;
foreach($prdata as $pdata)
{
$qty = $qty + $pdata['quantity'];
}
if ($cart) {
$virtuemart_product_ids = vRequest::getInt('virtuemart_product_id');
$error = false;
if($qty>=1)
{
$msg = vmText::_('you can't have more than one product in cart');
$type = 'warning';
}
else
{
$cart->add($virtuemart_product_ids,$error);
Use the same logic in updatecart() function in the same page.
If you using Fancypopup addtocart, then use the code in addJS function instead of add() function.

first time customer free shipping on your first order

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;
}

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;
}}

Magento: Increase "Qty" upon cancel a shipped order

I'm on Magento 1.7.0.2. I'm using "Cash On Delivery" as my payment method.I'll tell you exactly the steps That i follow on any order. When an order is placed(Qty decreased 1 item), I create a shipment for it and if customer paid the order grand total price. I create an invoice for that order.
My problem, If an order is placed(Qty decreased 1 item), I create a shipment for this order. If the customer refused to pay, I open this order and "Cancel" it and on this case the "Qty" doesn't increase so How can I make it increase?
If order status is Processing
Create a custom module with observer for 'order_cancel_before' (see example # Change Magento default status for duplicated products change <catalog_model_product_duplicate> to <order_cancel_before>
since <order_cancel_before> is not defined in app/code/core/Mage/Sales/Model/Order.php
You could override/rewrite order model class see e.g http://phprelated.myworks.ro/how-to-override-rewrite-model-class-in-magento/
In your local module do
public function cancel()
{
if ($this->canCancel()) {
Mage::dispatchEvent('order_cancel_before', array('order' => $this));
$this->getPayment()->cancel();
$this->registerCancellation();
Mage::dispatchEvent('order_cancel_after', array('order' => $this));
}
return $this;
}
Or you could create a new method increaseProductQty() in your model and copy the code below into it (this way you would not need an observer). Then replace the line Mage::dispatchEvent('order_cancel_before'... with $this->increaseProductQty()
In your observer method (pseudo code)
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = $item->getQty();
// you need to check order status to make sure it processing
//$order->getStatus() (assuming you are canceling entire order)
//$order->getPayment();
$product = Mage::getModel('catalog/product')->load($product_id);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->load($product_id);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}
If you have issue with updating stock see Set default product values when adding new product in Magento 1.7
Reference http://pragneshkaria.com/programatically-change-products-quantity-after-order-cancelled-magento/
If order status is Pending
Take a look at System > Configuration > Inventory
Set Items’ Status to be In Stock When Order is Cancelled — Controls whether products in pending orders automatically return to the stock if orders are cancelled. Scope: STORE VIEW.
Read more #
How to Manage Magento Store Inventory?
ADMIN: System → Configuration → Inventory Tab
Thanks to R.S as he helped me more & more.
I followed all instructions on R.S's reply https://stackoverflow.com/a/13330543/1794834 and I've only changed the observer code. Here is the observer code that worked with me on Magento 1.7.0.2.
$curr_date = date('Y-m-d H:i:s');
$order = $observer->getEvent()->getOrder();
foreach ($order->getItemsCollection() as $item)
{
$productId = $item->getProductId();
$qty = (int)$item->getQtyOrdered();
$product = Mage::getModel('catalog/product')->load($productId);
$stock_obj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockData = $stock_obj->getData();
$product_qty_before = (int)$stock_obj->getQty();
$product_qty_after = (int)($product_qty_before + $qty);
$stockData['qty'] = $product_qty_after;
/*
* it may be case that admin has enabled product add in stock, after product sold,
* he set is_in_stock = 0 and if order cancelled then we need to update only qty not is_in_stock status.
* make a note of it
*/
if($product_qty_after != 0) {
$stockData['is_in_stock'] = 1;
}else{
$stockData['is_in_stock'] = 0;
}
$productInfoData = $product->getData();
$productInfoData['updated_at'] = $curr_date;
$product->setData($productInfoData);
$product->setStockData($stockData);
$product->save();
}

Use Tierprices of other customergroup

i'm trying to get my pricing extension working but i'm stucking at the tierprices.
If a visitor comes to my site i added a paramter to the cookie.
Example: http://www.foo.com/category/product.html?sidx=5
Now the visitor enters my site with the sidx parameter. Now the Observer: catalog_product_get_final_price sets the finalprice with the first tierprice of this Product of Customergroup 5.
The Customergroup of the visitor is still NOT LOGGED IN. Now i need the other tierprices of the Customergroup ID 5 without registering.
I have googled a lot to find a solution for that but i don't get the point.
Best Regards
boti
If you want to get the Tier prices for any product:
$product = Mage::getModel('catalog/product')->load(PRODUCT_ID_HERE);
$tierPrices = $product->getData('tier_price');
if (!is_array($tierPrices)) {
$tierPrices = array();
}
$result = array();
foreach ($tierPrices as $tierPrice) {
$row = array();
$row['customer_group_id'] = (empty($tierPrice['all_groups']) ? $tierPrice['cust_group'] : 'all' );
$row['website'] = ($tierPrice['website_id'] ? Mage::app()->getWebsite($tierPrice['website_id'])->getCode() : 'all');
$row['qty'] = $tierPrice['price_qty'];
$row['price'] = $tierPrice['price'];
$result[] = $row;
}
// $result now has an array of all tier prices...
// looking for cust_group = 5 would be trivial:
foreach ($tierPrices as $tierPrice) {
if($tierPrice['cust_group'] == 5) {
return $tierPrice; // this is what you want..
}
}

Resources