Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add price variables for WooCommerce variations
<?php
//Remove the <?php opening tag to get rid of the red exclamation mark in the Code Snippets plugin
if(interface_exists('\PGMB\Placeholders\VariableInterface')){
class PGMB_WC_Variation_Prices implements \PGMB\Placeholders\VariableInterface{
private $product;
public function __construct($parent_id) {
if(!function_exists('wc_get_product')){ return; }
$this->product = wc_get_product($parent_id);
}
public function variables() {
if(!$this->product){ return []; }
return [
'%wc_variation_min_current_price%' => $this->product->get_variation_price(),
'%wc_variation_max_current_price%' => $this->product->get_variation_price('max'),
'%wc_variation_min_regular_price%' => $this->product->get_variation_regular_price(),
'%wc_variation_max_regular_price%' => $this->product->get_variation_regular_price('max'),
'%wc_variation_min_sale_price%' => $this->product->get_variation_sale_price(),
'%wc_variation_max_sale_price%' => $this->product->get_variation_sale_price('max'),
];
}
}
}
add_action('mbp_placeholder_decorators', function($decorators, $parent_post_id, $location){
if(class_exists('PGMB_WC_Variation_Prices')){
$variation_prices = new PGMB_WC_Variation_Prices($parent_post_id);
$decorators[] = $variation_prices;
}
return $decorators;
}
,10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment