Delete test orders in magento 1.4.x

– Reset Magento TEST Data
SET FOREIGN_KEY_CHECKS=0;
– reset dashboard search queries
TRUNCATE `catalogsearch_query`;
ALTER TABLE `catalogsearch_query` AUTO_INCREMENT=1;
– reset sales order info
TRUNCATE `sales_flat_creditmemo`;
TRUNCATE `sales_flat_creditmemo_comment`;
TRUNCATE `sales_flat_creditmemo_grid`;
TRUNCATE `sales_flat_creditmemo_item`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_comment`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_quote_shipping_rate`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_comment`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_track`;
TRUNCATE `sales_invoiced_aggregated`;
TRUNCATE `sales_invoiced_aggregated_order`;
TRUNCATE `sales_order_aggregated_created`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
SET FOREIGN_KEY_CHECKS=1;

Delete test orders in magento 1.5.x

Before getting a magento 1.5.x site from development to live you should first remove the test orders. To do that, just run the following script in your mysql admin.

SET FOREIGN_KEY_CHECKS=0;
 
TRUNCATE `sales_flat_order`;
TRUNCATE `sales_flat_order_address`;
TRUNCATE `sales_flat_order_grid`;
TRUNCATE `sales_flat_order_item`;
TRUNCATE `sales_flat_order_status_history`;
TRUNCATE `sales_flat_quote`;
TRUNCATE `sales_flat_quote_address`;
TRUNCATE `sales_flat_quote_address_item`;
TRUNCATE `sales_flat_quote_item`;
TRUNCATE `sales_flat_quote_item_option`;
TRUNCATE `sales_flat_order_payment`;
TRUNCATE `sales_flat_quote_payment`;
TRUNCATE `sales_flat_shipment`;
TRUNCATE `sales_flat_shipment_item`;
TRUNCATE `sales_flat_shipment_grid`;
TRUNCATE `sales_flat_invoice`;
TRUNCATE `sales_flat_invoice_grid`;
TRUNCATE `sales_flat_invoice_item`;
TRUNCATE `sendfriend_log`;
TRUNCATE `tag`;
TRUNCATE `tag_relation`;
TRUNCATE `tag_summary`;
TRUNCATE `wishlist`;
TRUNCATE `log_quote`;
TRUNCATE `report_event`;
 
ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
ALTER TABLE `tag` AUTO_INCREMENT=1;
ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
ALTER TABLE `wishlist` AUTO_INCREMENT=1;
ALTER TABLE `log_quote` AUTO_INCREMENT=1;
ALTER TABLE `report_event` AUTO_INCREMENT=1;
 
-- lets reset customers
TRUNCATE `customer_address_entity`;
TRUNCATE `customer_address_entity_datetime`;
TRUNCATE `customer_address_entity_decimal`;
TRUNCATE `customer_address_entity_int`;
TRUNCATE `customer_address_entity_text`;
TRUNCATE `customer_address_entity_varchar`;
TRUNCATE `customer_entity`;
TRUNCATE `customer_entity_datetime`;
TRUNCATE `customer_entity_decimal`;
TRUNCATE `customer_entity_int`;
TRUNCATE `customer_entity_text`;
TRUNCATE `customer_entity_varchar`;
TRUNCATE `log_customer`;
TRUNCATE `log_visitor`;
TRUNCATE `log_visitor_info`;
 
ALTER TABLE `customer_address_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_address_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_datetime` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_decimal` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_int` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_text` AUTO_INCREMENT=1;
ALTER TABLE `customer_entity_varchar` AUTO_INCREMENT=1;
ALTER TABLE `log_customer` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor` AUTO_INCREMENT=1;
ALTER TABLE `log_visitor_info` AUTO_INCREMENT=1;
 
-- Now, lets Reset all ID counters
TRUNCATE `eav_entity_store`;
ALTER TABLE `eav_entity_store` AUTO_INCREMENT=1;
 
SET FOREIGN_KEY_CHECKS=1;

Zend_Auth and subdomains

I was working today on an user authentication system that redirects the users to their own sub-domain after a successful login. It should work like this: user comes to www.exmple.com, fills in his credentials and after successful login he/she should be redirected to username.example.com. All fine and dandy except the session is not persistent between sub-domains of example.com. After a bit of google and some help from eXcuvator on #zftalk, i found the solution:

Just put the following in your Bootstrap.php and everything should just work:

    protected function _initSession()
    {

        Zend_Session::setOptions( array('cookie_domain' => '.example.com', 'use_only_cookies' => true, 'name' => 'yourSessionName'));
    }

First day timestamp of week number in PHP

function mondayTimestamp($weekNumber, $year)
{
$time = strtotime($year . '0104 +' . ($weekNumber - 1) . ' weeks');
					
// Get the time of the first day of the week
$mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days', $time);

return $mondayTime;
}

Test it as follows:

$mondayTimestamp = mondayTimestamp(46, 2010);

echo date('j F Y', $modayTimestamp);

Fix Zend Framework 404 routes on HTTPS

I’ve been working on a web project that required some of the pages to be loaded via HTTPS.

I’ve got the certificate, installed accessed homepage via HTTPS and seems to work. That was not the case for the rest of the routes. After a lot of searching i was able to find a post which proposes a fix for this. Namely, you have to add what’s on your app’s main .htaccess into your SSL virtualhost.

Add the following just before tag closes and you should be fine:

        
                RewriteEngine on
                RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -s [OR]
                RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -l [OR]
                RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d

                RewriteRule ^.*$ - [NC,L]
                RewriteRule ^.*$ /index.php [NC,L]

        

Another one of my requirements was to redirect some pages to HTTPS (ie a login page). To do that add the following to your .htaccess:

RewriteCond %{HTTPS} !=on
RewriteRule ^login(.*) https://%{SERVER_NAME}/login$1 [R,L]

Wipe all categories from magento database

Below is a small sql snippet that will delete all the categories from a magento database:

 TRUNCATE TABLE `catalog_category_entity`;
 TRUNCATE TABLE `catalog_category_entity_datetime`;
 TRUNCATE TABLE `catalog_category_entity_decimal`;
 TRUNCATE TABLE `catalog_category_entity_int`;
 TRUNCATE TABLE `catalog_category_entity_text`;
 TRUNCATE TABLE `catalog_category_entity_varchar`;
 TRUNCATE TABLE `catalog_category_product`;
 TRUNCATE TABLE `catalog_category_product_index`;
 
 insert  into `catalog_category_entity`(`entity_id`,`entity_type_id`,`attribute_set_id`,`parent_id`,`created_at`,`updated_at`,`path`,`position`,`level`,`children_count`) values (1,3,0,0,'0000-00-00 00:00:00','2009-02-20 00:25:34','1',1,0,1),(2,3,3,0,'2009-02-20 00:25:34','2009-02-20 00:25:34','1/2',1,1,0);
 insert  into `catalog_category_entity_int`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,32,0,2,1),(2,3,32,1,2,1);
 insert  into `catalog_category_entity_varchar`(`value_id`,`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) values (1,3,31,0,1,'Root Catalog'),(2,3,33,0,1,'root-catalog'),(3,3,31,0,2,'Default Category'),(4,3,39,0,2,'PRODUCTS'),(5,3,33,0,2,'default-category');

Wipe all products from magento database

Here’s a sql snippet that deletes all the products from a mysql database:

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`;
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');

Fix php base64_decode error

If the base64_decode() returns weird results, you might try to decode a string which is not base64 valid. The most common problem seems to be + being replaced with a space (” “) when sending the encoded string via $_POST or $_GET.
To fix that simply do a str_replace on the encoded string, then try to decode it:

$encodedMessages = str_replace(' ', '+', $encodedMessages);
echo base64_decode($encodedMessages);

Hope this will help someone, it took me several hours to figure it out.

Magento create custom order statuses

This post is just a reminder for myself. It’s taken from http://www.magentocommerce.com/boards/viewthread/9976/P30/ . The code below was tested and works just fine on magento 1.4.0.1

So to recapitulate, I did the following (citing the other guys, thanks to atlasit, ajayksh and rune00):

1) write a list of the additional statuses that you want

2) download and open the file Magento\app\code\core\Mage\Sales\etc\config.xml
(do not upload it again as soon as you’re done, you need to place it into a different folder, see below)

3) find this code:

and after that add your own new custom status

4) Now find this code, a few lines below:


  
    
    
      
    
  

and under or above this block create a new block with your own custom status:



  
    
    
      
    
  

  
    
    
      
    
  

repeat this (step 3 and 4) for each new custom status

5) So that you can change your status from the admin interface back and forth
from any current status, you need to edit again each of the status code block
as follows.

change this


    
    
      
    
  

to this


    
    
      
      
      
      
      
      
      
    
    
  

you need to do this with all of the existing statuses and the new custom statuses that you’ve just created

so that these new statuses do not only become available and visible to the admin, but also to the customer
on his “my account” page (order history, current status), you need to make the new statues visible (thanks to rune00)
with the tag , as described above.

6) After you saved your changes, upload the new config.xml file to
/app/code/local/Mage/Sales/etc/ (you will need to create that folder structure)

7)Create a file named Mage_Sales.xml with the following content (thanks to atlasit)


    
        
            true
            local
        
    

Upload it to /app/etc/modules/

that’s pretty much it.

If you got it running successfully as well, let us know so that it will help others as well.

Setting up Xdebug with Zend Server on Linux

Since our team work on different operating system, we switched for custom apache/php/mysql installs to Zend Server. Before everyone was using something different (the guys on windows were using XAMP, WAMP, on ubuntu i was using the packages in the repo and on the server we’re using apache from cPanel) and applications were behaving a differently according to the default settings for all those platforms.

So we decided to install Zend Server CE as it has more or less the same settings over different platforms. I’ve had some bad experiences installing ZSCE on a system that already had apache installed via apt, but after a clear install of Karmic, ZF was running like a charm. The install process described in the online documentation works great.

After install is done, make sure to symlink php, pecl, pear and phpize so you can access them system wide:

sudo ln -s /usr/local/zend/bin/zendctl.sh /usr/sbin/zendctl
sudo ln -s /usr/local/zend/bin/pear /usr/sbin/pear
sudo ln -s /usr/local/zend/bin/pecl /usr/sbin/pecl
sudo ln -s /usr/local/zend/bin/php /usr/sbin/php
sudo ln -s /usr/local/zend/bin/phpize /usr/sbin/phpize

At this point you should be able to run php -i in terminal and the phpinfo will be displayed.

Next step is to install xdebug via pecl by running:

sudo pecl install xdebug

If all went well you should have the xdebug library located at /usr/local/zend/lib/php_extensions/xdebug.so . If you don’t have it there, then something went wrong and you should NOT continue reading. This issue must be sorted first.

Next you need to comment out the 1st line of /usr/local/zend/etc/conf.d/debugger.ini so it looks like this:

#open editor
gksu gedit /usr/local/zend/etc/conf.d/debugger.ini

# this is how the 1st two lines should look afterwards
; register the extension to be loaded by Zend Extension Manager
;zend_extension_manager.dir.debugger=/usr/local/zend/lib/debugger

Xdebug needs to be loaded before Zend Extension Manager, that’s why you need to add the following line just on top of the /usr/local/zend/etc/ext.d/extension_manager.ini

#open the editor
gksu gedit /usr/local/zend/etc/ext.d/extension_manager.ini

#add this line on 1st line: 
zend_extension=/usr/local/zend/lib/php_extensions/xdebug.so

#save the file

Restart zend server by running:

sudo /etc/init.d/zend-server restart

You can check if xdebug is working like so:

php -i |grep xdebug

The output should be something similar to this:

xdebug
xdebug support => enabled
xdebug.auto_trace => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.extended_info => On => On
xdebug.idekey => mimir => no value
xdebug.manual_url => http://www.php.net => http://www.php.net
xdebug.max_nesting_level => 100 => 100
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_autostart => Off => Off
xdebug.remote_enable => Off => Off
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

Note the xdebug support => enabled on line 2.

Hope that helps 🙂