Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
Created January 18, 2018 18:43
Show Gist options
  • Select an option

  • Save mahdi-alavi/d2c89d0c62156688d0c2459640220c60 to your computer and use it in GitHub Desktop.

Select an option

Save mahdi-alavi/d2c89d0c62156688d0c2459640220c60 to your computer and use it in GitHub Desktop.

Revisions

  1. mahdi-alavi created this gist Jan 18, 2018.
    36 changes: 36 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    /* 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' );