Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
Created January 18, 2018 18:43
Show Gist options
  • Save mahdi-alavi/d2c89d0c62156688d0c2459640220c60 to your computer and use it in GitHub Desktop.
Save mahdi-alavi/d2c89d0c62156688d0c2459640220c60 to your computer and use it in GitHub Desktop.
Add Custom Fee to Woocommerce Cart for Physical Products
/* Add Custom Fee to Woocommerce Cart for Physical Products.
=========================================================================== */
function itl_woocommerce_custom_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return;
}
$is_physical = false;
$products = $woocommerce->cart->get_cart();
foreach( $products as $product ) {
if ( $product['variation_id'] ) {
$product_id = $product['variation_id'];
} else {
$product_id = $product['product_id'];
}
$is_virtual = get_post_meta( $product_id, '_virtual', true );
$is_downloadable = get_post_meta( $product_id, '_downloadable', true );
if ( $is_virtual != 'yes' && $is_downloadable != 'yes' ) {
$is_physical = true;
break;
}
}
if ( $is_physical ) {
$cost = 100; // fee
$woocommerce->cart->add_fee( 'fee', $cost, true, '' );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'itl_woocommerce_custom_fee' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment