Skip to content

Instantly share code, notes, and snippets.

@WhiteHatJoker
Created October 18, 2021 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WhiteHatJoker/c8f16cfb24c5cc649821b87425c2265e to your computer and use it in GitHub Desktop.
Save WhiteHatJoker/c8f16cfb24c5cc649821b87425c2265e to your computer and use it in GitHub Desktop.
Add fee for products from specific vendor in cart

Adding a fee for a specific vendor in cart

Add fee for products from specific vendor in cart

Installation

  1. Copy the repository code into your functions.php file replacing line 5 $fixed=30; with your desired fee and line 11 if ($values['data']->post->post_author == 40) { 40 with id of the vendor you would like to add a fee for. Keep in mind that the code in this repository adds one instance of fixed fee no matter how many products from the specific vendor is in the cart.
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
$fee = false;
$fixed = 30;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if ($values['data']->post->post_author == 40) {
$fee= true;
break;
}
}
if ($fee) {
$woocommerce->cart->add_fee( 'Tsar Nicoulai Shipping Fee', $fixed, true, '' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment