Shortcode for watermarking Woo product variation
Here is some PHP code for WordPress/WooCommerce which will make WaterWoo PDF recognize TWO custom shortcodes in any watermark content: [VARIATION_DESC] and[VARIATION_NAME]. These translate to variation description and variation name, both aspects of a WooCommerce variable product.
function wwpdf_product_variation_shortcode( $input, $order_id, $product_id ) {
$product = wc_get_product( $product_id );
// Only for product variation
if ( $product && $product->is_type('variation') ) {
// When we find [VARIATION_DESC] in a watermark, replace it with the product variation description
$input = preg_replace( '/[VARIATION_DESC]/', $product->get_description(), $input );
// When we find [VARIATION_NAME] in a watermark, replace it with the variation name
$input = preg_replace( '/[VARIATION_NAME]/', $product->get_name(), $input );
}
// Return watermark input, changed or not
return $input;
}
add_filter( 'wwpdf_filter_overlay', 'wwpdf_product_variation_shortcode', 10, 3 );
add_filter( 'wwpdf_filter_footer', 'wwpdf_product_variation_shortcode', 10, 3 );
This code (everything above the dashed line) should get added anywhere in your functions.php file. If unsure where to put it, add it at the end of the file. You could also add the snippet using the WordPress plugin called “CODE SNIPPETS” (front and back end).
This snippet development was sponsored by customer Michael. Thanks Michael!
(advertisement)
Simply extraordinary! Your in-depth analysis and crystal-clear explanations make this a must-read. The amount of valuable information you’ve packed in here is amazing.