Categories
Snippets

WooCommerce – Remove product data tabs and hook content in sequence instead

Categories
Snippets

WooCommerce – Change number of displayed upsells on product pages

Categories
Snippets

Showing quantity inputs in WooCommerce loops

Here is a quick snippet showing how you can add quantity inputs to WooCommerce loops for simple products. This used to be only possible through template edits, but is now doable through filters because they are magic.

The end result:

2016-04-21 at 18.21.png

Categories
Snippets

Github to WordPress.org deploy script

Today I polished, documented, and open sourced a bash script I’ve been using to do plugin deployments from Github to WordPress.org (plus I wanted an excuse to use the new WordPress.com desktop app which is awesome by the way). It’s based on code Barry Kooij sent me a while back. You can find it here:

https://github.com/mikejolley/github-to-wordpress-deploy-script

It handles Github tagging, removing junk files, and SVN tagging with minimal effort. It also has a quick checklist to stop you forgetting to set things in the readme.txt like the stable version (I’ve been guilty of forgetting this in the past).

Once downloaded to your machine, the script requires a Github access token, and a few edits to tell it which plugin you’re deploying.

After you’ve added the details specific to your project, you simply need to open it’s parent directory in terminal, and run the command:

sh release.sh

It then prompts you for a version, branch, and handles the deployment process.

demo

The above demo is from a release of WP Job Manager I did today showing it working nicely. Hope it’s useful!

Categories
Snippets

Using post status transitions to send custom alerts (in WP Job Manager)

Something I get asked a fair bit with regards to my plugins is how to send an alert during an event, for example, “when a job expires in WP Job Manager, how can I notify the user?”

Plugins which use custom post types, like WP Job Manager, are likely to use custom/standard post statuses too – it’s pretty common. Using these statuses you can easily hook-in a custom function and trigger and email.

This is a quick tutorial for hooking into a status change and sending an email for WP Job Manager.

Continue reading “Using post status transitions to send custom alerts (in WP Job Manager)”

Categories
Snippets

Manipulating shipping packages in WooCommerce 2.1

Up until 2.1, each order had to be shipped via a single method with a single price. 2.1 changes that and allows each package to be quoted and shipped individually.

By default, each order is a package, so to get this new functionality to kick in you must split it into multiple packages first.

Filtering the packages

Each package has cart items, a total cost, applied coupons, and the destination:

Once items are placed into this package, the package is then filtered through the woocommerce_cart_shipping_packages hook. This is where you can manipulate the packages and create more if needed.

Lets say for this example we have a regular items and a bulky item in our cart:

2013-12-12 at 15.16

The bulky item cannot ship with regular items, so we give it a shipping class called ‘Bulky’ and then filter the packages to separate it out via some code:

This code puts bulky items in one package, and regular items in another – once done, during cart and checkout you will get a shipping section per-package, and each can be chosen independently:

2013-12-12 at 15.30

Limiting available methods for a package

Each package can now also be marked to ‘ship via’ a method of your choosing. This is useful if certain packages can only be shipped by certain methods.

For this example, lets ensure bulky items are only shipped via flat rate, and not shipped for free.

Notice the ‘ship_via’ row which has been added. Now during cart and checkout, only flat rate will be allowed for the bulky items:

2013-12-12 at 15.34

Neat!

After an order is placed, this is displayed in the backend like this:

2013-12-12 at 15.37

Use-cases

So how can this new feature be used? Here are some example use cases:

  1. Shipping method restrictions
  2. Per product shipping with a different selectable method per product
  3. True per-product shipping costs
  4. Shipping per class
  5. Free shipping for qualifying products only, not the whole cart

Right now its obviously only do-able via code, but I plan on building some extra functionality into the per-product shipping plugin to use this at some point, and I’m sure someone will make a UI for this eventually 🙂

Categories
Snippets

Using the WordPress 3.5 Media Uploader within plugins

This week whilst working on WooCommerce 2.0 beta I was faced with the task of rewriting the media unloaders to use the fancy new interface in 3.5 instead of the tired old thickbox modal windows used in earlier versions.

This was no easy task mainly due to the lack of documentation available for the new system, but I persevered and wanted to share my experiences in this post.

Adding an inline uploader to a field; the simplest solution

We don't want this
We don’t want this

The simple solution is to simply trigger the bog standard uploader, much like was possible with the old thickbox media uploaders. This post pretty much explains how to do that.

This however was no good to me;  it gives you a media uploader with a sidebar and other confusing elements not relevant when adding images to fields inline.

A better solution with a custom frame

To remove the unwanted elements and to simplify the uploader you can create a custom frame (this was inspired by the header image uploader in Twenty Twelve).

This gives us a cut down version which allows upload, or selecting a file from the media library:

Screen Shot 2012-12-21 at 07.35.09

This also picks up the title and the button text from the button’s html (notice the data( ‘uploader_title’ ) and data( ‘uploader_button_text’ )) using data-uploader_title and data-uploader_button_text attributes respectively.

Handling multiple files

To handle multiple files set multiple to true:

Then you just need some tweaked handling on select:

Passing data to the uploader script

The largest hurdle I faced was finding a way to pass data through to the upload script. I had two reasons for needing to do this:

  1. I needed to pass through a different post ID to attach the image to if uploading from an edit post page, but attaching to a different post (in this case a product variation in WooCommerce).
  2. I needed to pass through a custom variable so I could pick it up via the upload_dir hook to upload *some* files to different directories.

This was largely done through trial and error but I got there in the end (thanks Justin for the debug assistance) by setting the wp.media post ID and then re-setting this everytime we open the frame.

One final note on enqueuing the scripts; if you use these on a page that doesn’t already load the media uploaders (e.g. a custom admin page) remember to call wp_enqueue_media(); This will get everything loaded into the page for you.

Overall, I’m pleased with the result and the new uploaders. I hope this post helps others implement them.

Categories
Snippets

add_menu_page supports decimal positions

When adding top level menu pages to WordPress admin (using add_menu_page) more often or not you’ll end up conflicting with other plugins. When two plugins share the same menu position, one is not shown:

WARNING: if 2 menu items use the same position attribute, one of the items may be overwritten so that only one item displays!

Since there are a limited number of integer positions in the menu this could be a problem, however, handily WordPress actually supports decimal positions. In this example I’m using ‘55.5’ instead of 55. This should reduce the risk of conflict significantly:

$main_page = add_menu_page(__('WooCommerce', 'woocommerce'), __('WooCommerce', 'woocommerce'), 'manage_woocommerce', 'woocommerce' , 'woocommerce_settings_page', null, '55.5' );

Note that the codex and the source state that integers should be used, even though the decimals do work. Kudos to Gary Jones for pointing this trick out.

Categories
Snippets

Finally fixed the flicker

Since developing my site’s theme there has always been this annoying browser bug causing a ‘flicker’ whilst rolling over any element with a css3 transition. I’ve finally found a decent workaround which involves adding 1 line of css:

-webkit-transform: translateZ(0);

This triggers the GPU to do the animation rather than the CPU and has fixed my flickering, as well as making the animation look smoother.

This line needs to go on the un-animated state, so in my case I applied it to the elements themselves rather than the :hover.

Kudos to Mauricio on Stack: http://stackoverflow.com/a/9727924

Categories
Snippets

WooCommerce – Output a simple, printable stock/inventory report

screen-capture-1

Another user request today to output a list of products, as well as their stock levels, to be used to compare the amount of real stock in hand with what WooCommerce thinks is in stock.

The code below can be added as a page template in your theme. Once added to your template, just create a page in WordPress admin and assign it the “Stock Report” page template.

Theres a check at the top of the page to only let admin users in, and when viewed the page will give you a simple list of products, SKU’s and stock levels which you can then print out.

I hope you find it useful 🙂