Advertisement
CodeDropz

Add custom fees based on extension (WooCommerce - File Upload)

Oct 10th, 2023 (edited)
1,277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. add_action('woocommerce_cart_calculate_fees', 'codedropz_upload_fees');
  2.  
  3. function codedropz_upload_fees(){
  4.    
  5.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  6.         return;
  7.     }
  8.    
  9.     //@note: Define  your raster file extension here
  10.     $raster_ext = 'jpg|jpeg|png|tiff';
  11.    
  12.     //@note: Add your custom label for the fee 
  13.     $label = 'Additional Fee';
  14.    
  15.     //@note: list of product id
  16.     $apply_to = array(4907,4661,4452);
  17.    
  18.     //@note: Replace with your desired percentage
  19.     $percentage = 10;
  20.    
  21.     $products = WC()->cart->get_cart_contents();
  22.     $total_fee = 0;
  23.    
  24.     foreach( $products as $index => $product ) {
  25.         if( isset( $product['dnd-wc-file-upload'] ) && count( $product['dnd-wc-file-upload'] ) > 0 ) {
  26.             $raster_extension = explode( '|', $raster_ext );
  27.             $_product = $product['data'];
  28.             foreach( $product['dnd-wc-file-upload'] as $index => $file ){
  29.                 $price = ( $_product->is_on_sale() ? $_product->get_sale_price() : $_product->get_regular_price() );
  30.                 $file_type = wp_check_filetype( $file );
  31.                 if( in_array( $file_type['ext'], $raster_extension ) && in_array( $product['product_id'], $apply_to ) ){
  32.                     $total_fee += ( $percentage / 100 ) * $price;
  33.                 }
  34.                
  35.             }
  36.         }
  37.     }
  38.    
  39.     if( $total_fee > 0 ){
  40.         WC()->cart->add_fee( $label, $total_fee, false, '');
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement