Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Hack to overcome Paypal Standard Payment quantity integer/float issue
- */
- add_filter( 'woocommerce_paypal_line_item', 'cncf_woo_paypal_line_item_alter', 10, 4 );
- function cncf_woo_paypal_line_item_alter( $item, $item_name, $quantity, $amount ) {
- if ( is_float($quantity) ) {
- $item['quantity'] = $quantity;
- }
- return $item;
- }
- add_filter( 'woocommerce_paypal_args', 'cncf_woocommerce_paypal_args_alter', 10, 2 );
- function cncf_woocommerce_paypal_args_alter( $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 = cncf_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 = '';
- $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] = cncf_get_product_name($args, $index, isset($quantity_suffix) ? $quantity_suffix : '');
- $args['quantity_' . $index] = 1;
- }
- }
- }
- return $args;
- }
- function cncf_get_product_meta_key( $item ) {
- if ( !empty($item['variation_id']) ) {
- return $item['variation_id'];
- } else {
- return $item['product_id'];
- }
- }
- function cncf_get_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];
- }
Add Comment
Please, Sign In to add comment