Skip to content

Instantly share code, notes, and snippets.

@Rhytz
Last active April 5, 2021 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Rhytz/e11b831c04a47486f7203407164a9923 to your computer and use it in GitHub Desktop.
Save Rhytz/e11b831c04a47486f7203407164a9923 to your computer and use it in GitHub Desktop.
<?php
//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:
* https://businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/
*/
//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