Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculate total cost for number of seats in woocommerce for woocommerce teams plugin
<?php
/**
* Shows the user a total price for a product as they increaset the seats
*
* @author SFNdesgn, Curtis McHale
* @starting source https://alexanderdejong.com/wordpress/wp-tutorial/add-total-price-calculation-woocommerce-product-page-ajax/
*
* @global $woocommerce Big global for woo
* @global $product WC product global
* @uses get_post_meta() returns metadata given key and post_id
* @uses absint() no negative numbers here
* @uses get_woocomerce_currency_symbol() returns the default Woo currency symbol
*/
function total_product_price() {
global $woocommerce, $product;
$seats = get_post_meta( absint( $product->get_id() ), '_wc_memberships_for_teams_min_member_count', true );
if ( isset( $seats ) && ! empty( $seats ) ){
$starting_price = $product->get_price() * absint( $seats );
} else {
$starting_price = $product->get_price();
}
// let's setup our divs
echo sprintf('<div id="product_total_price" style="font-size: 16px; font-weight: 200;">%s %s</div>',__('Total Price:','woocommerce'),'<span class="price">'. get_woocommerce_currency_symbol() .' ' .$starting_price.'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(0));
}
});
});
</script>
<?php
}
add_action( 'woocommerce_single_product_summary', 'cm_total_product_price' , 25 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment