Shortcode for stamping Woo product variation

cartoon image of stamp coming down on paper hard enough to send some ink spatters!

Here is some PHP code for WordPress/WooCommerce which will make PDF Ink 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 my_product_variation_shortcode( $content, $key, $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 the content, replace it with the product variation description
        $content = preg_replace( '/[VARIATION_DESC]/', $product->get_description(), $content );
        // When we find [VARIATION_NAME] in the content, replace it with the variation name
        $content = preg_replace( '/[VARIATION_NAME]/', $product->get_name(), $content );
     }
     // Return stamped input, changed or not
     return $content;

}
add_filter( 'pdfink_filter_placement_content', 'my_product_variation_shortcode', 10, 4 );

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!


For reference (maybe for people migrating from WaterWoo to PDF Ink) here is a snippet which would have worked with WaterWoo:

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 );
To top