thesufi

Woocommerce float quantity issue with Paypal Standard

Mar 7th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. /*
  2.  * Hack to overcome Paypal Standard Payment quantity integer/float issue
  3.  */
  4. add_filter( 'woocommerce_paypal_line_item', 'cncf_woo_paypal_line_item_alter', 10, 4 );
  5.  
  6. function cncf_woo_paypal_line_item_alter( $item, $item_name, $quantity, $amount ) {
  7.     if ( is_float($quantity) ) {
  8.         $item['quantity'] = $quantity;
  9.     }
  10.     return $item;
  11. }
  12.  
  13. add_filter( 'woocommerce_paypal_args', 'cncf_woocommerce_paypal_args_alter', 10, 2 );
  14.  
  15. function cncf_woocommerce_paypal_args_alter( $args ) {
  16.     global $woocommerce;
  17.  
  18.     $cart_items = $woocommerce->cart->get_cart();
  19.  
  20.     foreach ( $args as $key => $arg ) {
  21.         if ( preg_match('/quantity_/', $key) ) {
  22.             $index = substr($key, 9);
  23.             if ( is_int($args['quantity_' . $index]) ) {
  24.                 foreach ($cart_items as $item) {
  25.                     $productMetaNamePayPalKey = cncf_get_product_meta_key($item);
  26.                     $productMetaNamePayPal = $productMetaNamePayPalKey;
  27.                     if ( !is_int($item['quantity']) &&
  28.                             $args['item_name_' . $index] == $productMetaNamePayPal &&
  29.                             (int) $item['quantity'] == $args['quantity_' . $index] &&
  30.                             (float) $item['data']->price == $args['amount_' . $index]
  31.                     ) {
  32.                         $quantity_suffix = '';
  33.                         $args['quantity_' . $index] = $item['quantity'];
  34.                     }
  35.                 }
  36.             }
  37.             if ( !is_int($args['quantity_' . $index]) ) {
  38.                 $args['amount_' . $index] = round($args['amount_' . $index] * $args['quantity_' . $index], 2);
  39.                 $args['item_name_' . $index] = cncf_get_product_name($args, $index, isset($quantity_suffix) ? $quantity_suffix : '');
  40.                 $args['quantity_' . $index] = 1;
  41.             }
  42.         }
  43.     }
  44.  
  45.     return $args;
  46. }
  47.  
  48. function cncf_get_product_meta_key( $item ) {
  49.     if ( !empty($item['variation_id']) ) {
  50.         return $item['variation_id'];
  51.     } else {
  52.         return $item['product_id'];
  53.     }
  54. }
  55.  
  56. function cncf_get_product_name ( $args, $index, $quantity_suffix = null ) {
  57.  
  58.     if ( !empty($quantity_suffix) ) {
  59.         return $args['item_name_' . $index] . ' x ' . $args['quantity_' . $index] . ' ' . $quantity_suffix;
  60.     }
  61.  
  62.     return $args['item_name_' . $index] . ' x ' . $args['quantity_' . $index];
  63. }
Add Comment
Please, Sign In to add comment