1. Home
  2. Docs
  3. Topics
  4. Code Snippets

Code Snippets

This is a Developer-level document. If you are unfamiliar with code/templates and resolving potential conflicts, please engage with a developer who is familiar with FooSales and/or WooCommerce. These snippets are provided as a courtesy and are intended as a starting point to assist any specific customization needs. We are unable to provide support for customizations under our Terms of Service.

Capture Square Fees When Completing an Order

If you’re using a third-party export plugin to export your orders and you’re processing payments with Square, you might want to include the fees that Square deducts from the payment in your sales export. By default, FooSales doesn’t store any data obtained from Square other than the Square order ID that is generated upon successfully processing a payment. You can have FooSales store the Square processing fees as a hidden meta value for WooCommerce orders captured via FooSales where Square was used as the payment method.

Add the following code snippet to your theme’s functions.php file which will check to see if an order has a Square order ID when marked as completed. Next, it will use the Square API to retrieve the Square order from your account, add up all the payment fees involved for that order and save it as a meta field for the WooCommerce order.

add_action('woocommerce_order_status_completed', function($order_id) {
    $square_order_id = get_post_meta($order_id, "_foosales_square_order_id", true);
    
    if ( !empty($square_order_id) ) {
        //FooSales config
        $config = new FooSales_Config();

        //API helper methods
        require_once($config->helper_path.'foosales-api-helper.php');
        
        $order_result = fsfwc_get_square_order($square_order_id); // Uses the Square API to retrieve the order
        $square_fees = 0.0;
        
        if ( $order_result['status'] === 'success' ) {
            $order = $order_result['order'];
            
            if ( !empty($order['tenders']) ) {
                foreach ( $order['tenders'] as $tender ) {
                    $square_fees += ((float)($tender['processing_fee_money']['amount']) / 100.0); // The fee is returned as cents, so divide it by 100.0
                }
            }
        }
        
        update_post_meta($order_id, "_foosales_square_fee_amount", $square_fees);
    }
});
Was this article helpful to you? Yes 1 No