Automatic onepage checkout Magento - Shipping method - php

I'm working on the automation of the shipping method, it seems to be working fine, but problem is:
Every 2nd order there is same error "Please specify a shipping method"
Can it be session error? Should variables be send over get-post or retrieved from Magento in other way?
Here is a code:
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
echo $customAddress = Mage::getModel('customer/address')->load($customer->getDefaultBilling());
$quote = Mage::getSingleton('checkout/session')->getQuote()->setBillingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));
$cart = Mage::getSingleton('checkout/cart');
$storeId = Mage::app()->getStore()->getId();
$checkout = Mage::getSingleton('checkout/type_onepage');
$checkout->initCheckout();
$checkout->saveCheckoutMethod('register');
$checkout->saveShippingMethod('matrixrate_matrixrate_35');
$checkout->savePayment(array('method'=>'pay'));
Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->setShippingMethod('matrixrate_matrixrate_35');
try {
if ($checkout->saveOrder()) {echo "SUCCESSSSS!!!";}}
catch (Exception $ex) {
echo $ex->getMessage();
}
/* Clear the cart */
$cart->truncate();
$cart->save();
$cart->getItems()->clear()->save();
Mage::getSingleton('checkout/session')->clear();
It's using custom payment gateway called Pay and I'm using matrixrate extension for shipping, althought there is same eror on flatrate_flatrate so I don'd consider it as extension issue.
Thanks!!!
Adam

Issue: Cart items still displaying after order splitting
1.Save quote collection in shipping method step..
$quote->collectTotals()->save();
2.Add the below steps in the last section of the order split.
$quote->setIsActive(false);
$quote->save();

PROBLEM IS SOLVED.
Looks like there has been issue with sessions/time, server didn't process everything at once.
I had to add on cart-total page:
$checkout->getQuote()->getShippingAddress()->setShippingMethod('matrixrate_matrixrate_35');
$checkout->saveShippingMethod('matrixrate_matrixrate_35');
To make sure that shipping is set, also:
session is cleared at success page manually.
There has been a bug with total quote amount, so I just redesigned script to count it properly.
Many Thanks for help!!
Adam

Related

Magento 1.7 After (or before) cart update get old and new quotes of each product

I'm trying really hard to learn Magento, but I need some help.
I want to track product quotes changes in cart after clicking to "update cart" button. I found nice events called checkout_cart_update_items_after and checkout_cart_update_items_before. But I don't know how to get info about cart quotes changes in observer.
The best I achieved was
public function Mytestupd($observer) {
$product = $observer->getData('cart');
$quote = $product->getData('quote');
$items = $quote->getAllVisibleItems();
foreach($items as $item) {
$prsku .= $item->getQty()." ";
}
}
Which of course just gives me old (or new, depends on event) quote of each item.
At the end I'd like to do
echo Mage::getSingleton('checkout/session')->addSuccess("Cart Updated, your changes are: ".$changes);
where $changes is something like
"productOne: 3 items added; productTwo: 4 items removed"
I hope I was clear enough and someone will help me!
It's great to have session variables. So I could do
Mage::getSingleton('core/session')->setCartItems($myVals);
in checkout_cart_update_items_before event Observer and later do
$oldcart = Mage::getSingleton('core/session')->getCartItems();
in checkout_cart_update_items_after event Observer

Product URL in a shared shopping cart in Magento multi-store websites

I have Magento multi-store websites that I want that the user will be able to add products to his shopping cart from all the website and pay once.
I successfully done it using this article.
But when the user click on the product in the shopping cart, he is not redirected to the right website. It's a limitation the described in the article at the end.
The link for editing items in the cart will redirect customer to
original cart website. It is possible to change it, you can override
getUrl method for cart items block or override controller.
I couldn't find any explaination to how to do this override.
Someone can help me do this?
Thanks
In my case i seen file of cart's item.phtml, it was using getproducturl().
So, I modified that method in file /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php line 152.
I made condition that if its main website don't change otherwise it adds stores code in url like www.example/store1/Producturl.
I hope This will Help You.
As You Require The File method. Check belowed code.
public function getProductUrl()
{
if (!is_null($this->_productUrl)) {
return $this->_productUrl;
}
if ($this->getItem()->getRedirectUrl()) {
return $this->getItem()->getRedirectUrl();
}
$product = $this->getProduct();
$option = $this->getItem()->getOptionByCode('product_type');
if ($option) {
$product = $option->getProduct();
}
$webids = $product->getWebsiteIds();
$wid = array();
foreach($webids as $webid){
$wid[] = $webid;
}
if(!in_array(1,$wid)){
$website = Mage::app()->getWebsite($wid[0])->getCode();
$org_url = $product->getUrlModel()->getUrl($product);
$mod_url = str_replace("www.example.com/index.php/","www.example.com/".$website."/index.php/",$org_url);
return $mod_url;
}
return $product->getUrlModel()->getUrl($product);
}
As my first website id was "1", that's why i used in_array(1.$wid). You Can use your main website id over there. and in str_replace u can use baseurl() method of main website instead of static value i used.

Magento - Insufficient Stock Notification on Product Page

I am using Magento 1.7.0.2. Whilst on the product page, if a customer attempts to add a quantity greater than we have in stock they receive a message stating ".. the requested quantity is not available".
Is there any way for magento to either email or log when this occurs? I.e. I receive an automatic email stating a customer has attempted to add X number of item X? This would allow me to identify lost sales due to us not having enough stock of a particular item?
Has anyone come across anything like this before or is this even possible?
Thank you in advance
Mike Prentice
yes this is possible
You have to code for this.
I came across this problem one time and what i have do like this below.
I have make one observer event to check if customer is requesting quantity more then available if so i sent email to admin.
What you can do is create one observer for chekout_cart_add_before event in this event you can put your logic.
Or otherwise you can use magento feature Backorders you can find this in inventory tab,if you enable this then customer can order even requested quantity > available quantity, customer can see one message in cart page about backorder.
There is no standart functionality to notify about low quantity products by email.
But there is RSS notification http://www.magentocommerce.com/wiki/modules_reference/english/mage_adminhtml/system_config/edit/cataloginventory
Extend this functionality to match your needs.
You could write some script which would parse RSS, and send email etc.
EDIT
Here is some extension you may like http://www.magentocommerce.com/magento-connect/low-stock-email-notification.html
But is is not free.
Here's how I've done it so that it sends a google analytics tracking event whenever a customer tries to order more than the available stock level.
First copy: app/code/core/Mage/CatalogInventory/Model/Stock/Item.php
To: app/code/local/Mage/CatalogInventory/Model/Stock/Item.php
so that you're not modifying a core file.
In app/code/local/Mage/CatalogInventory/Model/Stock/Item.php add this function
public function notifyOutOfStock($productId){
$session = Mage::getSingleton('checkout/session');
//Initialise as empty array, or use existing session data
$outOfStockItems = array();
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
}
try {
$product = Mage::getModel('catalog/product')->load($productId);
$sku = $product->getSKu();
if($sku){
//Add the current sku to our out of stock items (if not already there)
if(! isset($outOfStockItems[$sku]) ) {
$outOfStockItems[$sku] = 0;
}
}
} catch (Exception $e){
//Log your error
}
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
In that same file is another function called checkQuoteItemQty.
Inside that function you need to call your new function using $this->notifyOutOfStock($this->getProductId()); right after it sets each of the error messages and before the return statement.
So:
public function checkQuoteItemQty($qty, $summaryQty, $origQty = 0)
{
....
if ($this->getMinSaleQty() && ($qty) < $this->getMinSaleQty()) {
$result->setHasError(true)
->setMessage(
$_helper->__('The minimum quantity allowed for purchase is %s.', $this->getMinSaleQty() * 1)
)
->setQuoteMessage($_helper->__('Some of the products cannot be ordered in requested quantity.'))
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
}
.....
->setQuoteMessageIndex('qty');
//** Call to new function **
$this->notifyOutOfStock($this->getProductId());
return $result;
.....
What this does is add your product sku to an array in the checkout session.
This means you will have access to that info in the template file right after your page loads displaying the "Insufficient stock" notification.
So in one of your template files you can add some code to render the necessary JavaScript.
I've chosen header.phtml since it loads on every page. (Users can add quantities of items to the cart in the cart page as well as the product view page).
app/design/frontend/CUSTOMNAME/default/template/page/html/header.phtml
Somewhere down the bottom of the code add this:
<!-- GA tracking for out of stock items -->
<script>
try {
<?php
$session = Mage::getSingleton('checkout/session');
if ($session->getOutOfStock()){
$outOfStockItems = $session->getOutOfStock();
foreach($outOfStockItems as $sku=>$value) {
if($value==0){
//Render the GA tracking code
echo "_gaq.push(['_trackEvent', 'AddToCart', 'ProductQtyNotAvailable', '".$sku."']); \r\n";
//Set it to 1 so we know not to track it again this session
$outOfStockItems[$sku] = 1;
}
}
//Update the main session
Mage::getSingleton('checkout/session')->setOutOfStock($outOfStockItems);
}
?>
}
catch(err) {
//console.log(err.message);
}
</script>
Can confirm this works well and in my opinion is better than an email or RSS feed as you can analyse it along with the rest of your analytics.

product price for customer group

I hope someone can help me with this, I am trying to extract the product prices from magento based on customer group.
I am not using tier pricing, I simply have a product with a standard price and I have specified different prices for each customer group. For some reason I have been unable to extract this information.
I can see that the price mappings seems to be held in the table 'catalog_product_index_group_price' so I guess I could write direct SQL to extract these but I would much rather use the PHP Mage model to do this, or the V2 SOAP API.
I have tried many methods, currently im using something like below, but without success the price variable is always empty.
$rules = Mage::getResourceModel('catalogrule/rule');
$price = $rules->getRulePrice($now, $websiteId, $customer_group_id, $productID);
Please try the following
$product = Mage::getModel('catalog/product')->load($productId);
$groupPrices = $product->getData('group_price')
$groupPrices should now be an array with the data you are looking for.
the code didnt format well in the comment so here it is again!
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$pd = Mage::getModel('catalog/product')->load($productID);
$groupPrices = $pd->getData('group_price');
echo json_encode($groupPrices);
?>

How to delete a shipment from Magento that was created accidentally?

How can I remove a shipment that was created accidentally? I know there is no way to do it through the admin, but I'm trying to figure out what I need to do to revert an order back to a status of "Processing" along with all the old shipment information so it can be shipped at a later time. I'm hoping this can be done programmatically via PHP or even just directly in MySQL.
FYI, I'm using Magento version 1.1.8.
Try
require_once 'app/Mage.php';
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('default');
Mage::register('isSecureArea', 1);
//Update Order id
$orderId = xyz;
$order = Mage::getModel('sales/order')->load($orderId);
// check if has shipments
if(!$order->hasShipments()){
die('No Shipments');
}
//delete shipment
$shipments = $order->getShipmentsCollection();
foreach ($shipments as $shipment){
$shipment->delete();
}
// Reset item shipment qty
// see Mage_Sales_Model_Order_Item::getSimpleQtyToShip()
$items = $order->getAllVisibleItems();
foreach($items as $i){
$i->setQtyShipped(0);
$i->save();
}
//Reset order state
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, 'Undo Shipment');
$order->save();
echo 'Done';
$shipment = Mage::getModel('sales/order_shipment')->load($shippingOrderId);
$shipment->delete();
I assume this works. I haven't tried it.
I ended up purchasing the extension below which did the trick beautifully. Even though it doesn't say it supports versions prior to 1.3, it worked flawlessly on my setup of v1.1.8.
http://www.magentocommerce.com/magento-connect/delete-shipment-6498.html
Say you shipped an order in its entirety and didn't mean to. Just delete the INVOICE. It will remove the invoice and any of the shipments...SHIPMENTS plural!
So you have to be careful, if you have other shipments you will need to recreate them. Then invoice it again. Then it will be just like you never shipped anything and you can then SHIP whatever quantity you originally required.

Categories