Last active
September 21, 2019 23:14
-
-
Save ammist/22ffc776d9e4bb0f1d6dda9ae6e21c7d to your computer and use it in GitHub Desktop.
WooCommerce Fractional Quantities for PayPal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* --------------------------------- | |
* 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 |
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
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.