greatwitenorth / quantities-and-units-for-woocommerce

Add unit support to items and allow decimal quantities in WooCommerce.
GNU General Public License v2.0
9 stars 18 forks source link

Losing decimals in Qty column on PayPal page #4

Open ghost opened 7 years ago

ghost commented 7 years ago

Hello,

Thank you for this plugin!

I'm having an issue where the quantity looks fine in the invoice but when you click the pay link in the email that redirects to PayPal it's it loses all the decimal places. The value shown to pay on the PayPal payment page is as if the values entered into the invoice had no decimal values at all. Hope that makes sense.

Any ideas?

Thanks!

ghost commented 7 years ago

Found this which has a potentially useful comment below: https://github.com/woocommerce/woocommerce/commit/10f10d1644f6a9a75f069e98638517ca5ad74f46

ghost commented 7 years ago

Actually, looky here it looks like they have actually added the filter mentioned: https://github.com/woocommerce/woocommerce/blob/master/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php#L337

cannap commented 6 years ago

i know is a bit older but here


add_filter('woocommerce_paypal_line_item',  function ($item, $item_name, $quantity, $amount) {

    if (is_float($quantity)) {
        $item['quantity'] = $quantity;
    }

    return $item;
},10,4);

add_filter( 'woocommerce_paypal_args', function ( $args) {

    {
        global $woocommerce;

        $cart_items = $woocommerce->cart->get_cart();

        foreach ( $args as $key => $arg ) {
            if ( preg_match( '/quantity_/', $key ) ) {
                $index = substr( $key, 9 );
                if ( is_int( $args[ 'quantity_' . $index ] ) ) {
                    foreach ( $cart_items as $item ) {
                        $productMetaNamePayPalKey = get_product_meta_key( $item );
                        $productMetaNamePayPal    = $productMetaNamePayPalKey;
                        if ( ! is_int( $item['quantity'] ) &&
                             $args[ 'item_name_' . $index ] == $productMetaNamePayPal &&
                             (int) $item['quantity'] == $args[ 'quantity_' . $index ] &&
                             (float) $item['data']->price == $args[ 'amount_' . $index ]
                        ) {
                            $quantity_suffix              = ''; //here you can add suffix when you want
                            $args[ 'quantity_' . $index ] = $item['quantity'];
                        }
                    }
                }
                if ( ! is_int( $args[ 'quantity_' . $index ] ) ) {
                    $args[ 'amount_' . $index ]    = round( $args[ 'amount_' . $index ] * $args[ 'quantity_' . $index ], 2 );
                    $args[ 'item_name_' . $index ] = get_new_product_name( $args, $index, isset( $quantity_suffix ) ? $quantity_suffix : '' );
                    $args[ 'quantity_' . $index ]  = 1;
                }
            }
        }

        return $args;
    }
}, 10, 2 );

function get_product_meta_key( $item ) {
    if ( ! empty( $item['variation_id'] ) ) {
        return $item['variation_id'];
    } else {
        return $item['product_id'];
    }
}

function get_new_product_name( $args, $index, $quantity_suffix = null ) {

    if ( ! empty( $quantity_suffix ) ) {
        return $args[ 'item_name_' . $index ] . ' x ' . $args[ 'quantity_' . $index ] . ' ' . $quantity_suffix;
    }

    return $args[ 'item_name_' . $index ] . ' x ' . $args[ 'quantity_' . $index ];
}

this solved the issue for me