Woocommerce hook custom button on admin order page

Another Woocommerce hook that i search high and low for it but in the end dig it out from the source code itself. What happen if you would like to create a custom button on Woocommerce admin order page? Something like the image below?

Screen Shot 2015-09-16 at 7.42.19 PM

Looks like something you need? It's pretty easy by using a hook call 'woocommerce_order_item_add_action_buttons'.

woocommerce_order_item_add_action_buttons woocommerce button hook

Basically this hook allows you to add custom button similar to the one woocommerce has and ensuring that you don't hack or edit the core code to get what you want. Here is a snippet of what i did.

// add new button for woocommerce
add_action( 'woocommerce_order_item_add_action_buttons', 'action_woocommerce_order_item_add_action_buttons', 10, 1);
// define the woocommerce_order_item_add_action_buttons callback
function action_woocommerce_order_item_add_action_buttons( $order )
{
    echo '<button type="button" onclick="document.post.submit();" class="button generate-items">' . __( 'Magic Button Appear!', 'hungred' ) . '</button>';
    // indicate its taopix order generator button
    echo '<input type="hidden" value="1" name="renew_order" />';
};
// resubmit renew order handler
add_action('save_post', 'renew_save_again', 10, 3);
function renew_save_again($post_id, $post, $update){
    $slug = 'shop_order';
    if(is_admin()){
            // If this isn't a 'woocommercer order' post, don't update it.
            if ( $slug != $post->post_type ) {
                    return;
            }
            if(isset($_POST['renew_order']) && $_POST['renew_order']){
                    // do your stuff here after you hit submit
                }
    }
}

dump the above snippet to your function.php file and you should see the "magic button appear!" button on your woocommerce admin order page.

A little bit of explanation here, what the above code does it create a button which the woocommerce_order_item_add_action_buttons hook does the trick

// add new button for woocommerce
add_action( 'woocommerce_order_item_add_action_buttons', 'action_woocommerce_order_item_add_action_buttons', 10, 1);
// define the woocommerce_order_item_add_action_buttons callback
function action_woocommerce_order_item_add_action_buttons( $order )
{
    echo '<button type="button" onclick="document.post.submit();" class="button generate-items">' . __( 'Magic Button Appear!', 'hungred' ) . '</button>';
    // indicate its taopix order generator button
    echo '<input type="hidden" value="1" name="renew_order" />';
};

I've place a hidden button to identify the submit is hit from this button. Next, since woocommerce admin order page is also a post page, all i have to do is to attached it with a post handler hook.

// resubmit renew order handler
add_action('save_post', 'renew_save_again', 10, 3);
function renew_save_again($post_id, $post, $update){
    $slug = 'shop_order';
    if(is_admin()){
            // If this isn't a 'woocommercer order' post, don't update it.
            if ( $slug != $post->post_type ) {
                    return;
            }
            if(isset($_POST['renew_order']) && $_POST['renew_order']){
                    // do your stuff here after you hit submit
                }
    }
}

and make sure that this handle only runs if its a shop_order type and we are good to go! That's all! Enjoy!

Get WordPress Custom Post Taxonomy Categories and Tags

Here's another problem with WordPress post where you want to know whether a particular custom post has a particular taxonomy categories or tags. In a normal post cast, you will highly likely used either get_categories or get_terms but for custom post type you might need to use another method which is what i am using currently as well. In order to determine whether a post category or tag is used by your custom post type, try the following

$terms = wp_get_object_terms($post->ID, 'review_category');

The first parameter is obviously the post id but the second parameter here is actually the custom post type taxonomy which you can find on your custom post type 'categories' section (on the url). Once you have determine your taxonomy name, getting post taxonomy categories or tags should be a piece of cake.

Check Whether a page is using WordPress Custom Taxonomy Category

Well, today i faced a little problem on checking whether a page displaying on WordPress site is using a custom taxonomy post type. I need to determine whether that particular page is using a custom taxonomy post type and that particular page is using a particular custom taxonomy post category as well. In this case, i search a bit but couldn't find this answer easily which resulted me to write this in the case i need this in the future as well.

In order to find whether a particular page is a custom taxonomy category type, we test with the following sentence

 if(is_single()){
          } else if(is_tax('reviews', $term = 'Games')){
            $args = array( 'post_type' => 'reviews', 'showposts' => 72, 'tax_query' => array(
		array(
			'taxonomy' => 'review_category',
			'field' => 'slug',
			'terms' => 'games'
		)
            ) );
          }else if(is_tax('reviews', $term = 'Apps')){
            $args = array( 'post_type' => 'reviews', 'showposts' => 72, 'tax_query' => array(
		array(
			'taxonomy' => 'review_category',
			'field' => 'slug',
			'terms' => 'apps'
		)
            ) );
          }else
            $args = array( 'post_type' => 'reviews', 'showposts' => 72);

My custom taxonomy is 'reviews' and i wanted to check whether a particular page is type 'Apps' or 'Games', this way i am able to determine whether the particular page is using a custom taxonomy category which is different from using is_category which check post category.

 

“You do not have sufficient permissions to access this page.” Unable to access WordPress

Well, if you are unable to access your WordPress installation and received the message "You do not have sufficient permissions to access this page.", it is most likely that your WordPress installation, upgrading or migration has been screwed and the access of your user has all been lifted. In order to restore all these, you will need a bit of work to get it started again. However, there are still temporary solutions which you can undertake to make it work which doesn't completely solve your problem but temporary allow you to access WordPress. Just managed to get it working for a client so for all others who are seeking for a solutions as well, hope the tips help. Good Luck 🙂

Installing CSS Tidy For WordPress W3 Total Cache Plugin

In my previous two article where i explain how to install Memcache and HTML Tidy for WordPress W3 Total Cache plugin, i forgotten about CSS tidy as shown below,

CSS Tidy allows the server to minimize your CSS without you doing it yourself. However, this largely depend on whether your web hosting provider had installed it for you. Nonetheless,  it is a great additional for every webmaster to have!

Installing CSS Tidy For WordPress W3 Total Cache Plugin

Installing CSSTidy is quite simple on a centos linux machine. All you have to do is to issue the following command.

yum install -y csstidy.x86_64

If its successfully installed, it would really be that simple. However, if you can't find this option, it will be time to do some work.

Installing CSS Tidy alternative

Since the simple step wasn't there for you, we will have to try something manual. Fire up the below command,

rpm -ivh http://download.fedora.redhat.com/pub/epel/5/x86_64/csstidy-1.4-1.el5.x86_64.rpm

and see whether it went successful. If it does, csstidy is installed. If it doesn't try download it and rpm again.

wget http://download.fedora.redhat.com/pub/epel/5/x86_64/csstidy-1.4-1.el5.x86_64.rpm
rpm -ivh csstidy-1.4-1.el5.x86_64.rpm

If it still doesn't work, you should find it on your yum using the following command.

yum install -y csstidy.x86_64

since the rpm was added into your repo. Hope it works 🙂