Create a custom product selection template for WooCommerce One Page Checkout to change how products are displayed on a checkout page. Test the template on a staging site before using it for live orders.
Note:
This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.
Creating custom product selection templates
↑ Back to topThis guide assumes familiarity with HTML, PHP, CSS, and WooCommerce templates.
The One Page Checkout shortcode includes a template attribute to determine which template to use for displaying product selection fields.
You can create custom product selection templates to customize the display of products on your single page checkout.
To create a custom template, you need to:
- Create a file in your theme’s
/woocommerce/checkout/templates directory. - Use HTML and PHP to display the products passed to the template.
- Make sure there is an input field for each product with the product’s ID as a data attribute.
- Optionally register the template with the
'wcopc_templates'filter to make it available in the shortcode’s graphical interface.
Create a template file
↑ Back to topOne Page Checkout conforms to WooCommerce’s template structure.
In your active theme, create woocommerce/checkout/{template-name}.php, replacing {template-name} with a name such as my-pricing-table. Use a child theme when appropriate so a parent-theme update does not overwrite the file.
Display Products for Selection
↑ Back to topOnce you have created a template file, you need to add the markup and PHP to the template to display each of the products for that page.
Before beginning to develop your template, review one or two of the built-in templates. These examples will give you a quick overview of the requirements for a custom template and help illustrate the information discussed in this section.
Product loop
Your template will receive an array of products to display. Your template should loop over this array to display each of the products.
The products are passed in a variable named $products with each of the elements representing a product object, for example, WC_Product_Simple, WC_Product_Variation or WC_Product_Subscription.
As these objects are standard WooCommerce product classes, they include all the standard data and methods of a product object to use in the template, for example WC_Product::get_title().
For example, this minimal template displays each product’s name and the built-in add-to-order button. Save it as woocommerce/checkout/my-pricing-table.php in your active theme or child theme.
<?php
defined( 'ABSPATH' ) || exit;
foreach ( $products as $product ) {
$selected = wcopc_get_products_prop( $product, 'in_cart' ) ? ' selected' : '';
echo '<div class="product-item' . esc_attr( $selected ) . '">' . esc_html( $product->get_name() );
wc_get_template(
'checkout/add-to-cart/button.php',
array( 'product' => $product ),
'',
PP_One_Page_Checkout::$template_path
);
echo '</div>';
}
Extra data
One Page Checkout stores cart-related data on the product for use in templates. Retrieve it with wcopc_get_products_prop( $product, $prop ):
in_cart: a boolean value indicating whether the product is already in the cart or not. This is useful for pre-checking input values on page load.cart_item: if the item is in the cart, the cart item’s data is included here. This is useful for pre-filling quantity inputs on page load.
Selection fields
↑ Back to topTo select a product, the customer needs a selection field. One Page Checkout supports the following input fields:
buttonradiocheckboxanumber
Built-in template parts
The simplest method for adding selection fields to your template is to use built-in template parts.
For example, to display the built-in add-to-order button, include the following line inside a product wrapper:
<?php wc_get_template( 'checkout/add-to-cart/button.php', array( 'product' => $product ), '', PP_One_Page_Checkout::$template_path ); ?>
Similarly, to include a quantity input, use the following code:
<?php wc_get_template( 'checkout/add-to-cart/quantity-input.php', array( 'product' => $product ), '', PP_One_Page_Checkout::$template_path ); ?>
The same can be done for radio or checkbox inputs.
For buttons and links, use a product-item wrapper and add its selected class when the product is already in the cart, as shown in the example above. The built-in button is hidden after selection; the customer can remove the item from the order summary.
Custom product selection fields
You may notice the built-in templates include a data-add_to_cart attribute on each product selection field.
This attribute is used by One Page Checkout to identify product selection fields and handle the addition or removal of the item to or from the order.
To create a custom product selection field, include a data-add_to_cart attribute containing the product ID returned by $product->get_id().
By using the built-in template parts for product selection input fields, this value will be automatically set.
Register your template
↑ Back to topOnce you have created a template, use its file name without the .php extension as the shortcode’s template attribute.
You can also register the template with One Page Checkout to have it display in the list of templates included with the graphic interface for creating the One Page Checkout shortcode.
To register your template, add a callback to the wcopc_templates filter in your child theme’s functions.php file or a site-specific plugin. Add the template’s details to the $templates array passed to your function. Test the change on staging first.
For example, register the my-pricing-table template created above:
<?php
/**
* Add a custom product selection template.
*
* @param array $templates Available templates.
* @return array
*/
function eg_add_opc_template( $templates ) {
$templates['my-pricing-table'] = array(
'label' => __( 'My product buttons', 'eg' ),
'description' => __( 'Display product names and add-to-order buttons.', 'eg' ),
);
return $templates;
}
add_filter( 'wcopc_templates', 'eg_add_opc_template' );
The key used in the $templates array should be the template’s file name (excluding the extension). The label element of the array is the name displayed on the One Page Checkout dialog. The description element is used for the tooltip next to the template’s name.
Verify the template
↑ Back to topOn a staging site, add the One Page Checkout shortcode to a test page with the template name and a test product ID. For example, replace 123 with your test product’s ID:
[woocommerce_one_page_checkout product_ids="123" template="my-pricing-table"]
Preview the page, add and remove the product, and confirm the checkout updates as expected. If the template does not appear, check the file location and the template name without the .php extension. Restore a built-in template while you troubleshoot.
For shortcode options and built-in templates, see WooCommerce One Page Checkout.