Tag Archives: Magento tutorials online
Magento get last insert id
Magento get last insert id : We often need to get the last inserted id in table. If you are using magento model to save data you can get last inserted id simply using $model->getId(); . Here in this tutorial we are going to explain how you can get the last inserted in table using the magento model.
Magento get last insert id
If you are using magento model you can get last inserted id simply as below. Suppose we are adding data using the model Mage::getModel(‘user/profile’). For this model here is an example –
Magento get last insert id:
$model = Mage::getModel('user/profile'); $model->setFirstname($firstName); $model->setLastname ($lastName); $model->save(); $lastInsrtedId = $model->getId(); |
The above example will give you the last inserted id in the table. $model->getId() gives you last inserted id.
Magento clear cart session
Magento clear cart session : Sometimes we need to clear cart’s session data programmatically. In this tutorial we are going to explain how to clear cart session programmatically.
Magento clear cart session
If you want to clear the entire cart session, shopping cart, shipping information, billing information and payment methods use the below method-
Clear Shopping Cart
Magento clear cart session:
$collection = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection(); foreach( $collection as $item ){ Mage::getSingleton('checkout/cart')->removeItem( $item->getId() )->save(); } |
Clear Entire Checkout Session
Clear Entire Checkout Session:
Mage::getSingleton('checkout/session')->clear(); |
The above code will remove the entire session.
Magento update customer programmatically
Magento update customer programmatically : Sometimes you need to update the customer details programmatically instead of updating manually. Mage::getModel(‘customer/customer’) is used to update customer details in magento. Here in this tutorial we are going to explain how to update customer details programmatically with example.
For Magento 2.x Refer- Magento 2.x Update Customer Data
Magento update customer address programmatically
You can update the customer details using the model Mage::getModel(‘customer/customer’) as below –
Magento update customer details programmatically:
$email = "abc@example.com"; $customer = Mage::getModel('customer/customer'); $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); $customer->loadByEmail($email); if($customer->getId()){ $customer->setFirstname($firstName); $customer->setLastname ($lastName); $customer->save(); } |
The above example will update the customer details such as first name, last name etc.
Update Customer Address
Let us have one more example in which we will update the customer address programmatically.
Magento Add / Update customer address programmatically
Mage::getModel(‘customer/address’) is used to update the customer address. Here is an example in which we are going to update the customer address.
Magento Add / Update customer address programmatically:
$customerId = 101; $shippingData = array( 'firstname' => $firstName, 'middlename' => $middleName, 'lastname' => $lastName, 'prefix' => $prefix, 'suffix' => $suffix, 'company' => $company, 'street' => $street, 'country_id' => $country, 'city' => $city, 'region_id' => '', 'region' => $region, 'postcode' => $postal, 'country_id' => $country, 'telephone' => $telephone, 'fax' => $fax, ); $customerAddress = Mage::getModel('customer/address'); if ($customer->getDefaultShipping()){ $defaultShippingId = $customer->getDefaultShipping() $customerAddress->load($defaultShippingId); } else { $customerAddress ->setCustomerId($customerId) ->setIsDefaultBilling('0') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); } try { $customerAddress->setData($shippingData); $customerAddress->save(); } catch (Exception $e) { Mage::log('Error .' . $e->getMessage()); } |
The above example will update the customer address. ->setIsDefaultBilling() is used to save the default billing address. If you set ->setIsDefaultBilling(1) it will save customer address as default billing address. ->setIsDefaultShipping() is used to set the default shipping address if you set ->setIsDefaultBilling(1) it will save the default shipping address as customer address. If you want to save this address in address book then set ->setSaveInAddressBook(‘1’) else set ->setSaveInAddressBook(‘0’).
Magento set product status programmatically
Magento set product status programmatically : Sometimes we need to update product status programmatically in magento. You can set product status enabled, disabled or other status. In this tutorial we are going to explain how to set product status programmatically in magento.
Magento set product status programmatically
You can update status as below
STATUS_ENABLED
You can set product status enabled as below –
Magento set product status programmatically: Enabled
$productId = 110; $storeId = 1; //store id Mage::getModel('catalog/product_status')->updateProductStatus($productId, $storeId, Mage_Catalog_Model_Product_Status::STATUS_ENABLED); |
The above example will set the product status as enabled.
STATUS_DISABLED
You can set product status disabled as below –
Magento set product status programmatically: Disabled
$productId = 110; $storeId = 1; //store id Mage::getModel('catalog/product_status')->updateProductStatus($productId, $storeId, Mage_Catalog_Model_Product_Status::STATUS_DISABLED); |
The above example will set the product status as disabled.
Magento add custom error messages
Magento add custom error messages : Displaying messages in proper format is very important in any application. Magento provides you the method to add the custom error messages in session which will be displayed on the page. Here we are going to explain the method to add the error messages in the session.
Magento add custom error messages
Here are the following inbuilt functions to add & display messages.
- Success
- Error
- Warning
- Notice
Let us go one by one –
Magento add success message
You can add the success message as below –
Magento add success message :
Mage::getSingleton('core/session')->addSuccess('Add success message.'); |
Add you success message in the session as above and this message will be automatically displayed and flushed.
Magento add error message
You can add the error message as below –
Magento add error message :
Mage::getSingleton('core/session')->addError('Add error message.'); |
Add you error message in the session as above and this message will be automatically displayed and flushed.
Magento add warning message
You can add and display the warning message as below –
Magento add error message :
Mage::getSingleton('core/session')->addWarning('Add warning message.'); |
Add you warning message in the session as above and this message will be automatically displayed and flushed.
Magento add notice message
You can add and display the notice message as below –
Magento add notice message :
Mage::getSingleton('core/session')->addNotice('Add notice message.'); |
Add you notice message in the session as above and this message will be automatically displayed and flushed.
Magento load shipment by shipment id
Magento load shipment by shipment id : Sometimes you need to load shipment using the shipment id. You can load shipment Mage::getModel(‘sales/order_shipment’); model and shipment id. After loading shipment object you can use it to update shipment or get the information of shipment.
Magento load shipment by shipment id
Load Shipment By Id
Here is an example to load shipment by shipment id –
Magento load shipment by shipment id:
$shipmentId = 198; $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId); |
Which will give you the shipment object.
Load Shipment By Increment Id
Here is an example to load shipment by shipment increment id –
Magento load shipment by shipment increment id:
$shipmentIncId = 198; $shipment = Mage::getModel('sales/order_shipment')->loadByIncrementId($shipmentIncId); |
Which will also give you the shipment object as above.
Magento update product programmatically
Magento update product programmatically :Working with magento we sometimes need to update the products programmatically instead of updating the product from magento admin. Mage::getModel(‘catalog/product’) is used to load the product object and then set the new value which you want to update. In this tutorial we are going to cover various scenarios with example.
Magento update product programmatically
Suppose we have product id and Let us update product name, description & sku as below-
Magento update product programmatically:
public function updateProduct(){ try{ $productId = 100; $product = Mage::getModel("catalog/product")->load($productId); $product->setStatus(1); $product->setName('Test Product'); $product->setSku('test-sku'); $product->setDescription("This is test product description."); $product->setShortDescription("This is short description of product."); $product->save(); }catch (Exception $e) { Mage::logException($e->getMessage()); } } |
The above example loads the product id with 100 and updates the product name, sku, description and short description.
More Examples
Let us have more examples related to updating the products.
magento update product attribute programmatically
Here is another example to update the product’s attribute.
magento update product attribute value programmatically:
public function updateProduct(){ try{ $productId = 100; $product = Mage::getModel("catalog/product")->load($productId); $attrCode = 'my_attr'; $attrVal = "someVal"; $product->setData($attrCode, $attrVal) ->getResource() ->saveAttribute($product, $attributeCode); }catch (Exception $e) { Mage::logException($e->getMessage()); } } |
The above example will update the attribute my_attr with new value $attrVal. Thus you can update any attribute of product.
Magento update product Quantity programmatically
Magento update product Quantity programmatically:
public function updateProduct(){ try{ $productId = 100; $product = Mage::getModel("catalog/product")->load($productId); $product->setQty(99); $product->save(); }catch (Exception $e) { Mage::logException($e->getMessage()); } } |
The above example loads the product and updates the quantity.
Magento Delete Products Programmatically
Magento Delete Products Programmatically : Some times we need to delete the products in magento. You can delete products programmatically from magento catalog. Before deleting products make sure you have set the store id because you can have multiple stores. In this example we are going to cover how to delete products programmatically in magento.
Magento Delete Products Programmatically
Here are the methods to delete products in magento –
Delete Single Product –
Magento Delete Products Programmatically:
load( $productId )->delete(); }catch(Exception $e){ echo $e->getMessage(); } ?> |
The above method loads products & Deletes the product.
Magento Delete all Products Programmatically –
If you want to delete all the products. Create a new file at root and add the below lines of code which will delete all products one by one from magento.
1. Create a file at root – deleteProducts.php
2. Add the below code in this file.
3. Run this file as : www.yourdomain.com/deleteProducts.php
Magento Delete Products Programmatically:
setCurrentStore( Mage_Core_Model_App :: ADMIN_STORE_ID ); $storeId = 1; $products = Mage :: getResourceModel('catalog/product_collection')->setStoreId($storeId)->getAllIds(); if(is_array($products)) { foreach ($products as $key => $productId) { try { $product = Mage::getModel('catalog/product')->load($productId)->delete(); } catch (Exception $e) { Mage::log("Unable to delete product with ID: ". $productId ."); } } } ?> |
set_time_limit(0); & ini_set(‘memory_limit’,’512M’); is set to fix timeout and memory limit problems. The above example will delete all products from magento so be sure before running the above script.
Magento send transactional email programmatically
Magento send transactional email programmatically: In Magento, we know emails are automatically sent on certain actions such as order creation, invoice generation etc. Each email has a template which is used in that email. You can find the template in admin under the tag – System->Transaction Email where you can manage them. We sometimes need to send transactional email in another module programmatically. Here in this tutorial, we are going to explain how you can send transactional emails.
For Magento 2.x Email Sending Click Here
Magento send transactional email programmatically
Here in this example we assume that you have created an email template. So in this example we will use template id ie. 10 . Here is an example –
Magento send transactional email programmatically:
$senderName,'email' => $senderEmail); //recepient $email = "customer@example.com"; $name = "Kelly"; // Set variables that can be used in email template $variables = array('custom_var' => $yourCustomVariable); $translate = Mage::getSingleton('core/translate'); $storeId = Mage::app()->getStore()->getId(); // Now Send Transactional Email try{ Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $email, $name, $variables , $storeId); } catch(Exception $e){ echo $e->getMessage(); } ?> |
$variables = array(‘custom_var’ => $yourCustomVariable); You use this to pass the custom variables to your email template if you want in template. Using the above example you can send the transactional email easily.