Skip to content

Instantly share code, notes, and snippets.

@ammist
Last active September 21, 2019 23:14
Show Gist options
  • Save ammist/22ffc776d9e4bb0f1d6dda9ae6e21c7d to your computer and use it in GitHub Desktop.
Save ammist/22ffc776d9e4bb0f1d6dda9ae6e21c7d to your computer and use it in GitHub Desktop.
WooCommerce Fractional Quantities for PayPal
/* ---------------------------------
* Paypal Args. Paypal only accepts integer quantities, so we have to update the
* information that's sent from the order, but only for metals.
* --------------------------------- */
add_filter('woocommerce_paypal_args', 'tl_paypal_args', 100, 1);
function tl_paypal_args( $args ){
$counter = 1;
while(isset( $args[ 'quantity_' . $counter])){
$quantity = $args['quantity_' . $counter];
// fractional amount
if (strpos($quantity, '.')){
$args[ 'amount_' . $counter ] = number_format( $quantity * $args[ 'amount_' . $counter ], 2, '.', '' );
$args[ 'quantity_' . $counter ] = 1;
$args[ 'item_name_' . $counter ] .= " $quantity Troy Oz.";
}
$counter++;
}
return $args;
}
// this filter is applied in woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
@dwayneparton
Copy link

dwayneparton commented Sep 4, 2019

With Woocommerce 3.7.0 you need to update the line item to be a float. Because when it get's to the woocommerce_paypal_args a fractional qty will be 0. This is because in woocommerce_paypal_line_item woo parses qty (int) $quantity. I added the code below and you're snippet worked like a charm! Thanks.

add_filter('woocommerce_paypal_line_item', 'tl_paypal_line_item', 100, 5);
function tl_paypal_line_item( $item, $item_name, $quantity, $amount, $item_number ){
	if (strpos($quantity, '.')) {
		$item['quantity'] = (float) $quantity;
	}
	return $item;
}

@ammist
Copy link
Author

ammist commented Sep 21, 2019

Oh wow, that's really helpful, @dwayneparton! Thanks for the update, you've probably saved me a bunch of time when I finally upgrade this site to WooCommerce 3.7.x!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment