/** * Process customer: updating or creating a new customer/saved CC * * @param WC_Order $order Order object * @param WC_Payment_Token $customer_token Payment Token * @param string $cart_token CC Token */ protected function process_customer($order, $customer_token = null, $cart_token = '') { // Are we saving a new payment method? if (is_user_logged_in() && isset($_POST['wc-simplify_commerce-new-payment-method']) && true === (bool) $_POST['wc-simplify_commerce-new-payment-method']) { $customer_info = array('email' => $order->get_billing_email(), 'name' => trim($order->get_formatted_billing_full_name())); $token = $this->save_token($customer_token, $cart_token, $customer_info); if (!is_null($token)) { $order->add_payment_token($token); } } }
/** * Process the pre-order. * * @param WC_Order $order * @param string $cart_token * @uses Simplify_ApiException * @uses Simplify_BadRequestException * @return array */ protected function process_pre_order($order, $cart_token = '') { if (WC_Pre_Orders_Order::order_requires_payment_tokenization($order->get_id())) { try { if ($order->get_total() * 100 < 50) { $error_msg = __('Sorry, the minimum allowed order total is 0.50 to use this payment method.', 'woocommerce'); throw new Simplify_ApiException($error_msg); } if (empty($cart_token)) { $error_msg = __('Please make sure your card details have been entered correctly and that your browser supports JavaScript.', 'woocommerce'); if ('yes' == $this->sandbox) { $error_msg .= ' ' . __('Developers: Please make sure that you\'re including jQuery and there are no JavaScript errors on the page.', 'woocommerce'); } throw new Simplify_ApiException($error_msg); } // Create customer $customer = Simplify_Customer::createCustomer(array('token' => $cart_token, 'email' => $order->get_billing_email(), 'name' => trim($order->get_formatted_billing_full_name()), 'reference' => $order->get_id())); if (is_object($customer) && '' != $customer->id) { $customer_id = wc_clean($customer->id); // Store the customer ID in the order update_post_meta($order->get_id(), '_simplify_customer_id', $customer_id); } else { $error_msg = __('Error creating user in Simplify Commerce.', 'woocommerce'); throw new Simplify_ApiException($error_msg); } // Reduce stock levels wc_reduce_stock_levels($order_id); // Remove cart WC()->cart->empty_cart(); // Is pre ordered! WC_Pre_Orders_Order::mark_order_as_pre_ordered($order); // Return thank you page redirect return array('result' => 'success', 'redirect' => $this->get_return_url($order)); } catch (Simplify_ApiException $e) { if ($e instanceof Simplify_BadRequestException && $e->hasFieldErrors() && $e->getFieldErrors()) { foreach ($e->getFieldErrors() as $error) { wc_add_notice($error->getFieldName() . ': "' . $error->getMessage() . '" (' . $error->getErrorCode() . ')', 'error'); } } else { wc_add_notice($e->getMessage(), 'error'); } return array('result' => 'fail', 'redirect' => ''); } } else { return parent::process_standard_payments($order, $cart_token); } }
/** * Test: get_billing_email */ function test_get_billing_email() { $object = new WC_Order(); $set_to = '*****@*****.**'; $object->set_billing_email($set_to); $this->assertEquals($set_to, $object->get_billing_email()); $set_to = 'not an email'; $this->setExpectedException('WC_Data_Exception'); $object->set_billing_email($set_to); $this->assertEquals('*****@*****.**', $object->get_billing_email()); }
/** * Grant downloadable product access to the file identified by $download_id. * * @param string $download_id file identifier * @param int|WC_Product $product * @param WC_Order $order the order * @param int $qty purchased * @return int|bool insert id or false on failure */ function wc_downloadable_file_permission($download_id, $product, $order, $qty = 1) { if (is_numeric($product)) { $product = wc_get_product($product); } $download = new WC_Customer_Download(); $download->set_download_id($download_id); $download->set_product_id($product->get_id()); $download->set_user_id($order->get_customer_id()); $download->set_order_id($order->get_id()); $download->set_user_email($order->get_billing_email()); $download->set_order_key($order->get_order_key()); $download->set_downloads_remaining(0 > $product->get_download_limit() ? '' : $product->get_download_limit() * $qty); $download->set_access_granted(current_time('timestamp')); $download->set_download_count(0); $expiry = $product->get_download_expiry(); if ($expiry > 0) { $order_completed_date = date_i18n("Y-m-d", $order->get_date_completed()); $download->set_access_expires(strtotime($order_completed_date . ' + ' . $expiry . ' DAY')); } return $download->save(); }
/** * Get a list of download IDs for a specific item from an order. * * @since 2.7.0 * @param WC_Order_Item $item * @param WC_Order $order * @return array */ public function get_download_ids($item, $order) { global $wpdb; return $wpdb->get_col($wpdb->prepare("SELECT download_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE user_email = %s AND order_key = %s AND product_id = %d ORDER BY permission_id", $order->get_billing_email(), $order->get_order_key(), $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id())); }
/** * Test: get_billing_email */ function test_get_billing_email() { $object = new WC_Order(); $set_to = '*****@*****.**'; $object->set_billing_email($set_to); $this->assertEquals($set_to, $object->get_billing_email()); $set_to = 'not an email'; $object->set_billing_email($set_to); $this->assertEquals('', $object->get_billing_email()); }
/** * Grant downloadable product access to the file identified by $download_id. * * @access public * @param string $download_id file identifier * @param int $product_id product identifier * @param WC_Order $order the order * @param int $qty purchased * @return int|bool insert id or false on failure */ function wc_downloadable_file_permission($download_id, $product_id, $order, $qty = 1) { global $wpdb; $user_email = sanitize_email($order->get_billing_email()); $limit = trim(get_post_meta($product_id, '_download_limit', true)); $expiry = trim(get_post_meta($product_id, '_download_expiry', true)); $limit = empty($limit) ? '' : absint($limit) * $qty; // Default value is NULL in the table schema $expiry = empty($expiry) ? null : absint($expiry); if ($expiry) { $order_completed_date = date_i18n("Y-m-d", strtotime($order->completed_date)); $expiry = date_i18n("Y-m-d", strtotime($order_completed_date . ' + ' . $expiry . ' DAY')); } $data = apply_filters('woocommerce_downloadable_file_permission_data', array('download_id' => $download_id, 'product_id' => $product_id, 'user_id' => absint($order->get_user_id()), 'user_email' => $user_email, 'order_id' => $order->get_id(), 'order_key' => $order->get_order_key(), 'downloads_remaining' => $limit, 'access_granted' => current_time('mysql'), 'download_count' => 0)); $format = apply_filters('woocommerce_downloadable_file_permission_format', array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d'), $data); if (!is_null($expiry)) { $data['access_expires'] = $expiry; $format[] = '%s'; } // Downloadable product - give access to the customer $result = $wpdb->insert($wpdb->prefix . 'woocommerce_downloadable_product_permissions', $data, $format); do_action('woocommerce_grant_product_download_access', $data); return $result ? $wpdb->insert_id : false; }
/** * Get the order data for the given ID. * * @since 2.5.0 * @param WC_Order $order The order instance * @return array */ protected function get_order_data($order) { $order_post = get_post($order->get_id()); $dp = wc_get_price_decimals(); $order_data = array('id' => $order->get_id(), 'order_number' => $order->get_order_number(), 'created_at' => $this->format_datetime($order_post->post_date_gmt), 'updated_at' => $this->format_datetime($order_post->post_modified_gmt), 'completed_at' => $this->format_datetime($order->completed_date, true), 'status' => $order->get_status(), 'currency' => $order->get_currency(), 'total' => wc_format_decimal($order->get_total(), $dp), 'subtotal' => wc_format_decimal($order->get_subtotal(), $dp), 'total_line_items_quantity' => $order->get_item_count(), 'total_tax' => wc_format_decimal($order->get_total_tax(), $dp), 'total_shipping' => wc_format_decimal($order->get_shipping_total(), $dp), 'cart_tax' => wc_format_decimal($order->get_cart_tax(), $dp), 'shipping_tax' => wc_format_decimal($order->get_shipping_tax(), $dp), 'total_discount' => wc_format_decimal($order->get_total_discount(), $dp), 'shipping_methods' => $order->get_shipping_method(), 'payment_details' => array('method_id' => $order->get_payment_method(), 'method_title' => $order->get_payment_method_title(), 'paid' => 0 < $order->get_date_paid()), 'billing_address' => array('first_name' => $order->get_billing_first_name(), 'last_name' => $order->get_billing_last_name(), 'company' => $order->get_billing_company(), 'address_1' => $order->get_billing_address_1(), 'address_2' => $order->get_billing_address_2(), 'city' => $order->get_billing_city(), 'state' => $order->get_billing_state(), 'postcode' => $order->get_billing_postcode(), 'country' => $order->get_billing_country(), 'email' => $order->get_billing_email(), 'phone' => $order->get_billing_phone()), 'shipping_address' => array('first_name' => $order->get_shipping_first_name(), 'last_name' => $order->get_shipping_last_name(), 'company' => $order->get_shipping_company(), 'address_1' => $order->get_shipping_address_1(), 'address_2' => $order->get_shipping_address_2(), 'city' => $order->get_shipping_city(), 'state' => $order->get_shipping_state(), 'postcode' => $order->get_shipping_postcode(), 'country' => $order->get_shipping_country()), 'note' => $order->get_customer_note(), 'customer_ip' => $order->get_customer_ip_address(), 'customer_user_agent' => $order->get_customer_user_agent(), 'customer_id' => $order->get_user_id(), 'view_order_url' => $order->get_view_order_url(), 'line_items' => array(), 'shipping_lines' => array(), 'tax_lines' => array(), 'fee_lines' => array(), 'coupon_lines' => array()); // add line items foreach ($order->get_items() as $item_id => $item) { $product = $order->get_product_from_item($item); $product_id = null; $product_sku = null; // Check if the product exists. if (is_object($product)) { $product_id = isset($product->variation_id) ? $product->variation_id : $product->id; $product_sku = $product->get_sku(); } $meta = new WC_Order_Item_Meta($item, $product); $item_meta = array(); foreach ($meta->get_formatted(null) as $meta_key => $formatted_meta) { $item_meta[] = array('key' => $meta_key, 'label' => $formatted_meta['label'], 'value' => $formatted_meta['value']); } $order_data['line_items'][] = array('id' => $item_id, 'subtotal' => wc_format_decimal($order->get_line_subtotal($item, false, false), $dp), 'subtotal_tax' => wc_format_decimal($item['line_subtotal_tax'], $dp), 'total' => wc_format_decimal($order->get_line_total($item, false, false), $dp), 'total_tax' => wc_format_decimal($item['line_tax'], $dp), 'price' => wc_format_decimal($order->get_item_total($item, false, false), $dp), 'quantity' => wc_stock_amount($item['qty']), 'tax_class' => !empty($item['tax_class']) ? $item['tax_class'] : null, 'name' => $item->get_name(), 'product_id' => $product_id, 'sku' => $product_sku, 'meta' => $item_meta); } // Add shipping. foreach ($order->get_shipping_methods() as $shipping_item_id => $shipping_item) { $order_data['shipping_lines'][] = array('id' => $shipping_item_id, 'method_id' => $shipping_item['method_id'], 'method_title' => $shipping_item['name'], 'total' => wc_format_decimal($shipping_item['cost'], $dp)); } // Add taxes. foreach ($order->get_tax_totals() as $tax_code => $tax) { $order_data['tax_lines'][] = array('id' => $tax->id, 'rate_id' => $tax->rate_id, 'code' => $tax_code, 'title' => $tax->label, 'total' => wc_format_decimal($tax->amount, $dp), 'compound' => (bool) $tax->is_compound); } // Add fees. foreach ($order->get_fees() as $fee_item_id => $fee_item) { $order_data['fee_lines'][] = array('id' => $fee_item_id, 'title' => $fee_item['name'], 'tax_class' => !empty($fee_item['tax_class']) ? $fee_item['tax_class'] : null, 'total' => wc_format_decimal($order->get_line_total($fee_item), $dp), 'total_tax' => wc_format_decimal($order->get_line_tax($fee_item), $dp)); } // Add coupons. foreach ($order->get_items('coupon') as $coupon_item_id => $coupon_item) { $order_data['coupon_lines'][] = array('id' => $coupon_item_id, 'code' => $coupon_item['name'], 'amount' => wc_format_decimal($coupon_item['discount_amount'], $dp)); } $order_data = apply_filters('woocommerce_cli_order_data', $order_data); return $this->flatten_array($order_data); }