Adding DRS charge to your woocommerce products - no plugin required.

Add DRS Charge on specific products without plugin with woocommerce


Create a tag in your product to check for DRS in my case “DRS per 24 pack”, then add the following code to your functions file (we can help you with this if needed


function action_woocommerce_cart_calculate_fees
( $cart ) {
if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) return;

// Initialize
$quantity = 0;

// Loop though each cart item
foreach ( $cart->get_cart() as $cart_item ) {
// Compare
if ( has_term( array(‘DRS’), ‘product_cat’, $product->id ) ) {
#if ( has_term( ‘clothing’, ‘product_cat’, $cart_item[‘product_id’] ) ) { //or for one
$quantity += $cart_item[‘quantity’];
}
}
}

// Greater than
if ( $quantity > 0 ) {
// Add fee
$cart->add_fee( __( ‘DRS per 24 pack’, ‘woocommerce’ ), 3.60 * $quantity );
}
}
add_action( ‘woocommerce_cart_calculate_fees’, ‘action_woocommerce_cart_calculate_fees‘, 10, 1 );

The code checks if the items in the cart are in the given tag and applies the rate required. In my case they are sold in packs of 24 so 24*15 cent was applied.

Let me know if this was helpful.

Bryan Fogarty

SWD CTO and Senior Web Designer