wpgenie

add fee for auction winner on checkout

May 4th, 2017 (edited)
2,566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. add_action( 'woocommerce_cart_calculate_fees', 'custom_auction_charge' );  
  2.  
  3. function custom_auction_charge( $cart_object ) {
  4.  
  5.     $spfee = 0.00; // initialize special fee, absolute amount
  6.     $spfeeperprod = 0.20; //special fee per product, percentage
  7.  
  8.     foreach ( $cart_object->cart_contents as $key => $value ) {
  9.  
  10.         $proid = $value['product_id']; //get the product id from cart
  11.         $quantiy = $value['quantity']; //get quantity from cart
  12.         $product = wc_get_product( $proid  ); //
  13.  
  14.         if('auction' == $product->get_type()) :
  15.  
  16.             if ($product->get_auction_closed() == 2){
  17.                     $itmprice = $value['data']->get_auction_current_bid(); //get product price 
  18.                     $spfee = $spfee + $itmprice * $quantiy * $spfeeperprod;
  19.             }      
  20.         endif;  
  21.  
  22.     }
  23.     if($spfee > 0 ) {
  24.         WC()->cart->add_fee( 'Buyers Premium', $spfee, true, 'standard' );
  25.     }
  26. }
  27.  
  28.  
  29. add_filter( 'wsa_stripe_automatic_charge_order', 'auctions_for_woocommerce_stripe_automatic_charge_addfee', 10, 2 );
  30. function auctions_for_woocommerce_stripe_automatic_charge_addfee( $order, $auctions_id ) {
  31.  
  32.     $fee_per_auction    = 0.20; //special fee per product, percentage
  33.     $spfee              = $spfee = 0.00; // initialize special fee, absolute amount
  34.    
  35.     $product         = wc_get_product( $auctions_id );
  36.     $item_price      = $product->get_auction_current_bid();
  37.     $global_fee      = $spfee + $item_price * $fee_per_auction;
  38.     $item_fee        = new WC_Order_Item_Fee();
  39.  
  40.     $item_fee->set_name( 'BPA' );
  41.     $item_fee->set_amount( $global_fee );
  42.     $item_fee->set_total( $global_fee );
  43.     $order->add_item( $item_fee );
  44.     $note = sprintf(
  45.                 __( 'WSA Stripe Auto Charge Log: This order includes a %s Buyers Premium fee.', 'woocommerce' ),
  46.                 $global_fee
  47.             );
  48.  
  49.     $order->add_order_note( $note, true ); // true if you want the customer to see order note about fees
  50.  
  51.     return $order;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment