WooCommerce – one of the biggest names in WordPress plugins, is equally preferred by independent business owners for its user-friendliness and a full range of personalization features. For a business owner selling a range of products online, having the flexibility to showcase just 3-4 products on the main page is of no use. What WooCommerce delivers is the ability to go full-range with neat, organized variations of product list tables for your online store.
That said, with more features to offer variation settings among similar products, who gets to decide what attributes you can add? This is where it gets interesting as you can generate variable products (with its attribute tables) via coding. However, despite the many types of product variation filtering techniques, out-of-the-box variation filtering has remained a challenge for both merchants and audiences because of a non-alignment of technical factors involved. But, before we get into coding the variable product, let’s learn what these variation tables are and how it benefits both the business owners and audiences.
Benefits Of Using Woocommerce Custom Variation Tables
The unique functionality of the WooCommerce product variation tables is their full range of options that it provides to the business owner for organizing and showcasing products right. Some of the benefits include:
- Product variations in the data table so users can sort out and filter the products one needs – in table and grid formats.
- Creating a WooCommerce variations table can enhance your product variation visibility by helping your customers to spot the products they need quickly and add them right into their carts.
- You can also personalize the table structures into seven different types. Since all of them are mobile responsive designs, your customers can access all the data they need to shop a product right on their mobile screens and shop even when they’re traveling.
- WooCommerce product variation tables also come with customizable borders and text colors, background images to present your products with the right kind of personalization they need.
- To stand out from the vast array of products, the search and filter functionalities in a table can be helpful for your users to quickly get to the type of products they’re looking for easily.
- WooCommerce variation filtering doesn’t just make the products look neat but also gives the audience the unique choice to view all variations of products in a single click.
Creating Woocommerce Variation Tables With Attributes
There are many ways in which you can code the following snippet to get your own variable product ID through with its new attribute values. We’ve used EnlighterJS below, and since it’s a multidimensional array, the parent product of variable product ID is essential to set attributes of values, prices, and stock. Now, when the function executes, it’ll test to see if there’s an attribute of the value, and if not:
- It will create a product attribute for the case
- It will set it in the parent variable product.
/**
* Creating a product variation for a defined variable product ID.
*/
function create_product_variation( $product_id, $variation_data ){
//Getting the variable product object (parent)
$product = wc_get_product($product_id);
$variation_post = array(
‘post_title’ => $product->get_title(),
‘post_name’ => ‘product-‘.$product_id.’-variation’,
‘post_status’ => ‘publish’,
‘post_parent’ => $product_id,
‘post_type’ => ‘product_variation’,
‘guid’ => $product->get_permalink()
);
//Creating the product variation
$variation_id = wp_insert_post( $variation_post );
//Testing an instance of WC_Product_Variation object
$variation = new WC_Product_Variation( $variation_id );
//Iterating each variation attribute
foreach ($variation_data[‘attributes’] as $attribute => $term_name )
{
$taxonomy = ‘pa_’.$attribute;
//In case of no taxonomy, creating it
if( ! taxonomy_exists( $taxonomy ) ){
register_taxonomy(
$taxonomy,
‘product_variation’,
array(
‘hierarchical’ => false,
‘label’ => ucfirst( $attribute ),
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => sanitize_title($attribute) ),
)
);
}
//Checking if the term exists, if not creating it
if( ! term_exists( $term_name, $taxonomy ) )
wp_insert_term( $term_name, $taxonomy ); //creating term
$term_slug = get_term_by(‘name’, $term_name, $taxonomy )->slug;//Getting the term slug
//Getting post Terms names from the parent variable product
$post_term_names = wp_get_post_terms( $product_id, $taxonomy, array(‘fields’ => ‘names’) );
//Testing if the post term exists and if not we set it in the parent variable product.
if( ! in_array( $term_name, $post_term_names ) )
wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
//Saving the attribute data in the product variation
update_post_meta( $variation_id, ‘attribute_’.$taxonomy, $term_slug );
}
##Saving all data
//SKU
if( ! empty( $variation_data[‘sku’] ) )
$variation->set_sku( $variation_data[‘sku’] );
//Prices
if( empty( $variation_data[‘sale_price’] ) ){
$variation->set_price( $variation_data[‘regular_price’] );
} else {
$variation->set_price( $variation_data[‘sale_price’] );
$variation->set_sale_price( $variation_data[‘sale_price’] );
}
$variation->set_regular_price( $variation_data[‘regular_price’] );
//Stock
if( ! empty($variation_data[‘stock_qty’]) ){
$variation->set_stock_quantity( $variation_data[‘stock_qty’] );
$variation->set_manage_stock(true);
$variation->set_stock_status(”);
} else {
$variation->set_manage_stock(false);
}
$variation->set_weight(”); //resetting weight
$variation->save(); //saving the data
}
Please ensure that all the coding snippets go into the functions.php file.
Bottomline
The issue with the slow performance of variation filtering lies in the fundamental architecture of WooCommerce and its working integration with WordPress APIs. Since the product data gets stored in the wp_post and wp_post meta in WordPress, getting more variations in queries such as product filters, stock checks get slower. As per WooCommerce’s proof of concept on the shortcomings of out-of-the-box variation filtering, most developers can toggle between the logical operators AND as well as OR for filtering multiple attributes of different types – causing to show only the results/products that match with the values of all filtered attributes.
If you’re an aspiring WooCommrece developer with some PHP experience looking for work, we have some suggestions.
Wire Media, California
They’re a UI/UX design agency specializing in custom web development, business intelligence, etc. They’re currently seeking WordPress developers to join their team and they can be a great opportunity to kickstart your career.
Turing, Palo Alto
If you’re a developer who loves challenges and would like to work on remote for top startups on a contractual basis, they can be a great place to get started. Starting with Front-end developer jobs, and many more technical positions at multiple company openings – they’re a hub for finding your preferred roles.
Codeclouds, Indiana
An industry leader in delivering innovative and custom web development, designing, and eCommerce solutions, they’re always on the lookout for getting talented professionals on board. If you’re interested in joining an ever-growing team of 400+ global experts and looking for database administrator jobs, now you know where to go!