Add a surcharge to cart and checkout

function df_add_ticket_surcharge( $cart_object ) {

global $woocommerce;
$specialfeecat = 14; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 15; //special fee per product

foreach ( $cart_object->cart_contents as $key => $value ) {

$proid = $value[‘product_id’]; //get the product id from cart
$quantiy = $value[‘quantity’]; //get quantity from cart
$itmprice = $value[‘data’]->price; //get product price

$terms = get_the_terms( $proid, ‘product_cat’ ); //get taxonamy of the prducts
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
$catid = $term->term_id;
if( 14 == $catid ) {
$spfee += ($quantiy * 15);
}elseif( 12 == $catid ) {
$spfee += ($quantiy * 10);
}elseif( 87 == $catid ) {
$spfee += ($quantiy * 15);
}
}
endif;
}

if($spfee > 0 ) {

$woocommerce->cart->add_fee( ‘Shipping Cost’, $spfee, true, ‘standard’ );
}

}

add_action( ‘woocommerce_cart_calculate_fees’, ‘df_add_ticket_surcharge’ );

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s