Skip to content

Instantly share code, notes, and snippets.

Last active April 5, 2021 04:57
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
//Remove the <?php opening tag to get rid of the red exclamation mark in the Code Snippets plugin
function mbp_add_woocommerce_product_variables($variables){
//Get the product ID from the variables
$id = intval($variables["%ID%"]);
//Check whether the WooCommerce wc_get_product function exists, and this is a product post type
if(!function_exists('wc_get_product') || get_post_type($id) !== 'product'){ return $variables; }
//Get the product
$product = wc_get_product($id);
//Parse WooCommerce rich content fields.
$product_description = new \PGMB\Vendor\Html2Text\Html2Text($product->get_description(),['width' => 0]);
$short_description = new \PGMB\Vendor\Html2Text\Html2Text($product->get_short_description(),['width' => 0]);
//Create our new variables
$newvariables = [
'%wc_product_price%' => $product->get_price(),
'%wc_product_name%' => $product->get_name(),
'%wc_product_description%' => $product_description->getText(),
'%wc_product_short_description%' => $short_description->getText(),
'%wc_product_sku%' => $product->get_sku(),
'%wc_product_virtual%' => $product->get_virtual(),
'%wc_product_regular_price%' => $product->get_regular_price(),
'%wc_product_sale_price%' => $product->get_sale_price(),
'%wc_product_price_including_tax%' => wc_get_price_including_tax( $product ),
* More functions that can be used can be found here:
//Return our variables to the Post to Google My Business pluign
return array_merge($variables, $newvariables);
add_filter('mbp_placeholder_variables', 'mbp_add_woocommerce_product_variables');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment