Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
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);
//Create our new variables
$newvariables = [
'%wc_product_price%' => $product->get_price(),
'%wc_product_name%' => $product->get_name(),
'%wc_product_description%' => $product->get_description(),
'%wc_product_short_description%' => $product->get_short_description(),
'%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(),
];
/*
* 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
You can’t perform that action at this time.