Magento. How to get address in quote - magento

I try to get address, which customer select in address step in checkout.
I use in /app/code/local/Mandarin/AddressTypeDiscount/Block/Onepage/Review.php this code:
$checkout = Mage::getSingleton('checkout/session')->getQuote();
$bilAddress = $checkout->getBillingAddress();
$mylog = print_r($bilAddress, true);
Mage::log("addres:".$mylog, null, 'mygento.log');
but in my log file I get array of all customer`s addresses.
How I could get selected address in address step?
Thanks.

Your code seem to be correct see Get billing information in order review section of one page checkout in Magento .
print_r($bilAddress, true) will print the entire object, instead try $bilAddress->getData()
Try
$checkout = Mage::getSingleton('checkout/session')->getQuote();
$billAddress = $checkout->getBillingAddress();
Mage::log($billAddress->getData());

For incremental order address id based on order,
$order_id=Mage::getSingleton('checkout/session')->getLastRealOrderId();
$sales_order=Mage::getModel('sales/order')->load($order_id);
$billing_address_id=$sales_order->billing_address_id;
$shipping_address_id=$sales_order->shipping_address_id;
For address entity id of the order based on customer,
$quote = Mage::getSingleton('checkout/session')->getQuote();
$billing_address_id=$quote->getBillingAddress()->customer_address_id;
$shipping_address_id=$quote->getShippingAddress()->customer_address_id;
Source

Related

Magento - get Customer Address id of order shipping address

I got the shipping address of an order by $order->getShippingAddress()
$order = Mage::getModel('Mage_Sales_Model_Order');
$order->loadByIncrementId($ext_order_id);
$address = $order->getShippingAddress();
and i load the DefaultBillingAddress by
$address_default_billing = Mage::getSingleton('customer/session')->getCustomer()
->getDefaultBillingAddress();
Now i want to compare them, but there's the problem. If i do a getId() on both of them, they have different id's even though i chose the billing address for shipping in checkout, so they have to be the same but the id is different.. how can that appear ? Is there a way to get the customer-address-id of the current shippingaddress in checkout ?
by example: $address->getId() returns 44 and $address_default_billing->getId() return 6
the 6 is the right id for the customer address in the model, but the order-shipping-id is wrong.
you can get customer address by customer_address_id field in sales_flat_order_address table
here the code:
$order = Mage::getModel('sales/order');
$order->loadByIncrementId($ext_order_id);
$address = $order->getShippingAddress();
$address->getData('customer_address_id');
The address id will never be the same, because after an order is placed the address info will 'never' change while customer address change as customer move or change there shipping address.
Order address is store in sales_flat_order_address
Customer address is store in customer_address_entity*
To compare the address you want want to compare individual elements
$address_data = $address->getData()
$address_default_billing_data = $address_default_billing->getData()
$compare = array('firstname', ..., 'city');
foreach($compare as $c){
if($address_data[$c] != $address_default_billing_data[$c]){
//not equal
break;
}
}

Getting Order Number from Order Id in Magento?

I am using following code to get the Order Number from Order ID.
$orderId=64; //Order Id will be supplied dynamically
$order = Mage::getModel('sales/order')->load($orderId);
echo "Order Number is: ".$order['increment_id'];
I am getting the correct order number using this code like 100000067.
I wanted to know Is this the correct approach to use.
Please guide.
Thanks
This should work for you.
$orderId = 64;
$order = Mage::getModel('sales/order')->load($orderId);
echo $order->getIncrementId();
Cheers!

How do I get in Magento the current order ID whlie on the products page?

Im on the page showing the products I have avaliable in my Magento store. I want to get the current ID of the order should I continue on adding the order to my cart and buy it at the end.
Since the order per say does not exist and more than one person can buy at the same time, I would have to calculate the order ID someway while making sure that it does not conflict with another person making another order at the same time.
Another thing is like I mentioned that this is on the product listing page (/../../../../../template/catalog/product/list.phtml) so a "order" per say (I mentioned this before) does not exist.
How can I get the best possible way a order ID that is not in usage?
In Magento,on creation on cart a quote object is created and for each item you add there is a quote_item object.
You can get the current quote id from session
$session = Mage::getSingleton('checkout/session');
$quote_id = $session->getQuoteId();
And subsequently load the sales_quote object
$item_quote = Mage::getModel('sales/quote')->load($quote_id);
By this you could access any attribute related to the quote table easily.
For the items in cart you can access by
$items_in_cart = $quote->getAllItems();
And run this in a loop
foreach ($items_in_cart as $item) {
echo 'ID: '.$item->getProductId().'<br />';
echo 'Name: '.$item->getName().'<br />';
echo 'Sku: '.$item->getSku().'<br />';
echo 'Quantity: '.$item->getQty().'<br />';
echo 'Price: '.$item->getPrice().'<br />';
}
Hope this helps!
For more reference you can refer here
http://inchoo.net/ecommerce/magento/magento-quote-order-invoice-workflow/
Take a look here to create a order, you can create a order and when you save your new order get the next id for order.

Magento getProductUrl() is not returning the right url (random?)

I am using Magento 1.5.0.1 and the getProductUrl() function used in the cross sell and up sell blocks on the product page is throwing up different URL formats.
Either the correct url like:
/laptop-bag.html
Or the wrong one (well it works, but of course its not the rewrite URL):
/catalog/product/view/id/825/s/laptop-bag/category/16/
Sometimes both cross sell and up sell blocks return the correct URL, sometimes both use the longer version, and in some cases, one uses the correct and the other uses the long version??
Any ideas why this is happening?
I have already run a magento database repair, reindexed, and refreshes / flushed all caches.
Try $product->getUrlPath() instead of $product->getProductUrl()
UPDATE: As per below comment by #jordan314, Magento recommends to EE customers:
The url_path attribute is no longer used as of 1.13 but is still available for backward-compatibility, and Magento will not assign a value to it for new products, so it's not recommended to continue using it. Perhaps you could try using $product->getProductUrl() instead.
The incorrect url is generated because it can't find the rewritten url.
Maybe it is caused because incorrect store_id.
eg:
$id = 290;
Mage::app()->setCurrentStore('default');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
//change store id
Mage::app()->setCurrentStore('admin');
echo "store_id: ".Mage::app()->getStore()->getId()."<br>";
$url = Mage::helper('catalog/product')->getProductUrl($id);
echo $url."<br>";
result:
store_id: 1
http://local.com/surestep-pro-diabetic-test-strips-50-strips-professional-care.html
store_id: 0
https://local.com/index.php/catalog/product/view/id/290/s/surestep-pro-diabetic-test-strips-50-strips-professional-care/
The correct url rewrite can be found in table named core_url_rewrite (including the information about the store_id)
If it found match value in core_url_rewrite, it will generate 'the correct url' else it will concat the product_id + url key + category_id
$routePath = 'catalog/product/view';
$routeParams['id'] = $product->getId();
$routeParams['s'] = $product->getUrlKey();
if ($categoryId) {
$routeParams['category'] = $categoryId;
}
Try add this when you're getting your collection
$collection->addUrlRewrite();
It has helped me.
$id = 10;
Mage::app()->setCurrentStore('admin');
$url = Mage::helper('catalog/product')->getProductUrl($id);

Retrieve all product IDs associated with a Catalog Price Rule in Magento

I need a way to retrieve product IDs associated with a Catalog Price Rule promotion (ex. 50% the price of all items in the BICYCLES category). I'd like to do this without having to iterate through the entire product database.
I know there is a function called getRuleProductIds($ruleID), which should return an array of product IDs by rule ID, but I have no idea where to use it, which collection to associate it with, etc.
I have the following code in a products_in_promotion.phtml template:
<?php
$rules = Mage::getModel('catalogrule/rule');
$collection = $rules->getCollection();
$sale_items = $collection->getRuleProductIds(1); # ??????? this throws an error
?>
$collection properly stores an array of all the Catalog Price rules, but that's as close as I can get. No product list is in sight.
Any ideas what I'm doing wrong here?
You don't have to get it as a collection. Just load the rule that you want and use getMatchingProductIds as found in Mage_CatalogRule_Model_Rule (aka catalogrule/rule).
$catalog_rule = Mage::getModel('catalogrule/rule')->load(1); // Rule ID
$skus = $catalog_rule->getMatchingProductIds();
var_dump($skus);
hth
If you have many products in your store then this is the efficient way. You may add website_id, custom_group_id in where class to be more specific.
$resource = Mage::getSingleton('core/resource');
$connection = $resource->getConnection('core_read');
$tableName = $resource->getTableName('catalogrule_product');
$productIdList = $connection->fetchAll('SELECT product_id as product_id FROM '.$tableName.' WHERE rule_id = 1);
It worked for me after adding website filter to the code suggested by seanbreeden.
$catalog_rule = Mage::getModel('catalogrule/rule')->load(1); // Rule ID
$catalog_rule->addWebsiteFilter('1');
$skus = $catalog_rule->getMatchingProductIds();
var_dump($skus);

Resources