I need to delete customer addresses programmatically, but I didn't find a function to do that.
$recordedAddresses = array();
foreach ($customer->getAddresses() as $address)
{
$recordedAddresses = $address->toArray();
}
I already took the addresses' collection as showed above, I just wanna delete them by id.
Curiously I didn't find examples but using API.
Could someone gimme a hand with that?
Somehow Magento keeps empty entities after using $address->delete() in my case. There were empty addresses on account preventing admin from saving the customer form when using this method.
Only way I've found to actually remove the address from user account is by changing the protected $_isDeleted flag to true:
$address = Mage::getModel('customer/address')->load($addressId);
$address->isDeleted(true);
Hope it saves some time for anyone who will stumble uppon same Magento behaviour.
Have a look at the Mage_Customer_AddressController controller class and deleteAction() method. Essentially all you need to is load the address by it's id:
$address = Mage::getModel('customer/address')->load($addressId);
and then delete it:
$address->delete();
delete() is a standard method you can run against all models (see Mage_Core_Model_Abstract), you can also set the _isDeleted flag and call save() which will have the same result.
Related
I'm working on getting joomla registration form values that user enters
at the time of registration. After two days of searching I reached to the
file Joomla2.5.7\components\com_users\controllers\registration.php. In the register() method I
tried to echo $data and $requestData variables but didn't see any output, on registering a new entry. I also tried to echo javascript but was unsuccessful. I'm trying to connect joomla database with my own database, so that whenever new user registers , he is also registered to my website. How can I get the registration form variables, any kind of help is really appreciated.
Ty this
On registration controller you can find a function call to model like this
$return = $model->register($data);
after that just
echo "<pre/>";
print_r($data);
Also in the registration model
components\com_users\model\registration.php
register() method is defined you can check that for more info.
In addition if you want to add the users info to the other DB like your website DB.
The best place to write mysql query is :
// Store the data.
if (!$user->save()) {
$this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
return false;
}else{
//Your custom mysql query to other DB or tables
}
find the above section in the registration model inside register()method.
Hope this may solve your problem..
I hope this tutorial helps you. You will find it explains the process of joomla registration form very well.
http://youtu.be/2AyCzb2vTaU
I want to get the name of the shipping method the user has chosen during checkout. Does anyone know how to retrieve that info?
This will get it to some extent but it is cached:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingDescription();
When I am on the onestep checkout and I go back to the shipping tab and change the shipping, it is still holding the old shipping method. I need to figure out how to get the current one.
Foreword
Constructed from Magento app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php and others:
app/design/frontend/base/default/template/checkout/onepage/shipping_method/available.phtml uses this code to determine which shipping method was selected:
$this->getAddressShippingMethod()
app/code/core/Mage/Checkout/Block/Onepage/Shipping/Method/Available.php expands that code to this:
return $this->getAddress()->getShippingMethod();
Let's research a bit and expand it even deeper:
$this->getQuote()->getShippingAddress()->getShippingMethod();
Parent block expands method getQuote():
return $this->getCheckout()->getQuote();
And deeper:
public function getChechout() {
return Mage::getSingleton('checkout/session');
}
Merging all that code gives us this:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingMethod()
That gives you the shipping method code. Giving that, you could manipulate it just as you wish. This data is stored within the database, so when you change shipping method, the code changes too.
Getting deeper and deeper!
If you've ever created your own shipping method, you'd know, that it has the method called collectRates().
It fills a set of shipping/rate_result_method models, stores it within the instance of shipping/rate_result model and returns it (you can get each model' instance using Mage::getModel(<model i've named>); ).
Yet, note: one could contain multiple rate_result_method instances, while the shipping method code is the same for all those instances!
Thus, in order to get the description, you need to get one of the rate_result_method instances and retrieve its methodTitle or carrierTitle.
After a small researching i've found how to retrieve all these rates:
Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getShippingRatesCollection()
This will provide you with a collection of all rates for the selected shipping method. You can operate it with getItems() and get a hash. Or you could use getFirstItem() and use it as the template.
Anyway, let's assume u've retrieved some item of that collection and stored it within the $rate variable:
$rate->getCarrier(); // This will provide you with the carrier code
$rate->getCarrierTitle(); // This will give you the carrier title
$rate->getCode(); // This will give you **current shipping method** code
$rate->getMethod(); // This will provide you with the **shipping method** code
$rate->getMethodTitle(); // This will tell you current shipping method title
$rate->getMethodDescription(); // And this is the description of the current shipping method and **it could be NULL**
That's all, folks!
I am really sorry for my poor English and for my strange mind flow. Hope this will help you or someone else. Thanks!
Just in case you need it still. You can get shipping method from order by:
$order->getShippingMethod();
Of course how you get your $order depends on context.
Also you can get description by:
$order->getShippingDescription();
shipping method in magento
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
$options = array();
foreach($methods as $_code => $_method)
{
if(!$_title = Mage::getStoreConfig("carriers/$_code/title"))
$_title = $_code;
$options[] = array('value' => $_code, 'label' => $_title . " ($_code)");
}
echo "<xmp>";
print_r($options);
echo "</xmp>";
In your checkout controller you need to add extra steps to save your quote if you want this information to be accessible to you.
I added a few '$quote->save();' entries to get this to work, however, I cannot definitively say which entry is the one that did the fix. I also cannot find the link on Magento forums, however, I hope I have given you a head start on what is going on.
You could override the saveShippingMethodAction() function in the Mage_Checkout_OnepageController, or extend upon it, and save the method into the registry by inserting:
Mage::register('blahShippingMethod', $this->getRequest()->getPost('shipping_method'));
and call upon it as you need it: Mage::registry('blahShippingMethod');
Don't forget to unset it when you no longer need it as you will run into an error if you try to reset when it's already been set.
Mage::unregister('blahShippingMethod');
I am using the SoftDeletableBehavior for my addresses model.
This sets it so when you delete an address it doesnt really delete it, instead sets a deleted column in the database to 1.
This also changes your query in the beforeFind function of the Address model to only pull entries where deleted != 1
This works when I am viewing addresses via the addresses controller like /addresses/show/43.
However my Users controller hasMany addresses. So when I am in the users controller and I call $this->find('first'); To get the user, I also get the associated addresses. I am expecting this to NOT give me the (softly) deleted addresses but it DOES. The beforeFilter on the Address Model is never called either.
What is the right way to handle this?
Update.
Apparently when I do this:
$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
I have a $data['User] array and a $data['Address] array (along with others).
But this does not use the beforeFind filter in the Address Model. So I get the wrong data.
But if I do this:
$data = $this->User->find('first',array('conditions' => array('User.id' => $id),'recursive' => 2));
$data['Address'] = $this->User->Address->find('all',array('conditions'=>array('user_id'=>$id)));
Then it does use the beforeFind filter on the Address model and returns the right data.
(Of course in a different format [Address][0][id] vs [Address][0][Address][id])
What I do not understand is if the associated data thats pulled does not use its own model to find its data, then whats the point? Isn't this one of the main purposes of MVC?
Maybe I just don't understand? Or I am missing something?
Can someone please enlighten me?
Can't you put this condition in your association?
<?php
class User extends AppModel {
var $hasMany = array(
'Address' => array(
'conditions' => array('NOT' => array('Address.deleted' => '1')),
)
);
}
?>
Personally, I would add a condition to the find query for users
$this->Users->find->('all',array('conditions'=>array('Address.deleted !='=>'1')));
(haven't tested the code for exact syntax, but this is the general idea)
When you use $this->User->find(...) and include Addresses, you will only see the User model's callbacks fire, because that is the model you are using to drive the find.
This is why you have to use extra conditions in your User model's associations to filter out the soft-deleted Addresses.
If the Address model's callbacks were fired during every find on the User model when recursion scope included Addresses, you'd have no control as to when to include the soft-deleted Addresses and would risk clashes in functionality and param manipulation by those callbacks. Not to mention the serious performance impact this could have.
I m trying to learn coding a bit through Magento, and I have to admit that I'm a bit confused about this notion of object chaining in it.
In fact I don't understand when to do a load and when I can avoid it. For exemple:
$product = Mage::getModel('catalog/product')->load($item->getProductId());
I would like to get the info of product from a product ID in this case; why do I need to load it? ($item is the loop of all the products of an order)
And here I don't need to do any load:
$customer = $payment->getOrder()->getCustomer();
I'm sorry in advance for my stupid question: What does load do comparing to my second example? Thanks a lot and have a nice day,
Anselme
Behind the scenes a method like $payment->getOrder() is effectively (after checking to see if it's already loaded) doing this:
return Mage::getModel('sales/order')->load($this->getOrderId());
// $this in this context is $payment
So a load is still needed to retrieve the relevant data from the database, the getOrder() method is just a convenience. The load() method itself returns it's class instance, which is why you can assign it to $product in your first example. The getOrder() and getCustomer() methods don't return themselves, they return a different object, which is why $payment is not assigned to $customer in your second example.
The Mage::getModel() method is only responsible for determining the correct class and creating a blank instance of it. Instead of a load you could instead set it's data with a setData() call, passing a keyed array of values. All of the setters return their object, just like load() does.
$customer = $payment->getOrder()->getCustomer();
It means the id of the customer is already present in the session, so you don't need to explicitly tell magento to load the customer.
In the products case, you have to tell magento the product id of the product you want to get details of.
I am trying to populate address fields during registration, using data from another system. In my observer, I am able to use
$customer = $observer->getCustomer();
$customer->setFirstname($value);
$customer->setLastname($value);
and the information is saved in the database, but
$customer->setStreet($value);
$customer->setPostcode($value);
$customer->setTelephone($value);
do not. How would I set address fields?
Thanks!
Addresses are not stored in the Mage_Customer_Model_Customer object. You should instead do something like:
$address = Mage::getModel('customer/address');
$address->setStreet(...);
...
$customer->addAddress($address);
I found some good posts:
this was easier for me:-)
http://www.pauldonnellydesigns.com/blog/magento-display-address-fields-in-create-an-account/
and longer post:
http://www.magentocommerce.com/boards/viewthread/11110/
It's working, I checked.
I need show fields form address in orders, controller: /checkout/onepage on the page with form login and register (I want add fields personal and address in step register)
Do someone saw code to create for this functionality?