| Conditions | 22 |
| Paths | 498 |
| Total Lines | 143 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 340 | public function create_order( $data = array() ) { |
||
| 341 | if ( empty( $data ) ) { |
||
| 342 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
||
| 343 | } |
||
| 344 | |||
| 345 | $order = wc_create_order(); |
||
| 346 | |||
| 347 | if ( is_wp_error( $order ) ) { |
||
| 348 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 520 ) ); |
||
| 349 | } elseif ( false === $order ) { |
||
| 350 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 521 ) ); |
||
| 351 | } else { |
||
| 352 | $order_id = $order->id; |
||
| 353 | do_action( 'woocommerce_new_order', $order_id ); |
||
| 354 | } |
||
| 355 | |||
| 356 | // Store the line items to the new/resumed order |
||
| 357 | foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { |
||
| 358 | $item_id = $order->add_product( |
||
| 359 | $values['data'], |
||
| 360 | $values['quantity'], |
||
| 361 | array( |
||
| 362 | 'variation' => $values['variation'], |
||
| 363 | 'totals' => array( |
||
| 364 | 'subtotal' => $values['line_subtotal'], |
||
| 365 | 'subtotal_tax' => $values['line_subtotal_tax'], |
||
| 366 | 'total' => $values['line_total'], |
||
| 367 | 'tax' => $values['line_tax'], |
||
| 368 | 'tax_data' => $values['line_tax_data'] // Since 2.2 |
||
| 369 | ) |
||
| 370 | ) |
||
| 371 | ); |
||
| 372 | |||
| 373 | if ( ! $item_id ) { |
||
| 374 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 525 ) ); |
||
| 375 | } |
||
| 376 | |||
| 377 | // Allow plugins to add order item meta |
||
| 378 | do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key ); |
||
| 379 | } |
||
| 380 | |||
| 381 | // Store fees |
||
| 382 | foreach ( WC()->cart->get_fees() as $fee_key => $fee ) { |
||
| 383 | $item_id = $order->add_fee( $fee ); |
||
| 384 | |||
| 385 | if ( ! $item_id ) { |
||
| 386 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 526 ) ); |
||
| 387 | } |
||
| 388 | |||
| 389 | // Allow plugins to add order item meta to fees |
||
| 390 | do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key ); |
||
| 391 | } |
||
| 392 | |||
| 393 | // Store tax rows |
||
| 394 | foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $tax_rate_id ) { |
||
| 395 | if ( $tax_rate_id && ! $order->add_tax( $tax_rate_id, WC()->cart->get_tax_amount( $tax_rate_id ), WC()->cart->get_shipping_tax_amount( $tax_rate_id ) ) && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { |
||
| 396 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 528 ) ); |
||
| 397 | } |
||
| 398 | } |
||
| 399 | |||
| 400 | // Store coupons |
||
| 401 | foreach ( WC()->cart->get_coupons() as $code => $coupon ) { |
||
| 402 | if ( ! $order->add_coupon( $code, WC()->cart->get_coupon_discount_amount( $code ), WC()->cart->get_coupon_discount_tax_amount( $code ) ) ) { |
||
| 403 | throw new Exception( sprintf( __( 'Error %d: Unable to create order. Please try again.', 'woocommerce-gateway-stripe' ), 529 ) ); |
||
| 404 | } |
||
| 405 | } |
||
| 406 | |||
| 407 | // Billing address |
||
| 408 | $billing_address = array(); |
||
| 409 | if ( ! empty( $data['token']['card'] ) ) { |
||
| 410 | // Name from Stripe is a full name string. |
||
| 411 | $name = explode( ' ', $data['token']['card']['name'] ); |
||
| 412 | $lastname = array_pop( $name ); |
||
| 413 | $firstname = implode( ' ', $name ); |
||
| 414 | $billing_address['first_name'] = $firstname; |
||
| 415 | $billing_address['last_name'] = $lastname; |
||
| 416 | $billing_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 417 | $billing_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 418 | $billing_address['country'] = $data['token']['card']['country']; |
||
| 419 | $billing_address['address_1'] = $data['token']['card']['address_line1']; |
||
| 420 | $billing_address['address_2'] = $data['token']['card']['address_line2']; |
||
| 421 | $billing_address['city'] = $data['token']['card']['address_city']; |
||
| 422 | $billing_address['state'] = $data['token']['card']['address_state']; |
||
| 423 | $billing_address['postcode'] = $data['token']['card']['address_zip']; |
||
| 424 | } |
||
| 425 | |||
| 426 | // Shipping address. |
||
| 427 | $shipping_address = array(); |
||
| 428 | if ( WC()->cart->needs_shipping() && ! empty( $data['shippingContact'] ) ) { |
||
| 429 | $shipping_address['first_name'] = $data['shippingContact']['givenName']; |
||
| 430 | $shipping_address['last_name'] = $data['shippingContact']['familyName']; |
||
| 431 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 432 | $shipping_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 433 | $shipping_address['country'] = $data['shippingContact']['countryCode']; |
||
| 434 | $shipping_address['address_1'] = $data['shippingContact']['addressLines'][0]; |
||
| 435 | $shipping_address['address_2'] = $data['shippingContact']['addressLines'][1]; |
||
| 436 | $shipping_address['city'] = $data['shippingContact']['locality']; |
||
| 437 | $shipping_address['state'] = $data['shippingContact']['administrativeArea']; |
||
| 438 | $shipping_address['postcode'] = $data['shippingContact']['postalCode']; |
||
| 439 | } elseif ( ! empty( $data['shippingContact'] ) ) { |
||
| 440 | $shipping_address['first_name'] = $firstname; |
||
| 441 | $shipping_address['last_name'] = $lastname; |
||
| 442 | $shipping_address['email'] = $data['shippingContact']['emailAddress']; |
||
| 443 | $shipping_address['phone'] = $data['shippingContact']['phoneNumber']; |
||
| 444 | $shipping_address['country'] = $data['token']['card']['country']; |
||
| 445 | $shipping_address['address_1'] = $data['token']['card']['address_line1']; |
||
| 446 | $shipping_address['address_2'] = $data['token']['card']['address_line2']; |
||
| 447 | $shipping_address['city'] = $data['token']['card']['address_city']; |
||
| 448 | $shipping_address['state'] = $data['token']['card']['address_state']; |
||
| 449 | $shipping_address['postcode'] = $data['token']['card']['address_zip']; |
||
| 450 | } |
||
| 451 | |||
| 452 | $order->set_address( $billing_address, 'billing' ); |
||
| 453 | $order->set_address( $shipping_address, 'shipping' ); |
||
| 454 | |||
| 455 | WC()->shipping->calculate_shipping( WC()->cart->get_shipping_packages() ); |
||
| 456 | |||
| 457 | // Get the rate object selected by user. |
||
| 458 | foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
||
| 459 | foreach ( $package['rates'] as $key => $rate ) { |
||
| 460 | // Loop through user chosen shipping methods. |
||
| 461 | foreach( WC()->session->get( 'chosen_shipping_methods' ) as $method ) { |
||
| 462 | if ( $method === $key ) { |
||
| 463 | $order->add_shipping( $rate ); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 470 | $order->set_payment_method( $available_gateways['stripe'] ); |
||
| 471 | $order->set_total( WC()->cart->shipping_total, 'shipping' ); |
||
| 472 | $order->set_total( WC()->cart->get_cart_discount_total(), 'cart_discount' ); |
||
| 473 | $order->set_total( WC()->cart->get_cart_discount_tax_total(), 'cart_discount_tax' ); |
||
| 474 | $order->set_total( WC()->cart->tax_total, 'tax' ); |
||
| 475 | $order->set_total( WC()->cart->shipping_tax_total, 'shipping_tax' ); |
||
| 476 | $order->set_total( WC()->cart->total ); |
||
| 477 | |||
| 478 | // If we got here, the order was created without problems! |
||
| 479 | wc_transaction_query( 'commit' ); |
||
| 480 | |||
| 481 | return $order; |
||
| 482 | } |
||
| 483 | } |
||
| 487 |
This check marks private properties in classes that are never used. Those properties can be removed.