Remove 1 qty of product Magento - ajax

I'm implementing custom sidebar cart of Magento, which will feature incrementation and decrementaton of products in this cart, refreshed in ajax.
AJAX is possible to work just with HTML, isn't working as return value is whole cart.
I'm adding 1 quantity of product using CartController: /checkout/cart/add/uenc/aHR0cDovL3BsYXkudGhlaGFwcHlwZWFyLm5ldC9zaG9wL2RyaW5rcw,,/product/586/
Is it possible to just remove 1 quantity of product? Do I have to create new custom function in CartController?
Thanks,
Adam
ANSWER:
you can call this link with
/checkout/cart/remove/id/ $ITEMID /uenc/aHR0cDovL3BsYXkudGhlaGFwcHlwZWFyLm5ldC9zaG9w/ (depending on your magento settings link could be different)
Copy into CartController.php
public function removeAction()
{
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
$product = $this->_initProduct();
$related = $this->getRequest()->getParam('related_product');
/**
* Check product availability
*/
$id = (int) $this->getRequest()->getParam('id');
$items = $cart->getItems();
foreach ($items as $item) {
if ($item->getProduct()->getId() == $id) {
if( $item->getQty() == 1 ){
$cart->removeItem($item->getItemId())->save();
}
else if($item->getQty() > 1){
$item->setQty($item->getQty() - 1);
$cart->save();
}
break;
}
}
$this->_getSession()->setCartWasUpdated(true);
/**
* #todo remove wishlist observer processAddToCart
*/
Mage::dispatchEvent('checkout_cart_add_product_complete',
array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
if (!$this->_getSession()->getNoCartRedirect(true)) {
if (!$cart->getQuote()->getHasError()){
//$message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($product->getName()));
//$this->_getSession()->addSuccess($message);
}
$this->_goBack();
}
} catch (Mage_Core_Exception $e) {
if ($this->_getSession()->getUseNotice(true)) {
$this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
} else {
$messages = array_unique(explode("\n", $e->getMessage()));
foreach ($messages as $message) {
$this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
}
}
$url = $this->_getSession()->getRedirectUrl(true);
if ($url) {
$this->getResponse()->setRedirect($url);
} else {
$this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
}
} catch (Exception $e) {
$this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
Mage::logException($e);
$this->_goBack();
}
}

Related

Programmatically created order not sending order receipt email

When I make an order normally through the Magento store, I immediately get an email receipt. When I create an order through my custom code, No email is sent.
Here's my code:
private function _submitOrder($customer, $billing, $shipping, $products, $payment, $coupon, $finalize){
if($coupon && $this->_checkCouponValidity($coupon)){
$quote = Mage::getModel('sales/quote')->setCouponCode($coupon)->load();
}else{
$quote = Mage::getModel('sales/quote');
}
foreach($products as $item) {
if($item['qty']>0){
$product = Mage::getModel('catalog/product');
$productId = Mage::getModel('catalog/product')->getIdBySku($item['sku']);
$product->load($productId);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product);
$quoteItem->setQuote($quote);
$quoteItem->setQty($item['qty']);
$quote->addItem($quoteItem);
}
}
$quote->getBillingAddress()
->addData($billing);
$quote->getShippingAddress()
->addData($billing)
->setShippingMethod('tablerate_bestway')
->setPaymentMethod('authorizenet')
->setCollectShippingRates(true);
$quote->setCheckoutMethod('guest')
->setCustomerId(null)
->setCustomerEmail($quote->getBillingAddress()->getEmail())
->setCustomerIsGuest(true)
->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
$quote->collectTotals();
$quote->save();
$convertQuote = Mage::getSingleton('sales/convert_quote');
$quotePayment = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
$quotePayment->setMethod('authorizenet');
$order = $convertQuote->addressToOrder($quote->getShippingAddress());
if($finalize){
$orderPayment = $convertQuote->paymentToOrderPayment($quotePayment);
$order->setBillingAddress($convertQuote->addressToOrderAddress($quote->getBillingAddress()));
$order->setShippingAddress($convertQuote->addressToOrderAddress($quote->getShippingAddress()));
$order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
$order->getPayment()->setCcNumber($payment['ccNumber']);
$order->getPayment()->setCcType($payment['ccType']);
$order->getPayment()->setCcExpMonth($payment['ccExpMonth']);
$order->getPayment()->setCcExpYear($payment['ccExpYear']);
$order->getPayment()->setCcLast4($payment['ccLast4']);
}
Mage::log("loop quote items");
foreach ($quote->getAllItems() as $item) {
$orderItem = $convertQuote->itemToOrderItem($item);
if ($item->getParentItem()) {
$orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
}
$order->addItem($orderItem);
}
try {
if($finalize){
$order->place();
$order->save();
if ($order->getCanSendNewEmailFlag()) {
try {
$order->sendNewOrderEmail();
} catch (Exception $e){
Mage::log($e->getMessage());
}
}
return $order;
}
else{
return $order;
}
} catch (Exception $e){
return $e->getMessage();
}
}
I figured out the problem.
This line:
$quote->getBillingAddress()->getEmail()
was returning nothing. I wasn't setting a shipping email when creating $billing. Whoops.

Magento: Get all updated cart items data in AJAX response

My Cart is looking like this with popup window:
I am updating multiple configurable items in cart with custom options at a time by AJAX Call.
But I am not able to get all items data back to AJAX response.
I am getting just 1st itemPrice and rowTotal. For remaining items itemPrice and rowTotal are set to 0.
Code:
public function updateItemOptionsAction()
{
$cartData = $this->getRequest()->getParam('cart');
Mage::log($cartData);
if (is_array($cartData)) {
$result = array();
$result['data'] = array();
foreach ($cartData as $index => $data) {
$cart = $this->_getCart();
$params = $data;
$id = (int)$data['id'];
if (!isset($params['options'])) {
$params['options'] = array();
}
$result['data'][$index] = array();
$oldQty = null;
$kitType = $params['efk_kType'];
$params['super_attribute'] = array($data['sAttr']=>$kitType);
unset($params['sAttr']);
$stock = null;
try {
if (isset($params['qty'])) {
$product = Mage::getModel("catalog/product")->load($params['product']);
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product);
foreach($childProducts as $cProd){
if($cProd->getKitType() == $kitType){
$stock = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($cProd)->getQty());
}
}
if(intval($params['qty']) > $stock){
$oldQty = intval($params['qty']);
$params['qty'] = $stock;
$result['data'][$index]['revised'] = true;
}
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
$quoteItem = Mage::getSingleton('checkout/cart')->getQuote()->getItemById($id);
if (!$quoteItem) {
Mage::throwException($this->__('Quote item is not found.'));
}
//Its going to infinity loop duwe to Varien Object need to check later
//$item = $cart->updateItem($id, new Varien_Object($params));
$item = Mage::getSingleton('checkout/cart')->updateItem($id, $params);
if (is_string($item)) {
Mage::throwException($item);
}
if ($item->getHasError()) {
Mage::throwException($item->getMessage());
}
Mage::log('hi2');
$related = $params['related_product'];
if (!empty($related)) {
Mage::getSingleton('checkout/cart')->addProductsByIds(explode(',', $related));
}
Mage::getSingleton('checkout/cart')->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::dispatchEvent('checkout_cart_update_item_complete',
array('item' => $item, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);
$cart->getQuote()->setTotalsCollectedFlag(false);
Mage::getSingleton('checkout/cart')->init();
if (!Mage::getSingleton('checkout/session')->getNoCartRedirect(true)) {
if (!Mage::getSingleton('checkout/cart')->getQuote()->getHasError()) {
Mage::log('hi4');
$result['success'] = true;
if($oldQty > $item->getQty()){
$message = $this->__('%s has been revised due to stock limitations. You may proceed with the order for the revised quantity.', Mage::helper('core')->escapeHtml($item->getProduct()->getName()));
}else{
$message = $this->__('%s was updated in your shopping cart.', Mage::helper('core')->escapeHtml($item->getProduct()->getName()));
}
$result['data'][$index]['message'] = $message;
$result['data'][$index]['itemId'] = $item->getId();
$result['data'][$index]['itemPrice'] = Mage::helper('checkout')->formatPrice($item->getCalculationPrice());
$result['data'][$index]['qty'] = $item->getQty();
$result['data'][$index]['rowTotal'] = Mage::helper('checkout')->formatPrice($item->getRowTotal());
}
}
} catch (Mage_Core_Exception $e) {
$result['success'] = false;
$result['data'][$index]['success'] = 'qty';
$result['data'][$index]['message'] = $e->getMessage();
} catch (Exception $e) {
$result['success'] = false;
$result['data'][$index]['message'] = $e->getMessage();
$result['data'][$index]['secondMessage'] = $this->__('Cannot update the item.');
}
}
$result['data']['grandTotal'] = Mage::helper('checkout')->formatPrice(Mage::getSingleton('checkout/cart')->getQuote()->getGrandTotal());
$result['data']['totalItems'] = Mage::getSingleton('checkout/cart')->getSummaryQty();
$totals = Mage::getSingleton('checkout/cart')->getQuote()->getTotals();
$result['data']['subTotal'] = Mage::helper('checkout')->formatPrice($totals['subtotal']->getValue());
if(isset($totals['discount']) && $totals['discount']->getValue()){
$result['data']['discount'] = Mage::helper('checkout')->formatPrice($totals['discount']->getValue());
}else{
$result['data']['discount'] = Mage::helper('checkout')->formatPrice(0);
}
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
AJAX Response I'm getting
{
"data": {
"1187": {
"success": true,
"message": "THREE PHASE SOLID STATE RELAY WITH ZVS was updated in your shopping cart.",
"itemId": "1191",
"itemPrice": "<span class=\"price\">20b9 3,799</span>",
"qty": 1,
"rowTotal": "<span class=\"price\">20b9 3,799</span>",
"forLoop": "yes"
},
"1189": {
"success": true,
"message": "AUTO INTENSITY CONTROL OF STREET LIGHTS was updated in your shopping cart.",
"itemId": "1193",
"itemPrice": "<span class=\"price\">20b9 0</span>",
"qty": 1,
"rowTotal": "<span class=\"price\">20b9 0</span>",
"forLoop": "yes"
},
"grandTotal": "<span class=\"price\">20b9 8,798</span>",
"totalItems": 2,
"subTotal": "<span class=\"price\">20b9 8,798</span>",
"discount": "<span class=\"price\">20b9 0</span>"
}
}
I am getting itemPrice and rowTotal for 2nd item as 0. Every time I am getting correct values for 1st item only. If i am update 5 items at a time (say for example) then i am getting correct values for 1st item and for remianing items i am getting 0's.
If i refresh cart once i get AJAX response, then it is showing itemPrice and rowTotal updated values correctly for all items.
Note: 20b9 is HEX Code for Indian Rupee symbol
Please point out where i am wrong.
Thanks in advance.
You're working tooooo hard... try updating the items in the controller server side as you sort of are, saving the current quote... and then just have a controller method that loads the current .phtml and returns the html as json for the cart block, and replace the whole cart html block (div) with the new one.
At the end of your controller method
$this->getResponse()->setBody( json_encode(
array("html" =>
Mage::app()->getLayout()->createBlock('checkout/[[whatevertag_is_to_cart_div phtml block]]')->toHtml()
)
);

unable to add invoice fee tax in magento order total

I am working on magento 1.7. i am working on payment gateway where i have added invoice fee now i have to add tax of invoice fee in tax group
please anyone help to solve this problem here is following my code i have tried to add tax amount in taxes but still not working may be i am doing something wrong
<?php
class ***_******_Model_Quote_TaxTotal
extends Mage_Sales_Model_Quote_Address_Total_Tax
{
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$quote = $address->getQuote();
if (($quote->getId() == null)
|| ($address->getAddressType() != "shipping")
) {
return $this;
}
$payment = $quote->getPayment();
if (($payment->getMethod() != 'invoice')
&& (!count($quote->getPaymentsCollection()))
) {
return $this;
}
try {
/**
* Instead of relying on hasMethodInstance which would not always
* work when i.e the order total is reloaded with coupon codes, we
* try to get the instance directly instead.
*/
$methodInstance = $payment->getMethodInstance();
} catch (Mage_Core_Exception $e) {
return $this;
}
if (!$methodInstance instanceof Mage_Payment_Model_Method_Abstract) {
return $this;
}
if ($methodInstance->getCode() != 'invoice') {
return $this;
}
$fee = $methodInstance->getAddressInvoiceFee($address);
if(Mage::getStoreConfig('payment/invoice/tax_class') == '' ){
return $this;
}
$invoiceFee = $baseInvoiceFee = Mage::getStoreConfig('payment/invoice/_fee');
$fee = Mage::helper('invoice')->getInvoiceFeeArray($invoiceFee, $address, null);
if (!is_array($fee)) {
return $this;
}
$address->setTaxAmount($address->getTaxAmount() + 5454+ $fee['taxamount']);
$address->setBaseTaxAmount(
$address->getBaseTaxAmount() + 5454+ $fee['base_taxamount']
);
$address->setInvoiceTaxAmount($fee['taxamount']);
$address->setBaseInvoiceTaxAmount($fee['base_taxamount']);
return $this;
}
}
and this is config.xml
<sales>
<quote>
<totals>
<fee>
<class>invoice/sales_quote_address_total_fee</class>
</fee>
<invoicetax>
<class>invoice/quote_taxTotal</class>
<after>subtotal,discount,shipping,tax</after>
<before>grand_total</before>
</invoicetax>
</totals>
</quote>
</sales>
your code must be following i have following i modified your code
<?php
class *****_******_Model_Quote_TaxTotal extends Mage_Sales_Model_Quote_Address_Total_Tax
{
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$collection = $address->getQuote()->getPaymentsCollection();
if ($collection->count() <= 0 || $address->getQuote()->getPayment()->getMethod() == null) {
return $this;
}
$paymentMethod = $address->getQuote()->getPayment()->getMethodInstance();
if ($paymentMethod->getCode() != 'invoice') {
return $this;
}
$store = $address->getQuote()->getStore();
$items = $address->getAllItems();
if (!count($items)) {
return $this;
}
$custTaxClassId = $address->getQuote()->getCustomerTaxClassId();
$taxCalculationModel = Mage::getSingleton('tax/calculation');
/* #var $taxCalculationModel Mage_Tax_Model_Calculation */
$request = $taxCalculationModel->getRateRequest(
$address,
$address->getQuote()->getBillingAddress(),
$custTaxClassId,
$store
);
$InvoiceTaxClass = Mage::helper('invoice')->getInvoiceTaxClass($store);
$InvoiceTax = 0;
$InvoiceBaseTax = 0;
if ($InvoiceTaxClass) {
if ($rate = $taxCalculationModel->getRate($request->setProductClassId($InvoiceTaxClass))) {
if (!Mage::helper('invoice')->InvoicePriceIncludesTax()) {
$InvoiceTax = $address->getFeeAmount() * $rate/100;
$InvoiceBaseTax= $address->getBaseFeeAmount() * $rate/100;
} else {
$InvoiceTax = $address->getPaymentTaxAmount();
$InvoiceBaseTax= $address->getBasePaymentTaxAmount();
}
$InvoiceTax = $store->roundPrice($InvoiceTax);
$InvoiceBaseTax= $store->roundPrice($InvoiceBaseTax);
$address->setTaxAmount($address->getTaxAmount() + $InvoiceTax);
$address->setBaseTaxAmount($address->getBaseTaxAmount() + $InvoiceBaseTax);
$this->_saveAppliedTaxes(
$address,
$taxCalculationModel->getAppliedRates($request),
$InvoiceTax,
$InvoiceBaseTax,
$rate
);
}
}
if (!Mage::helper('invoice')->InvoicePriceIncludesTax()) {
$address->setInvoiceTaxAmount($InvoiceTax);
$address->setBaseInvoiceTaxAmount($InvoiceBaseTax);
}
$address->setGrandTotal($address->getGrandTotal() + $address->getPaymentTaxAmount());
$address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBasePaymentTaxAmount());
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$store = $address->getQuote()->getStore();
/**
* Modify subtotal
*/
if (Mage::getSingleton('tax/config')->displayCartSubtotalBoth($store) ||
Mage::getSingleton('tax/config')->displayCartSubtotalInclTax($store)) {
if ($address->getSubtotalInclTax() > 0) {
$subtotalInclTax = $address->getSubtotalInclTax();
} else {
$subtotalInclTax = $address->getSubtotal()+ $address->getTaxAmount() -
$address->getShippingTaxAmount() - $address->getPaymentTaxAmount();
}
$address->addTotal(
array(
'code' => 'subtotal',
'title' => Mage::helper('sales')->__('Subtotal'),
'value' => $subtotalInclTax,
'value_incl_tax' => $subtotalInclTax,
'value_excl_tax' => $address->getSubtotal()
)
);
}
return $this;
}
}

How to create cron for partial invoice of an order

I want to create partial invoice for downloadable product of an order based on release date of that product, I have got so many solutions to create partial invoice but it's create the invoice for all product, not for selected one.
Here is the solution but don't forget to change condition according your requirement.
<?php
require_once '../app/Mage.php';
Mage::app('admin');
CapturePayment::createInvoice();
class CapturePayment {
public static function createInvoice() {
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', array('processing'));
foreach ($orders as $order) {
$orderId = $order->getIncrementId();
try {
if (!$order->getId() || !$order->canInvoice()) {
echo 'Invoice is not allowed for order=>.' . $orderId . "\n";
continue;
}
$items = $order->getAllItems();
$partial = false;
$qtys = array();
foreach ($items as $item) {
/* Check wheter we can create invoice for this item or not */
if (!CapturePayment::canInvoiceItem($item, array())) {
continue;
}
/* Get product id on the basis of order item id */
$productId = $item->getProductId();
/* If product is tablet then don't create invoice for the same */
/* Put your condition here for items you want to create the invoice e.g.*/
/* Load the product to get the release date */
$productDetails = Mage::getModel('catalog/product')->load($productId);
/* create your $qtys array here like */
$qtys['orderItemId'] = 'quantityOrdered';
/* NOTE: But if you don't want to craete invoice for any particular item then pass the '0' quantity for that item and set the value for $partial = true if some product remain in any order*/
}
/* Prepare the invoice to capture/create the invoice */
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($qtys);
if (!$invoice->getTotalQty()) {
continue;
}
$amount = $invoice->getGrandTotal();
if ($partial) {
$amount = $invoice->getGrandTotal();
$invoice->register()->pay();
$invoice->getOrder()->setIsInProcess(true);
$history = $invoice->getOrder()->addStatusHistoryComment('Partial amount of $' . $amount . ' captured automatically.', false);
$history->setIsCustomerNotified(true);
$invoice->sendEmail(true, '');
$order->save();
Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder())
->save();
$invoice->save();
} else {
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$invoice->getOrder()->setCustomerNoteNotify(true);
$invoice->getOrder()->setIsInProcess(false);
$invoice->getOrder()->setData('state', "complete");
$invoice->getOrder()->setStatus("complete");
$invoice->sendEmail(true, '');
$order->save();
Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder())
->save();
$invoice->save();
}
} catch (Exception $e) {
echo 'Exception in Order => ' . $orderId . '=>' . $e . "\n";
}
}
}
/* Check the invoice status for an item of an order */
public static function canInvoiceItem($item, $qtys=array()) {
if ($item->getLockedDoInvoice()) {
return false;
}
if ($item->isDummy()) {
if ($item->getHasChildren()) {
foreach ($item->getChildrenItems() as $child) {
if (empty($qtys)) {
if ($child->getQtyToInvoice() > 0) {
return true;
}
} else {
if (isset($qtys[$child->getId()]) && $qtys[$child->getId()] > 0) {
return true;
}
}
}
return false;
} else if ($item->getParentItem()) {
$parent = $item->getParentItem();
if (empty($qtys)) {
return $parent->getQtyToInvoice() > 0;
} else {
return isset($qtys[$parent->getId()]) && $qtys[$parent->getId()] > 0;
}
}
} else {
return $item->getQtyToInvoice() > 0;
}
}
}

How to get carrier by "shipping method" in magento

Typical magento's order has shipping_method. For example, in my case, it is "flatrate_flatrate".
How to bind carrier ("flatrate") with shipping_method?
Thanks for response.
public function getShippingMethodsList($quoteId, $store=null)
{
$quote = $this->_getQuote($quoteId, $store);
$quoteShippingAddress = $quote->getShippingAddress();
if (is_null($quoteShippingAddress->getId())) {
$this->_fault("shipping_address_is_not_set");
}
try {
$quoteShippingAddress->collectShippingRates()->save();
$groupedRates = $quoteShippingAddress->getGroupedAllShippingRates();
$ratesResult = array();
foreach ($groupedRates as $carrierCode => $rates ) {
$carrierName = $carrierCode;
if (!is_null(Mage::getStoreConfig('carriers/'.$carrierCode.'/title'))) {
$carrierName = Mage::getStoreConfig('carriers/'.$carrierCode.'/title');
}
foreach ($rates as $rate) {
$rateItem = $this->_getAttributes($rate, "quote_shipping_rate");
$rateItem['carrierName'] = $carrierName;
$ratesResult[] = $rateItem;
unset($rateItem);
}
}
} catch (Mage_Core_Exception $e) {
$this->_fault('shipping_methods_list_could_not_be_retrived', $e->getMessage());
}
return $ratesResult;
}

Resources