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

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.

Related

Get Eloquent table data from a nested loop

I am working on a student consultancy project. In the website header's country option, I am showing the value as, when the country is clicked, it will show the subcontinent and countries under the subcontinent. However, there are some popular countries that students are familiar with but unfamiliar with their subcontinent. So that's why those countries will be created under the subcontinent. So, for example, Australia is in Oceania, but Australia is in the subcontinent.
The subcontinent is in one-to-many relations with the country, and countries are in one-to-many relations with the university. So if I create Australia, I have to add something in countries to find the university under Australia. So I am adding one country Australia under subcontinent Australia. That way, I can access the university.
However, the main problem is I don't want to show that in the header's country drop-down menu. So I am trying to select it differently in the controller, which subcontinent doesn't have the same country name under it. So I will pass it with a different variable.
Controller
$dis = '';
$concatenated = collect();
foreach ($divisions as $key => $value) {
$dis = DB::table('subcontinents')
->where('subcontinent_name', '!=', $value->country_name)->get();
foreach ($concatenated as $k => $val) {
if ($val->subcontinent_name != $dis->subcontinent_name)
$concatenated = $concatenated->concat($dis);
}
}
dd($concatenated);
I created the country as a division. I am selecting subcontinents where the name is not the same. It gives output like this: [output][1]. So I tried to concat once if the name didn't exist previously. Now it's showing a null value. How can I solve this? Any kind of idea will also be helpful.

How to convert CodeIgniter urls into user friendly ones?

I've explored lot of questions and articles regarding this but I can't find how to get this done.
I'm doing a website which provides specifications of several products such as phones, tablets, tv etc. Here's what I've:
Controller - Specs (create and display specification of all products)
Method - Display (fetches detailed specs of selected model and shows)
Method - Index (lists names of all models stored in the table. this is where I build anchor links)
Display method takes three arguments (1, 2, 3).
1 - Type of product (Phones, Tablets, TV etc)
2 - Model Slug (iphone-6, galaxy-tab-s3, bravia-kdl-50w800d etc)
3 - Model ID (1, 4, 13 etc)
My URLs right now are like this:
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
What I want to achieve is URLs which are like this:
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
I don't mind restructuring my tables/controllers/methods or anything else if this can be achieved using whatever.
Thanks for reading.
Edit:
Route.php
$route['default_controller'] = 'Specs/index';
$route['404_override'] = 'Errors/show_404';
$route['translate_uri_dashes'] = FALSE;
This is how I'm building the anchor links (view_file->index.php, called from Index method):
<?php
foreach model(in the table)
echo anchor(specs_controller.display_function.product_type.model_slug.model_id, model_name);
end foreach
?>
I can get the desired URLs with following code in route.php. Only problem is I'm not able to make the 'urlController/urlMethod' return a value in the function which can be assigned to $result variable.
$route['(:any)'] = function ($1)
{
$result = 'urlController/urlMethod/'.$1;
return $result;
};
I'm not sure how to do this. Can someone suggest how I should call 'urlController/urlMethod'?
You could achieve it with CodeIgniter URI Routing. Considering
localhost/sitename/galaxy-tab-s3
maps to
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
And, model id i.e 4, in this case, is static with respect to galaxy tab s3, as you have not mentioned any such Id in the simplified URL.
My understanding is with every URL localhost/sitename/iphone-6, you need three details about the string 'iphone-6'. i.e. type of product, model-slug, model id. One way could be write something like
$route['sitename/(:any)'] = 'routingController/commonRoutingMethod/$1';
Here, create a new routingController and write some logic into commonRoutingMethod() method, which takes the string like iphone-6 and fetches its all three details i.e. product type, model id etc. And then redirects by building the exact URL using
header('Location: http://localhost/sitename/specs/display/$productType/$modelSlug/$modelId/');
NOTE : There could be more forward ways just using regex match in routes.php, given that you create diffrentiated structure of the string, based on product type and model id e.g p_iphone-6_1 or t_galaxy-tab-s3_4.
Please use below routing code, to achieve it.
localhost/sitename/specs/display/phones/iphone-6/1
localhost/sitename/specs/display/tablets/galaxy-tab-s3/4
localhost/sitename/specs/display/tv/bravia-kdl-50w800d/13
localhost/sitename/iphone-6
localhost/sitename/galaxy-tab-s3
localhost/sitename/bravia-kdl-50w800d
$route['(:any)'] = 'specs/display/$1/$1/$1';
Let me know if you need any help.

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

Mezzanine Forms Dropdown

I'm trying out Django/Mezzanine and if I have a custom user profile as such:
class UserProfile(models.Model):
user = models.OneToOneField("auth.User")
street_address1 = models.CharField(max_length=100)
street_address2 = models.CharField(max_length=100)
postalcode = models.CharField(max_length=10)
city = models.CharField(max_length=32)
country = models.CharField(max_length=2)
phone = models.CharField(max_length=15)
Mezzanine creates a sign up form at account/signup/ and I would like to modify the Country field to have a drop down list of countries from a table or xml file. The foreign key is a two character field.
How should go about doing this? Do I create a model form or try to extend the right template (tried looking at accounts\templates\account_form.html but don't think it is there?
I believe if you defined a "choices" arg for the field, it'll do just that:
https://docs.djangoproject.com/en/dev/ref/models/fields/#choices
A quick Google search will probably also reveal some pre-built packages for country lists.

getRate() and Magento tax percentage

I'm trying to get the tax rate (percentage, not currency) for a given postcode so I can display it in a third-party quote PDF printout (no relation to the "quote" Magento uses as the shopping cart pre-checkout). While I'm still relatively new to Magento it appears that getRateRequest() and getRate() are the two main functions which get the tax rate based on all the variables (product tax class, customer tax class, etc.).
Since this is for a third-party extension and all our products are taxable I figured I would just use getRate() with the correct Varien Object input and it would return the tax rate. After a week of trial and error I can't figure out why I'm always getting a rate of zero. I've confirmed I'm calling the getRate() function and that it's not returning zero from the first if() statement checking for Country and Customer/Product class ID. In addition I've confirmed all the variables are being passed on and accessible in the getRate() function itself.
I've created an object with the below input (based on the output of getRateRequest()) that I call with getRate() and am hoping someone can shed light on what is wrong with my data input or why the getRate() function is always returning a result of zero. (I'm actually setting with $variables below, they are just defined earlier up and one of my test case values are below)
// UPDATED CODE (variable values come from 3rd party quote extension)
$country = 'US'; // use short country code
$region = '12'; // must be numeric!
$postcode = '95050';
// our quote extension stores the customer id ('2') which we use to get the tax class
$customer = Mage::getModel('customer/customer')->load( '2' );
$custTax = $customer->getTaxClassId();
$TaxRequest = new Varien_Object();
$TaxRequest->setCountryId( $country );
$TaxRequest->setRegionId( $region );
$TaxRequest->setPostcode( $postcode );
$TaxRequest->setStore( Mage::app()->getStore() );
$TaxRequest->setCustomerClassId( $custTax );
$TaxRequest->setProductClassId(2); // 2=taxable id (all our products are taxable)
$taxCalculationModel = Mage::getSingleton('tax/calculation');
$rate = $taxCalculationModel->getRate($TaxRequest);
My backup plan is to just do a direct SQL lookup formula although that will probably get a bit messy. Since our web development team didn't exactly follow good coding standards an eventual site re-write is in my future anyway once the initial launch fixes are in (all 4 pages of them).
Thanks for any help and taking the time to read this.
EDIT - Stack Overflow is awesome :)
You can also try this
$store = Mage::app()->getStore('default');
$request = Mage::getSingleton('tax/calculation')->getRateRequest(null, null, null, $store);
$taxclassid = $product->getData('tax_class_id');
$percent = Mage::getSingleton('tax/calculation')->getRate($request->setProductClassId($taxclassid));
If you change:
$TaxRequest->setRegionId(California);
to
$TaxRequest->setRegionId($stateId);
where $stateId is numeric region id. Your code should work then.

Resources