Gift Wrapper allows us to set a different gift wrapping (or other add-on) category for each and every product, should we desire. (Each WooCommerce product also has its own Gift Wrapper settings.) But what if we want a category of product to always show a specific category of wrap? Meaning: what if we don’t want to manually set the Gift Wrapper category for hundreds of products in a category?
In version 6.0.3, we added a filter hook called ‘wcgwp_get_wrap_category_id‘ which allows us to do just that, programmatically. The filter takes the presumed category for a product (what it would naturally settle on based on Gift Wrapper settings) and allows us to tweak that further. So we are going to look to see what category a product is in, and if it’s in our target category (let’s say “candlesticks”) then we are going to give it Candlestick Wrap (we’ll pretend it’s a Woo category with ID #1057).
Example 1
function my_custom_wrap_category_id( $category_id, $product_id ) {
// Get product categories
$terms = wc_get_product_terms(
$product_id,
'product_cat'
);
foreach ( $terms as $term ) {
// If product is in certain category (800), give it different wrap category id 321
if ( 800 === $term->term_id || 'candlesticks' === $term->slug ) {
return 1057; // Candlestick Wrap category
}
}
return $category_id;
}
add_filter( 'wcgwp_get_wrap_category_id', 'my_custom_wrap_category_id', 10, 2 );
Taking this a bit further, and more realistically, we know we probably aren’t only going to map just one category. Most WooCommerce shops are more complex than that. Let’s re-write this hooked function to map more Woo product categories (by slug) to Gift Wrapper cats (by ID).
Example 2
function custom_wrap_category_id_mapping( $category_id, $product_id ) {
// Get product categories
$terms = wc_get_product_terms(
$product_id,
'product_cat'
);
if ( $mapped = array_map( 'custom_cat_map', $terms ) ) {
return (int) $mapped;
}
return $category_id;
}
add_filter( 'wcgwp_get_wrap_category_id', 'custom_wrap_category_id_mapping', 10, 2 );
/**
* Function to return values in mapped category array, if they exist
*
* @param object $term
* @return int|boolean
*/
function custom_cat_map( $term ) {
/**
* Here's what needs to be edited to suit your WooCommerce site
* Create an array of WooCommerce category slugs to their matched Gift Wrapper category IDs,
* using the following as an example:
*/
$mapped_values = array(
'hoodies' => 307,
'caps' => 578,
'visors' => 443,
'necklaces' => 198,
'socks' => 632
);
if ( in_array( $term->slug, $mapped_values ) ) {
return $mapped_values[ $term->slug ];
}
return false;
}
This feature is a realistic need for WooCommerce shops with a lot of product, to avoid having to enter wrap categories product-by-product.
Why only a filter hook to do this?
This feature has been requested once in the 9 years the plugin has been out.
A filter hook was created rather than creating a separate and possibly confusing settings page. Typically when features sound great to us, and make sense, but are uncommonly requested, action and filter hooks are added into the plugin rather than settings. This is common amongst most WordPress plugins. For folks requiring more specific or nuanced website customization, they’ll often need to go “under the hood” with their developer and add code (PHP, HTML, CSS, JavaScript, most commonly) to their site. The PHP code above can be added to your site using the Code Snippets plugin, or simply by opening and editing your child theme “functions.php” file to add these lines. If using the second example, do not use the first (use one or the other, or you might have a conflict)
Learning about WordPress action and filter hooks opens up a whole new world of website customization. At first, it is hard to understand how hooks would work if you don’t know much PHP language, but it gets easier with practice. If you don’t have the time to learn, contact your friendly WP developer for help.
Whenever I need a plugin to do “something else” that isn’t in the settings, I always look through the code or documentation to see if there is a filter or action hook which can help me accomplish that. This is why plugin authors add in hooks – so that users can get exactly what they want from a one-size-fits-most (but not all) plugin! Imagine how endlessly long and unruly WordPress settings panels would be if developers added in settings for every request, and thank the stars for hooks. 💫