Website Building » WooCommerce » How Do I Create a Custom Single Product Page in WooCommerce Programmatically?

How Do I Create a Custom Single Product Page in WooCommerce Programmatically?

Last updated on January 19, 2023 @ 2:52 pm

If you’re selling just a single product, you may want to consider creating a custom WooCommerce single product page template. This will allow you to customize the look and feel of your product pages to better match your brand, and it can also help you to reduce clutter and make it easier for customers to find the information they need.

In this article, we’ll show you how to create a custom WooCommerce single product page template, step-by-step. We’ll also provide some tips on how to style your pages using HTML tags.

Creating a Custom WooCommerce Single Product Page Template

The first thing you need to do is create a new file in your child theme’s directory. You can name this file anything you like, but we recommend something like “single-product-custom.php”.

Once you have your new file created, open it up in a text editor and add the following code:

<?php
/*
Template Name: Custom Single Product
*/

get_header();

// The loop
while ( have_posts() ) : the_post();

// Get the product information
$product = wc_get_product();
$product_id = $product->get_id();
$product_title = $product->get_title();
$product_price = $product->get_price();
$product_image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'full' );
$product_description = $product->get_description();

// Output the product information
echo '<div class="product-info">';
echo '<h1>' . $product_title . '</h1>';
echo '<img src="' . $product_image[0] . '" alt="' . $product_title . '">';
echo '<p class="price">' . $product_price . '</p>';
echo '<div class="description">' . $product_description . '</div>';
echo '</div>';

endwhile;

get_footer();
?>

This code creates a new template for your single product pages and sets the template name to “Custom Single Product”. It then uses the WooCommerce functions to get the product information, such as the title, price, and image. Finally, it outputs this information within a div with the class “product-info”.

You can then use CSS to style the elements within this div to match your brand and design. For example, you could use the following CSS to change the font of the product title:

.product-info h1 {
font-size: 2em;
font-family: Arial, sans-serif;
}

Note that this is just a sample code, you need to customize it as per your requirement and you need to have knowledge of php and css to make it work as expected.

Once you’ve created your custom WooCommerce single product page template and styled it to your liking, you can activate it by going to the WooCommerce settings in your WordPress admin and selecting it from the dropdown menu under the “Product” tab.

And with this, you now have a custom single product page template that you can use to make your product pages look great and provide a better user experience for your customers.

PRO TIP: This article contains code snippets and is intended for developers. If you are not a developer, we recommend that you hire a developer to help you with this.
Madison Geldart

Madison Geldart

Cloud infrastructure engineer and tech mess solver.