Magento - get Customer Address id of order shipping address - magento

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

Related

Magento - How to get which store a country is allowed in

I would like to be able to input a country and have it return the store where that country is allowed.
Example: I'm using the the store code in a link to go to the right instance of my site (for example, frfr if I have a french address or wwuk if I have a american address)
Here is what I have so far:
$allowed = Mage::getStoreConfig('general/country/allow');
$countriesAllowed = explode(',', $allowed);
$allowedstoreCode = Mage::app()->getStore()->getCode($countriesAllowed);
It only returns the current store instead of the store where the country is allowed.

Wrong customer group id returned in subtotal.phtml?

I am using
$id = Mage::getSingleton('customer/session')->getCustomerGroupId();
in app\design\frontend\base\default\template\tax\checkout\subtotal.phtml to get the customers group id. It always returns 1, which is not correct. At other template files, it returns the correct number in the same session.
I am logged in as the correct customer, this cant be the problem.
What could be the problem?
Thanks!
Today i stumbled upon the same problem so i thought i'd share my findings on this one.
In our store the Enable Automatic Assignment to Customer Group (Config -> System -> Customer Configuration -> Create New Account Options) setting was enabled. Whenever a customer was logged in i got different results for the customer's group id depending on where the block requesting this information was placed in the layout. I then found out that it's the Minicart-Block messing up things.
If the automatic customer group assignment setting is enabled magento will execute an Observer from Mage_Sales: Mage_Sales_Model_Observer::changeQuoteCustomerGroupId. This is due to the minicart collecting totals on the Quote.
This observer takes the address from the quote and validates whether there is a VAT ID present for that address and if not whether the chosen country is part of the European Union.
If not, it will assign the default usergroup to the quote - but moreover it also will assign that usergroup to the user object, this is where things get messy:
if ((empty($customerVatNumber) || !Mage::helper('core')->isCountryInEU($customerCountryCode))
&& !$isDisableAutoGroupChange
) {
$groupId = ($customerInstance->getId()) ? $customerHelper->getDefaultCustomerGroupId($storeId)
: Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
$quoteAddress->setPrevQuoteCustomerGroupId($quoteInstance->getCustomerGroupId());
$customerInstance->setGroupId($groupId);
$quoteInstance->setCustomerGroupId($groupId);
return;
}
The problem is that there will never be a VAT ID or country set on the quote address when you add a product to an empty cart. These values will only be filled out during checkout process and can't be entered before. That means that for every customer that has a cart but hasn't been to checkout process yet Magento will assign the default user group to the user.
I ended up disabling the original observer and checking if there is any value for the country_id attribute set on the quote address.
If it's not set i get the vat_id and country_id values from the default addresses, if any, set them on the quote address and then defer to the original observer.
public function changeQuoteCustomerGroupId(Varien_Event_Observer $observer)
{
/** #var $quoteAddress Mage_Sales_Model_Quote_Address */
$quoteAddress = $observer->getQuoteAddress();
$quoteInstance = $quoteAddress->getQuote();
$customerInstance = $quoteInstance->getCustomer();
if (!$quoteAddress->getCountryId())
{
// no country chosen, yet. Copy default billing addresses value to quote address.
$primaryBillingAddress = $customerInstance->getPrimaryAddress('default_' . $quoteAddress->getAddressType());
if ($primaryBillingAddress->getId())
{
$quoteAddress->setVatId($primaryBillingAddress->getVatId());
$quoteAddress->setCountryId($primaryBillingAddress->getCountryId());
}
}
Mage::getSingleton('sales/observer')->changeQuoteCustomerGroupId($observer);
return $this;
}
config.xml
<events>
<sales_quote_address_collect_totals_before>
<observers>
<sales_customer_validate_vat_number>
<type>disabled</type>
</sales_customer_validate_vat_number>
<fallback_customer_validate_vat_number>
<class>vendor/sales_observer</class>
<method>changeQuoteCustomerGroupId</method>
</fallback_customer_validate_vat_number>
</observers>
</sales_quote_address_collect_totals_before>
<events>
Maybe this will be helpful for someone.

Magento. How to get address in quote

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

Get Magento option entity value based on the value's id

Here is the scenario specific to a base installation of magento.
I would like to be able to change some content on my category pages when a manufacturer filter has been chosen.
What I see is
http://www.myurl.com/path/to/cat.html?manufacturer=777
From this I can get the manufacturers id by doing something like:
$request->getParam('manufacturer');
What I would like to be able to do is jump into eav_attribute_option_value and get the manufacturers name based on the value_id (8404 in my case) and the manufacturer id to return the name of the manufacturer.
I am sure there are other use cases where this would become handy too.
For my case I will be prepending the brand to the category title tag based on some other logic. This will create new highly branded category pages that should rank well on google.
So your base category title tag might look like: 'Washing Machines'
And then the manufacturer brand filter page for Maytag would look like: 'Maytag Washing Machines'
I was able to find a dirty way to do this.
$query = "SELECT * FROM mg_eav_attribute_option_value WHERE option_id =" . $request_params['manufacturer'] . " LIMIT 0 , 30";
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$manufacturer = $read->fetchAll($query);

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