Skip to content

Instantly share code, notes, and snippets.

@CrispDev
Created December 27, 2018 19:52
Show Gist options
  • Save CrispDev/cc51f14b9e301f5954836c2974df05be to your computer and use it in GitHub Desktop.
Save CrispDev/cc51f14b9e301f5954836c2974df05be to your computer and use it in GitHub Desktop.
Woocommerce with Autoship Powered by QPilot Dynamic Checkout Price
<?php
/**
* Applies a custom checkout price to a speicific priduct with a frequency type and number
* otherwise returns original checkout price.
* @param float $checkout_price. The current discounted or not checkout price for this autoship product.
* @param int $product_id. The current product's id.
* @param string $frequency_type. The Autoship frequency type ( Months, Days, etc ).
* @param int $frequency. The frequency value.
*
* @return float The new calculated checkout price or the originally supplied
* vlaue.
*/
function xx_dynamic_autoship_checkout_price( $checkout_price, $product_id, $frequency_type, $frequency ){
// Simple example checks for if this is a special product id.
if ( 9 == $product_id ){
$product = wc_get_product( $product_id );
$original_price = $product->get_price();
// Generate a switch key.
$key = $frequency_type . '-' . $frequency;
$discounts = array(
'Months-3' => .1,
'Months-4' => .25,
'Months-5' => .4,
);
// Easy case just check if this is a discounted combo
// If so return new calculated checkout price.
// If not return original checkout price.
if ( isset( $discounts[$key] ) ) {
return (int)( ( $original_price - ($original_price * $discounts[$key]) ) * 100 ) / 100;
} else {
return $checkout_price;
}
}
return $checkout_price;
}
add_filter( 'autoship_checkout_price', 'xx_dynamic_autoship_checkout_price', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment