Last active
January 2, 2019 17:40
-
-
Save lucasstark/65cdfd194e3f044051c27b16d7346f1d to your computer and use it in GitHub Desktop.
Add WooCommerce Product Addon full costs to Dynamically Adjusted Price
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
class WC_Dynamic_Pricing_Product_Addons_Compatibility { | |
private static $instance; | |
public static function register() { | |
if ( self::$instance == null ) { | |
self::$instance = new WC_Dynamic_Pricing_Product_Addons_Compatibility(); | |
} | |
} | |
private $add_on_prices = array(); | |
protected function __construct() { | |
add_filter( 'woocommerce_dynamic_pricing_get_price_to_discount', array( | |
$this, | |
'get_price_to_discount' | |
), 10, 3 ); | |
add_filter( 'wc_dynamic_pricing_apply_cart_item_adjustment', array( | |
$this, | |
'apply_cart_item_adjustment' | |
), 10, 2 ); | |
} | |
function get_price_to_discount( $price, $cart_item, $cart_item_key ) { | |
if ( isset( $cart_item['addons_price_before_calc'] ) ) { | |
$this->add_on_prices[ $cart_item_key ] = floatval( $price ) - floatval( $cart_item['addons_price_before_calc'] ); | |
$price = floatval( $cart_item['addons_price_before_calc'] ); | |
} | |
return $price; | |
} | |
function apply_cart_item_adjustment( $adjusted_price, $cart_item_key ) { | |
if ( isset($this->add_on_prices[ $cart_item_key ]) ) { | |
$adjusted_price += $this->add_on_prices[ $cart_item_key ]; | |
} | |
return $adjusted_price; | |
} | |
} | |
add_action( 'init', function () { | |
WC_Dynamic_Pricing_Product_Addons_Compatibility::register(); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment