Magento: How to delete/remove all products from all categories
When you are using some script to automatically add products to categories from XLS file to your Magento system, you may need to delete all products frequently for testing purpose. It’s not as easy to mark all products and press delete button in admin. No, it’s not like that, you need to delete all products, remove them from their linked categories, reset the inventory, etc. so many things to do.
Below is the MySQL queries that will make your task easier. By running all the queries, it will delete all the products in your Magento system. Use it carefully, it’s going to delete everything without an option to rollback.
SET FOREIGN_KEY_CHECKS = 0; | |
TRUNCATE TABLE `catalog_product_bundle_option`; | |
TRUNCATE TABLE `catalog_product_bundle_option_value`; | |
TRUNCATE TABLE `catalog_product_bundle_selection`; | |
TRUNCATE TABLE `catalog_product_entity_datetime`; | |
TRUNCATE TABLE `catalog_product_entity_decimal`; | |
TRUNCATE TABLE `catalog_product_entity_gallery`; | |
TRUNCATE TABLE `catalog_product_entity_int`; | |
TRUNCATE TABLE `catalog_product_entity_media_gallery`; | |
TRUNCATE TABLE `catalog_product_entity_media_gallery_value`; | |
TRUNCATE TABLE `catalog_product_entity_text`; | |
TRUNCATE TABLE `catalog_product_entity_tier_price`; | |
TRUNCATE TABLE `catalog_product_entity_varchar`; | |
TRUNCATE TABLE `catalog_product_link`; | |
TRUNCATE TABLE `catalog_product_link_attribute`; | |
TRUNCATE TABLE `catalog_product_link_attribute_decimal`; | |
TRUNCATE TABLE `catalog_product_link_attribute_int`; | |
TRUNCATE TABLE `catalog_product_link_attribute_varchar`; | |
TRUNCATE TABLE `catalog_product_link_type`; | |
TRUNCATE TABLE `catalog_product_option`; | |
TRUNCATE TABLE `catalog_product_option_price`; | |
TRUNCATE TABLE `catalog_product_option_title`; | |
TRUNCATE TABLE `catalog_product_option_type_price`; | |
TRUNCATE TABLE `catalog_product_option_type_title`; | |
TRUNCATE TABLE `catalog_product_option_type_value`; | |
TRUNCATE TABLE `catalog_product_super_attribute`; | |
TRUNCATE TABLE `catalog_product_super_attribute_label`; | |
TRUNCATE TABLE `catalog_product_super_attribute_pricing`; | |
TRUNCATE TABLE `catalog_product_super_link`; | |
TRUNCATE TABLE `catalog_product_enabled_index`; | |
TRUNCATE TABLE `catalog_product_website`; | |
TRUNCATE TABLE `catalog_product_entity`; | |
TRUNCATE TABLE `cataloginventory_stock`; | |
TRUNCATE TABLE `cataloginventory_stock_item`; | |
TRUNCATE TABLE `cataloginventory_stock_status`; | |
TRUNCATE TABLE `catalog_product_link`; | |
TRUNCATE TABLE `catalog_product_link_type`; | |
TRUNCATE TABLE `catalog_product_option`; | |
TRUNCATE TABLE `catalog_product_option_type_value`; | |
TRUNCATE TABLE `catalog_product_super_attribute`; | |
TRUNCATE TABLE `catalog_product_entity`; | |
TRUNCATE TABLE `cataloginventory_stock`; | |
DELETE FROM catalog_product_flat_1; | |
DELETE FROM catalog_product_flat_10; | |
DELETE FROM catalog_product_flat_11; | |
DELETE FROM catalog_product_flat_12; | |
DELETE FROM catalog_product_flat_13; | |
DELETE FROM catalog_product_flat_14; | |
DELETE FROM catalog_product_flat_15; | |
DELETE FROM catalog_product_flat_16; | |
DELETE FROM catalog_product_flat_17; | |
DELETE FROM catalog_product_flat_18; | |
DELETE FROM catalog_product_flat_19; | |
DELETE FROM catalog_product_flat_2; | |
DELETE FROM catalog_product_flat_20; | |
DELETE FROM catalog_product_flat_21; | |
DELETE FROM catalog_product_flat_22; | |
DELETE FROM catalog_product_flat_23; | |
DELETE FROM catalog_product_flat_24; | |
DELETE FROM catalog_product_flat_25; | |
DELETE FROM catalog_product_flat_26; | |
DELETE FROM catalog_product_flat_27; | |
DELETE FROM catalog_product_flat_28; | |
DELETE FROM catalog_product_flat_29; | |
DELETE FROM catalog_product_flat_3; | |
DELETE FROM catalog_product_flat_30; | |
DELETE FROM catalog_product_flat_31; | |
DELETE FROM catalog_product_flat_32; | |
DELETE FROM catalog_product_flat_33; | |
DELETE FROM catalog_product_flat_34; | |
DELETE FROM catalog_product_flat_35; | |
DELETE FROM catalog_product_flat_36; | |
DELETE FROM catalog_product_flat_37; | |
DELETE FROM catalog_product_flat_4; | |
DELETE FROM catalog_product_flat_5; | |
DELETE FROM catalog_product_flat_6; | |
DELETE FROM catalog_product_flat_7; | |
DELETE FROM catalog_product_flat_8; | |
DELETE FROM catalog_product_flat_9; | |
SET FOREIGN_KEY_CHECKS = 1; | |
insert into `catalog_product_link_type`(`link_type_id`,`code`) values (1,'relation'),(2,'bundle'),(3,'super'),(4,'up_sell'),(5,'cross_sell'); | |
insert into `catalog_product_link_attribute`(`product_link_attribute_id`,`link_type_id`,`product_link_attribute_code`,`data_type`) values (1,2,'qty','decimal'),(2,1,'position','int'),(3,4,'position','int'),(4,5,'position','int'),(6,1,'qty','decimal'),(7,3,'position','int'),(8,3,'qty','decimal'); | |
insert into `cataloginventory_stock`(`stock_id`,`stock_name`) values (1,'Default'); |
Now go to System > Index Management, and re-index all indexes.
Hope this helps!
Magento, PHP: Merging/Joining two objects collections
It’s easy to merge two arrays with array_merge, but have you came across to merge two objects? It’s not that easy in Magento. You need to convert it to array first, merge them, and convert it to the object again to make it work. If you have two objects of different class, then it’s really difficult job to merge as both will not be compatible. If they are from similar classes or share same elements inside, then it’s not that tricky.
If you want to add collection2 in collection1 (in Magento), where collection1 will be a merged form of both, you can do so by:
foreach($collection2 as $coll) { | |
$collection1->addItem($coll); | |
} |
Magento: Check if customer already exist or not
When trying to add new user programatically, you will need to first check whether the customer is already registered or not in the system. For that, email address is required, as Magento uses email address for login purposes.
Below is the code which checks if customer is already there in the database, with some particular website id if any.
protected function _customerExists($email, $websiteId = null) | |
{ | |
$customer = Mage::getModel('customer/customer'); | |
if ($websiteId) { | |
$customer->setWebsiteId($websiteId); | |
} | |
$customer->loadByEmail($email); | |
if ($customer->getId()) { | |
return $customer; | |
} | |
return false; | |
} |
Magento: Add customer Facebook, Twitter, Google+, Pinterest handles
It’s always a good idea to get as much information as you get from customer. Adding customer’s social networking usernames are common in most of the sites, which can allow you to post content to their wall regarding any purchase customer made. It helps you to get more visibility of your store in front of customer’s friends and whoever visits their page.
So, you will need to add these in database, so that it can reflect to the customer’s account:
$installer = $this; | |
$installer->startSetup(); | |
$setup = new Mage_Eav_Model_Entity_Setup('core_setup'); | |
$setup->addAttribute('customer', 'facebook', array( | |
'label' => 'Facebook', | |
'type' => 'varchar', | |
'input' => 'text', | |
'default' => '', | |
'visible' => true, | |
'required' => false, | |
'unique' => true, | |
'user_defined' => true, | |
)); |
Magento: How to run/set cron in Magento
Ever came with the requirement where you need to set cron for periodically running certain jobs automatically? If you are new to cron in Magento, this blog post is going to help you out. Cron is a time-based job scheduler in Unix-like computer operating systems.
There are basically two crons: one is system’s cron (Unix, where it will trigger the specified file, for each specified time (in terms of minute, hour, day, week, year) AND another is Magento’s internal cron where it will decide which file and method to call to complete the operation.
Continue reading »
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)