Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Losing decimals in Qty column on PayPal page #4

Open
ghost opened this issue Jan 11, 2017 · 3 comments
Open

Losing decimals in Qty column on PayPal page #4

ghost opened this issue Jan 11, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Jan 11, 2017

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
Copy link
Author

ghost commented Jan 11, 2017

Found this which has a potentially useful comment below: woocommerce/woocommerce@10f10d1

@ghost
Copy link
Author

ghost commented Jan 11, 2017

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
Copy link

cannap commented Jan 18, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant