Creating custom pages in woocommerce

what if you want to have a custom page in woocommerce? For example a page called abc under my-account

best way is to use shortcodes. Create a new page under my-account, ie my-account/abc and put in the shortcode [abc_shortcode]

then in functions.php

function abc_function( $atts ){
	wc_get_template( 'myaccount/abc.php' );
}
add_shortcode( 'abc_shortcode', 'abc_function' );

now you need to create the abc.php file which can contain anything

My experience with Magento, Woocommerce and other e-shop options

Magento

Magento has been a buzz word in the ecommerce industry for many years now. I worked with magento in my previous life and still get a lot of calls from many job agencies looking for magento developers today. The fact is that good magento developers are hard to find simply because having an in depth understanding of magento requires a bit of time. If you have good object oriented foundation, it  is not as complicated as many claimed. It was smooth sailing for me once I got the concept and the hang of it. The challenge for me was actually teaching the end user how to use it.

Magento was packed with features and was like amazon.com out of the box. Most people only used probably 10% of what it had to offer. The large code base and number of objects also meant a strong requirement for hardware and server tweaks to get the desired performance. Caching helped but it was not enough. I was puzzled why most people opted for magento as their first choice when they wanted an e-shop. Perhaps this just showed how successful magento’s marketing was – they could brainwash the public into believing that magento was the best solution for everyone. I often warned people that unless they have lots (and I mean LOTS) of budget, magento should be the first thing they should strike from their list. Personally, I was not comfortable with the cost involved to get customisation in magento. You just need to look at the cost of some simple extensions to get a feel of what’s going on.

In conclusion, I was comfortable with the software but not comfortable with money making policy. My gut feeling was that they would face tougher competition from other ecommerce solutions as time went by (and I was right). Relunctantly, I filed a divorce with magento.

So go ahead with magento want a robust ecommerce solution and has too much money to spare.

Hosted solution

Most hosted solution such as shopify requires a monthly fee and is usually very user friendly. This can be a good option for people who requires little customisation and don’t want to host their own shopping cart. I never considered this as a good option for me because I always believed that personalisation and customisation was the key to improve client relationship. Hosted solution means limited customisation. The logic is simple, their software should be generic for everyone and they can’t break their software just to accomodate your requirements.

Woocommerce and Other Ecommerce Software

I have used other software over the years as well but came to like Woocommerce recently. Woocommerce offers the flexibility and robustness that an ecommerce shop should have. As it is open sourced (as with magento community edition), I had full control over the behaviour of the shop. There were enough hooks in the code to do what I want. I could also make it work with other wordpress plugins such as  membership management system, LMS, caching, seo…etc. The only thing I had to be careful was not to make the shop too bloated, ie by adding too much features into it, thereby affecting the performance.

I believe there are other good options out there and time will tell us when one stands out above the rest.

woocommerce: widget login redirect

If the woocommerce_login_widget_redirect hook doesn’t work, it is because the redirection occurs even earlier, in the core wp_signon function. so we need to hook on to wp_login instead, for example

// allow user to redirect to the same page from the place that they login. The exception is wp-login.php
add_action( 'wp_login', 'my_login_redirect', 2, 2 );
function my_login_redirect( $login, $user ) {
  if ($_SERVER['REQUEST_URI'] != '/wp-login.php') {
  // redirect to the referrer
  wp_safe_redirect($_SERVER['HTTP_REFERER']);
  exit;
  }
}

Woocommerce: Allow users to extend downloadable products expiry after login

There are a few ways that we can protect downloadable product links in woocommerce.

Option A: Limit the number of download per user

Option B: Enforce Logged-in before download.

Option C: Set an expiry date for the download link.

Personally, I dislike option A and B because they are a hassle. Option C sounds more viable but the complain might come in after the expiry date. I think the best solution is to allow users to download the product immediately upon purchase (or in the email) and subsequently, they have to be logged in order to download the product. However, woocommerce doesn’t allow “log in to download” per product (we have to do it globally). So another option is to extend the expiry date everytime the user login. We can do this using the woocommerce_after_my_account hook.

# extend download expiry date by 1 day everytime user logins to dashboard
add_action('woocommerce_after_my_account', 'update_downloadable_expiry_now');
function update_downloadable_expiry_now() {
  global $wpdb;
  $table = $wpdb->prefix.'woocommerce_downloadable_product_permissions';
  $query = $wpdb->prepare("update $table set access_expires=CURDATE() + INTERVAL 1 DAY where user_id='".wp_get_current_user()->ID."' and access_expires is not NULL");
  if ($wpdb->query($query) === FALSE) {
    _e("could not update downloadable product access expiry date.", 'woocommerce');
  }
}

Set Featured Image not appearing in child theme

Some parent theme switched off certain features which would affect the child theme. For example, the parent theme might have this line:

add_theme_support( 'post-thumbnails' , array( 'post' ));

this means that there won’t be featured image support for post_type other than “post” in the child theme. If featured image support is needed, the child theme needs to overwrite this feature after the parent theme has been initialised. We need to use the “after_setup_theme” hook.

add_action( 'after_setup_theme', 'child_add_theme_support');
function child_add_theme_support(){
  add_theme_support( 'post-thumbnails', array( 'post', 'page', 'product' ) );
}      

The above code will then allow featured image in post, page and product for example.

woocommerce: getting paypal ipn to update order status of downloadable product to complete

Woocommerce has paypal functionality built-in to handle downloadble product. After the paypal IPN is setup, it should ping the merchant server on the status of the payment and update the order status from “pending” to “complete”. If you are seeing the “processing” status, it means that you haven’t check the right boxes when setting up the product. For any product that you want IPN to update from “pending” to “complete”, it needs to be both “virtual” and “downloadable”. So much for what virtual and dowloadable means… I realised that only after reading line 1161 in plugins/woocommerce/classes/class-wc-order.php

if ( $_product->is_downloadable() && $_product->is_virtual() )

I then realised woothemes does document this part a bit – http://wcdocs.woothemes.com/user-guide/managing-products/