Conditions | 52 |
Paths | > 20000 |
Total Lines | 212 |
Code Lines | 129 |
Lines | 14 |
Ratio | 6.6 % |
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 if ( !defined( 'ABSPATH' ) ) exit; |
||
32 | public static function process_checkout($paymentMethod='paypal', $order_id = 0, $customer_id = 0, $customer_billing_address_id = 0, $customer_shipping_address_id = 0) { |
||
33 | |||
34 | global $wpdb, $wpshop, $wpshop_cart; |
||
35 | $wps_message = new wps_message_ctr(); |
||
36 | $shipping_address_option = get_option('wpshop_shipping_address_choice'); |
||
37 | |||
38 | if (is_user_logged_in()) : |
||
39 | $user_id = get_current_user_id(); |
||
40 | |||
41 | if ( $customer_id != 0 ) { |
||
42 | $user_id = $customer_id; |
||
43 | } |
||
44 | |||
45 | // If the order is already created in the db |
||
46 | if(!empty($order_id) && is_numeric($order_id)) { |
||
47 | $order = get_post_meta($order_id, '_order_postmeta', true); |
||
48 | |||
49 | if(!empty($order)) { |
||
50 | if($order['customer_id'] == $user_id) { |
||
51 | $order['payment_method'] = $paymentMethod; |
||
52 | $_SESSION['order_id'] = (int) $order_id; |
||
53 | // Store cart in session |
||
54 | //wpshop_cart::store_cart_in_session($order); |
||
55 | // Add a payment |
||
56 | $order['order_payment']['received'][] = array( 'method' => $paymentMethod, 'waited_amount' => $order['order_amount_to_pay_now'], 'status' => 'waiting_payment', 'author' => get_current_user_id() ); |
||
57 | |||
58 | // On enregistre la commande |
||
59 | update_post_meta($order_id, '_order_postmeta', $order); |
||
60 | update_post_meta($order_id, '_wpshop_order_customer_id', $user_id); |
||
61 | } |
||
62 | else $wpshop->add_error(__('You don\'t own the order', 'wpshop')); |
||
63 | } |
||
64 | else $wpshop->add_error(__('The order doesn\'t exist.', 'wpshop')); |
||
65 | } |
||
66 | else{ |
||
67 | $order_data = array( |
||
68 | 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ORDER, |
||
69 | 'post_title' => sprintf(__('Order - %s','wpshop'), mysql2date('d M Y\, H:i:s', current_time('mysql', 0), true)), |
||
70 | 'post_status' => 'publish', |
||
71 | 'post_excerpt' => !empty($_POST['wps-customer-comment']) ? sanitize_text_field( $_POST['wps-customer-comment'] ) : '', |
||
72 | 'post_author' => $user_id, |
||
73 | 'comment_status' => 'closed' |
||
74 | ); |
||
75 | |||
76 | // Cart items |
||
77 | $order_items = array(); |
||
78 | $order_tva = array(); |
||
79 | |||
80 | //$cart = (array)$wpshop_cart->cart; |
||
81 | if ( !empty($_SESSION['cart']) && !empty( $_SESSION['cart']['shipping_method']) ) { |
||
82 | $_SESSION['cart']['shipping_method'] = __('Standard shipping method', 'wpshop'); |
||
83 | } |
||
84 | $cart = (array)$_SESSION['cart']; |
||
85 | |||
86 | $download_codes = array(); |
||
87 | |||
88 | // Nouvelle commande |
||
89 | $order_id = wp_insert_post($order_data); |
||
90 | $_SESSION['order_id'] = $order_id; |
||
91 | |||
92 | // Cr�ation des codes de t�l�chargement si il y a des produits t�l�chargeable dans le panier |
||
93 | if ( !empty( $cart['order_items'] ) ) { |
||
94 | foreach($cart['order_items'] as $c) { |
||
95 | $product_id = $c['item_id']; |
||
96 | $product = null; |
||
97 | View Code Duplication | if( isset( $c['item_meta']['variations'] ) ) { |
|
98 | foreach ( $c['item_meta']['variations'] as $variation_id => $variation ) { |
||
99 | if( isset( $variation['item_meta']['is_downloadable_'] ) ) { |
||
100 | $product_id = $c['item_id'] . '__' . $variation_id; |
||
101 | $product = wpshop_products::get_product_data( $product_id ); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | if( !isset( $product ) ) { |
||
106 | $product = wpshop_products::get_product_data( $c['item_id'] ); |
||
107 | $product_id = $c['item_id']; |
||
108 | /** Check if it's a variation and check the parent product **/ |
||
109 | if ( get_post_type( $c['item_id'] ) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) { |
||
110 | $parent_def = wpshop_products::get_parent_variation( $c['item_id'] ); |
||
111 | if ( !empty($parent_def) && !empty($parent_def['parent_post_meta']) && !empty($parent_def['parent_post_meta']['is_downloadable_']) ) { |
||
112 | $product['is_downloadable_'] = $parent_def['parent_post_meta']['is_downloadable_']; |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | if(!empty($product['is_downloadable_'])) { |
||
117 | $download_codes[$product_id] = array('item_id' => $product_id, 'download_code' => uniqid('', true)); |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | if(!empty($download_codes)) update_user_meta($user_id, '_order_download_codes_'.$order_id, $download_codes); |
||
122 | |||
123 | // Informations de commande � stocker |
||
124 | $currency = wpshop_tools::wpshop_get_currency(true); |
||
125 | $order = array_merge(array( |
||
126 | 'order_key' => NULL, |
||
127 | 'customer_id' => $user_id, |
||
128 | 'order_status' => 'awaiting_payment', |
||
129 | 'order_date' => current_time('mysql', 0), |
||
130 | 'order_shipping_date' => null, |
||
131 | 'order_invoice_ref' => '', |
||
132 | 'order_currency' => $currency, |
||
133 | 'order_payment' => array( |
||
134 | 'customer_choice' => array('method' => $paymentMethod), |
||
135 | 'received' => array('0' => array('method' => $paymentMethod, 'waited_amount' => $cart['order_amount_to_pay_now'], 'status' => 'waiting_payment', 'author' => $user_id)), |
||
136 | 'shipping_method' => ( ( !empty( $_SESSION['shipping_method']) ) ? wpshop_tools::varSanitizer( $_SESSION['shipping_method']) : __('Standard shipping method', 'wpshop') ) |
||
137 | ), |
||
138 | ), $cart); |
||
139 | |||
140 | // Si c'est un devis |
||
141 | if ( $paymentMethod == 'quotation' ) { |
||
142 | $order['order_temporary_key'] = wpshop_orders::get_new_pre_order_reference(); |
||
143 | } |
||
144 | else { |
||
145 | $order['order_key'] = wpshop_orders::get_new_order_reference(); |
||
146 | } |
||
147 | |||
148 | //Round final amount |
||
149 | $order['order_grand_total'] = number_format( round($order['order_grand_total'], 2), 2, '.', ''); |
||
150 | $order['order_total_ttc'] = number_format( round($order['order_total_ttc'], 2), 2, '.', ''); |
||
151 | $order['order_amount_to_pay_now'] = number_format( round($order['order_amount_to_pay_now'], 2), 2, '.', ''); |
||
152 | |||
153 | /** On enregistre la commande */ |
||
154 | update_post_meta($order_id, '_order_postmeta', $order); |
||
155 | update_post_meta($order_id, '_wpshop_order_customer_id', $order['customer_id']); |
||
156 | update_post_meta($order_id, '_wpshop_order_shipping_date', $order['order_shipping_date']); |
||
157 | update_post_meta($order_id, '_wpshop_order_status', $order['order_status']); |
||
158 | |||
159 | |||
160 | do_action( 'wps_order_extra_save', $order_id ); |
||
161 | |||
162 | //Add an action to extra actions on order save |
||
163 | // @TODO : REQUEST |
||
164 | $args = array( 'order_id' => $order_id, 'posted_data' => $_REQUEST ); |
||
165 | wpshop_tools::create_custom_hook( 'wps_order_extra_save_action', $args ); |
||
166 | |||
167 | /** Set custmer information for the order */ |
||
168 | $shipping_address = ( !empty($shipping_address_option) && !empty($shipping_address_option['activate']) ) ? ( ( !empty($_SESSION['shipping_address']) ) ? wpshop_tools::varSanitizer($_SESSION['shipping_address']) : $customer_shipping_address_id ) : ''; |
||
169 | $billing_address = ( !empty($_SESSION['billing_address']) ) ? wpshop_tools::varSanitizer($_SESSION['billing_address']) : $customer_billing_address_id; |
||
170 | |||
171 | |||
172 | if ( !empty( $billing_address) ) { |
||
173 | wpshop_orders::set_order_customer_addresses($user_id, $order_id, $shipping_address, $billing_address); |
||
174 | } |
||
175 | |||
176 | if ( !empty($_SESSION['shipping_address_to_save']) ) { |
||
177 | $order_infos_postmeta = get_post_meta($order_id, '_order_info', true); |
||
178 | $order_infos_postmeta['shipping']['address'] = $_SESSION['shipping_address_to_save']; |
||
179 | $order_infos_postmeta['shipping']['address_id'] = ''; |
||
180 | update_post_meta($order_id, '_order_info', $order_infos_postmeta); |
||
181 | unset( $_SESSION['shipping_address_to_save'] ); |
||
182 | } |
||
183 | |||
184 | |||
185 | /** Save Coupon use **/ |
||
186 | if ( !empty($_SESSION['cart']['coupon_id']) ) { |
||
187 | $wps_coupon_mdl = new wps_coupon_model(); |
||
188 | $wps_coupon_mdl->save_coupon_use( $_SESSION['cart']['coupon_id'] ); |
||
189 | } |
||
190 | |||
191 | /** Notify the customer as the case */ |
||
192 | $user_info = get_userdata($user_id); |
||
193 | $email = $user_info->user_email; |
||
194 | $first_name = $user_info->user_firstname ; |
||
195 | $last_name = $user_info->user_lastname; |
||
196 | |||
197 | // Envoie du message de confirmation de commande au client |
||
198 | $order_meta = get_post_meta( $order_id, '_order_postmeta', true); |
||
199 | |||
200 | $shipping_mode_option = get_option( 'wps_shipping_mode' ); |
||
201 | $shipping_method = ( !empty($order_meta['order_payment']['shipping_method']) && !empty($shipping_mode_option) && !empty($shipping_mode_option['modes']) && is_array($shipping_mode_option['modes']) && array_key_exists($order_meta['order_payment']['shipping_method'], $shipping_mode_option['modes'])) ? $shipping_mode_option['modes'][$order_meta['order_payment']['shipping_method']]['name'] : ( (!empty($order_meta['order_payment']['shipping_method']) ) ? $order_meta['order_payment']['shipping_method'] : '' ); |
||
202 | |||
203 | if ( !empty($order_meta) && !empty($order_meta['cart_type']) && $order_meta['cart_type'] == 'quotation' && empty($order_meta['order_key']) ) { |
||
204 | $wps_message->wpshop_prepared_email($email, 'WPSHOP_QUOTATION_CONFIRMATION_MESSAGE', array('order_id' => $order_id,'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'customer_email' => $email, 'order_date' => current_time('mysql', 0), 'order_content' => '', 'order_addresses' => '', 'order_customer_comments' => '', 'order_billing_address' => '', 'order_shipping_address' => '', 'order_shipping_method' => $shipping_method, 'order_personnal_informations' => '') ); |
||
205 | } |
||
206 | else { |
||
207 | $email_option = get_option( 'wpshop_emails' ); |
||
208 | if ( empty($email_option['send_confirmation_order_message']) ) { |
||
209 | $payment_method_option = get_option( 'wps_payment_mode' ); |
||
210 | $order_payment_method = ( !empty($payment_method_option) && !empty($payment_method_option['mode']) && !empty($order_meta['order_payment']['customer_choice']['method']) && !empty($payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']]) ) ? $payment_method_option['mode'][$order_meta['order_payment']['customer_choice']['method']]['name'] : $order_meta['order_payment']['customer_choice']['method']; |
||
211 | |||
212 | $wps_message->wpshop_prepared_email($email, 'WPSHOP_ORDER_CONFIRMATION_MESSAGE', array('order_id' => $order_id,'customer_first_name' => $first_name, 'customer_last_name' => $last_name, 'customer_email' => $email, 'order_key' => ( ( !empty($order_meta['order_key']) ) ? $order_meta['order_key'] : ''),'order_date' => current_time('mysql', 0), 'order_payment_method' => $order_payment_method, 'order_content' => '', 'order_addresses' => '', 'order_customer_comments' => '', 'order_billing_address' => '', 'order_shipping_address' => '', 'order_shipping_method' => $shipping_method, 'order_personnal_informations' => '' ) ); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | View Code Duplication | if ( empty($_SESSION['wps-pos-addon']) ) { |
|
217 | $email_option = get_option('wpshop_emails'); |
||
218 | if( empty($email_option) || ( !empty($email_option) && empty($email_option['send_confirmation_order_message']) ) ){ |
||
219 | self::send_order_email_to_administrator( $order_id, $user_info ); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | |||
224 | /** IF Order amount is 0, Finish the Order **/ |
||
225 | if ( $cart['order_amount_to_pay_now'] == 0 ) { |
||
226 | $order_meta = get_post_meta($order_id, '_order_postmeta', true); |
||
227 | $payment_status = 'completed'; |
||
228 | $params_array = array ( |
||
229 | 'method' =>'free', |
||
230 | 'waited_amount' => $order_meta['order_amount_to_pay_now'], |
||
231 | 'status' => 'payment_received', |
||
232 | 'author' => $order_meta['customer_id'], |
||
233 | 'payment_reference' => 'FREE_ORDER', |
||
234 | 'date' => current_time('mysql', 0), |
||
235 | 'received_amount' => $order_meta['order_amount_to_pay_now'] |
||
236 | ); |
||
237 | wpshop_payment::check_order_payment_total_amount($order_id, $params_array, $payment_status); |
||
238 | } |
||
239 | apply_filters( 'wpshop_finish_order_extra_actions', $order_id); |
||
240 | } |
||
241 | endif; |
||
242 | return $order_id; |
||
243 | } |
||
244 | |||
329 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.