Created
June 8, 2022 13:53
-
-
Save koen12344/1d2ff9f57d0b95625e8f1ce6172ac860 to your computer and use it in GitHub Desktop.
Add price variables for WooCommerce variations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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