Advertisement
CodeDropz

WooCommerce Uploads - Adjust Price Based on Total no of Uploads

Mar 16th, 2022
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. add_action('woocommerce_before_calculate_totals', 'dndmfu_wc_calculate_totals', 20, 1 );
  2.  
  3. function dndmfu_wc_calculate_totals( $cart_object ) {
  4.  
  5.     if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  6.         return;
  7.     }
  8.  
  9.     foreach ( $cart_object->get_cart() as $cart_item ) {
  10.        
  11.         // Get uploader name
  12.         $file_upload = 'dnd-wc-file-upload';
  13.  
  14.         // Price
  15.         $price = $cart_item['data']->get_price();
  16.  
  17.         // Count no. of files
  18.         if( isset( $cart_item[ $file_upload ] ) ) {
  19.            
  20.             // Get total files
  21.             $total_files = count( $cart_item[ $file_upload ] );
  22.  
  23.             // Multiple current price * total no of files
  24.             if( $total_files > 1 ){
  25.                 $price = ( $price * $total_files );
  26.             }
  27.  
  28.             // Set new price
  29.             $cart_item['data']->set_price( $price );
  30.         }
  31.  
  32.     }
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement