Archive for the ‘WooCommerce’ Category

Automatically Create WooCommerce Product for PDF Download

Monday, January 9th, 2017

A challenging WordPress project that I recently worked on required that a WooCommerce no-cost virtual, downloadable product be created each time a PDF was uploaded into a certain section on the screen of certain custom post types.

  • A history of the downloads of a PDF product was to be kept for years.
  • Only PDFs uploaded in the PDF Product Group section (a section created as part of the solution for this business requirement) were to be converted into WooCommerce PDF products. (PDFs could be uploaded and linked to inside the regular WordPress post content area. Those PDFs were to be handled by WordPress as it normally does, and were NOT to be added as WooCommerce products.)
    Custom Field Group for PDF Product

    Custom field group for PDF product shown on this client’s custom post type for Case Studies

  • The client had multiple custom post types. Some allowed PDF product uploads and others did not.
  • On the custom post types that allowed PDF product uploads, a PDF was not required to be uploaded.
  • Only one PDF product could be associated with a post at any given time.
  • The reason the PDFs were added to WooCommerce as products was so that the client could track which people downloaded which PDF products. (I was not asked for a recommendation on how I would implement this; I was asked to make it happen using WooCommerce as the manager.)

What I did was:

  1. Make a list of the minimum fields that WooCommerce creates when a virtual, downloadable product is created
  2. Confirm how WooCommerce handles no-cost products
  3. Create a custom field group, the PDF Product Group, that contains the Title of the PDF to be used for the Product title, an upload file field (for uploading the PDF), a small description field to be used as the PDF product’s description.
  4. Added an ‘action’ function in functions.php that runs on ‘save_post’. The function checks whether the post is one of the custom post types that can have a PDF. Then it checks whether a PDF has been successfully uploaded, followed by verifying that a title and a description have been added.
  5. Once all of those validations have been passed, a WooCommerce virtual, downloadable product is created using those fields and others (defined in the first step above) that are set to default values (like the product category which changes according to the custom post type). In all, 29 postmeta records are added.
  6. Finally, the one-to-one relationship between this post and the PDF product that was just created is recorded by adding another postmeta record. This effectively defines the post as the ‘parent’ of the product.

Adding the product for the first time (processing above) was the easy part. Deleting the product was the second easiest.

Remember that the PDF product is related to the ‘original‘ post. That original post has a unique identifier, the post ID. When a WordPress post is edited, a new post is created with its own unique ID (the revision) and the original post’s ID is added to the ‘post_parent’ field in the revision’s record, so, I had to be careful about which post ID I was referencing in the PHP programming: the revision’s post ID, the parent’s post ID, the PDF product’s post ID, or one of the postmeta record’s post ID.

Automatically deleting the WooCommerce PDF product

Since this client was tracking who downloaded which PDFs, the PDF products were not deleted. Instead, they were marked as inactive so that the PDF free products would not display as active downloads on any of the pages – and – the download history was preserved for reporting purposes.

The delete process works like this:

  • Check to see if one of those postmeta ‘parent-child’ records exists for the ‘parent’ post. (By business requirement, there can be only one…if there is any.)
  • Check whether there is a value in the PDF product custom file upload field of the current post.
  • If there is no product in the PDF product custom field of the revision post, use the WooCommerce product post ID in the parent-child record of the revision’s parent to retrieve the product information and mark it as inactive.
  • If there is a product in the PDF Product custom field of the current post, then maybe the records needed to be updated. (see next section)

Updating the WooCommerce PDF product

The toughest part was trying to decipher whether the PDF had been modified and re-uploaded, and, if so, was it uploaded with the same name or a different one? When updating WooCommerce downloadable products directly, WooCommerce keeps track and knows which version to serve for download. WooCommerce can even send a notice to users who downloaded the previous version that there is a new version available. Updating a WooCommerce downloadable product from outside WooCommerce upped the level of complexity.

One thought was to check the most recent PDF name against the name of the PDF originally uploaded. While this final piece was being coded and unit tested, the program manager decided to change course.  The WooCommerce-as-the-manager approach was abandoned. Before that happened, all the other code was unit and system tested, and working – as was the ability to track who downloaded what.

Limited Checkout Fields for Free PDF Downloads

Since the client wanted to know who downloaded which PDF, it was necessary for the person downloading to be logged in.

  • If the person downloading did not already have an account at the website, an account needed to be created.
  • If they were downloading only the FREE PDFs, it was not necessary to fill in the multiple checkout screens that WooCommerce normally presents. The WooCommerce Checkout for Digital Goods plugin was used to capture minimal contact information and create a WooCommerce account for them.
  • If there were pay-for products also in the cart, the purchaser would experience the normal, multi-screen WooCommerce checkout process (if they did not already have an account).

So, what seemed like a simple requirement “use WooCommerce to track who downloads the FREE PDFs” became quite challenging when adding and maintaining WooCommerce records outside of the WooCommerce Products module.



3 Ways to Change Products per Page in WooCommerce

Wednesday, August 19th, 2015
  1. WooCommerce products are saved to ‘post’ pages, so, from WordPress’ perspective, it just wants to know how many “posts” to display per page. Go to SettingsReading and change the number of Posts to display per page (“Blog pages show at most”). Changing this number affects all instances of posts per page, so, if you are also hosting a blog at the website and want a different number of posts per page for products and another for blog posts, use one of the solutions below.
  2. Non-Techies: Install a plugin. One is WooCommerce Products per Page
  3. Techs can edit the functions.php file and one line of code at the bottom:
    // Display 36 products per page
    add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 36;' ), 20 );

Hopkinton Lumber Launches a new Responsive WordPress Website

Monday, August 17th, 2015

Congratulations to Hopkinton Lumber of Hopkinton, MA on its new website. This is the first website for Hopkinton Lumber. Hopkinton Lumber is a friendly, family-owned establishment on Main Street, and, as far as the owners are concerned, Hopkinton’s best-kept secret!

Early in 2015, Hopkinton Lumber decided it was time to use the Internet to get the word out about itself and its products. The owners were tired of hearing, “I didn’t know you sell paint supplies.”, “I didn’t know you sell home improvement products.”, “I didn’t know you sell contractors’ supplies, automotive repair products, camping gear, backyard furniture, landscaping tools, pet grooming supplies, holiday decorations, and whatever-else-you-can-name”.

Well, Hopkinton Lumber has over 10,000 products in stock! As the name implies, it started out selling only lumber, but, the front building was converted into a hardware/home improvement store many years ago.
Responsive WordPress Website with custom WooCommerce It is conveniently located on Main Street (Rte. 135) within 1 mile of route 85 and route 495. The owners invite local home owners and contractors to stop into the store and introduce themselves.

Primary Business Goals:

  • Get the word out to local home owners and contractors about the wide array of home improvement products in stock
  • Showcase products in a catalog-like fashion
  • Allow prospective clients a convenient way to browse the online catalog of products
  • Implement solutions that make it easy for staff to update and customers to navigate


  • Use WordPress so staff can update content on the web pages
  • Use a responsive WordPress theme so that the website visitor enjoys a pleasant experience while viewing the website regardless of the device (cell phone, tablet, laptop, desktop) used by the visitor
  • Use a shopping cart/catalog building product with an easy learning curve. The WordPress-ready shopping cart WooCommerce was installed in order to provide staff the ability to effortlessly add products and to be able to categorize each product in multiple categories.
  • WooCommerce customizations included: The Catalog viewing and Add to Cart functionality were used, and the check out, my account and related functionalities were disabled.
  • Custom WooCommerce Plugin: Custom PHP code bundled in a plugin that allows customers to print out a shopping list was developed. The shopping list prints to a PDF for easy download to a cell phone or formatted printing. The Print Shopping Cart option displays dynamically once items are added to the shopping cart.

Shopping List PDF

Click for larger image

The website is live and ready for visits. Staff are loading the products each week, so, please return to view additional products as time goes on. And…No, we did not forget to post the email or an online contact form. Hopkinton Lumber is a friendly face-to-face operation. Staff specializes in welcoming each person, taking time to help each one find the best solution for their project, assisting or keeping a distance as each customer desires.

Stop in and let Hopkinton Lumber know that the word is out. You know that there are thousands of products! Heck, the hardware store structure is an original post and beam building with a 100 year history – and – that alone is a reason for visiting!