/**
  * Create or update an order coupon
  *
  * @since 2.2
  * @param \WC_Order $order
  * @param array $coupon item data
  * @param string $action 'create' to add coupon or 'update' to update it
  * @throws WC_API_Exception invalid data, server error
  */
 protected function set_coupon($order, $coupon, $action)
 {
     // coupon amount must be positive float
     if (isset($coupon['amount']) && floatval($coupon['amount']) < 0) {
         throw new WC_API_Exception('woocommerce_invalid_coupon_total', __('Coupon discount total must be a positive amount', 'woocommerce'), 400);
     }
     if ('create' === $action) {
         // coupon code is required
         if (empty($coupon['code'])) {
             throw new WC_API_Exception('woocommerce_invalid_coupon_coupon', __('Coupon code is required', 'woocommerce'), 400);
         }
         $item = new WC_Order_Item_Coupon(array('code' => $coupon['code'], 'discount' => isset($coupon['amount']) ? floatval($coupon['amount']) : 0, 'discount_tax' => 0, 'order_id' => $order->get_id()));
         $coupon_id = $item->save();
         if (!$coupon_id) {
             throw new WC_API_Exception('woocommerce_cannot_create_order_coupon', __('Cannot create coupon, try again', 'woocommerce'), 500);
         }
     } else {
         $item = new WC_Order_Item_Coupon($coupon['id']);
         if (isset($coupon['code'])) {
             $item->set_code($coupon['code']);
         }
         if (isset($coupon['amount'])) {
             $item->set_discount(floatval($coupon['amount']));
         }
         $coupon_id = $item->save();
         if (!$coupon_id) {
             throw new WC_API_Exception('woocommerce_cannot_update_order_coupon', __('Cannot update coupon, try again', 'woocommerce'), 500);
         }
     }
 }
 /**
  * Hosted payment args.
  *
  * @param  WC_Order $order
  *
  * @return array
  */
 protected function get_hosted_payments_args($order)
 {
     $args = apply_filters('woocommerce_simplify_commerce_hosted_args', array('sc-key' => $this->public_key, 'amount' => $order->get_total() * 100, 'reference' => $order->get_id(), 'name' => esc_html(get_bloginfo('name', 'display')), 'description' => sprintf(__('Order #%s', 'woocommerce'), $order->get_order_number()), 'receipt' => 'false', 'color' => $this->modal_color, 'redirect-url' => WC()->api_request_url('WC_Gateway_Simplify_Commerce'), 'address' => $order->get_billing_address_1() . ' ' . $order->get_billing_address_2(), 'address-city' => $order->get_billing_city(), 'address-state' => $order->get_billing_state(), 'address-zip' => $order->get_billing_postcode(), 'address-country' => $order->get_billing_country(), 'operation' => 'create.token'), $order->get_id());
     return $args;
 }
Exemple #3
0
 /**
  * Test: get_id
  */
 function test_get_id()
 {
     $object = new WC_Order();
     $id = $object->save();
     $this->assertEquals($id, $object->get_id());
 }
 /**
  * Wrapper method to create/update order items.
  * When updating, the item ID provided is checked to ensure it is associated
  * with the order.
  *
  * @param WC_Order $order order
  * @param string $item_type
  * @param array $posted item provided in the request body
  * @throws WC_REST_Exception If item ID is not associated with order
  */
 protected function set_item($order, $item_type, $posted)
 {
     global $wpdb;
     if (!empty($posted['id'])) {
         $action = 'update';
     } else {
         $action = 'create';
     }
     $method = 'prepare_' . $item_type;
     // Verify provided line item ID is associated with order.
     if ('update' === $action) {
         $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d", absint($posted['id']), absint($order->get_id())));
         if (is_null($result)) {
             throw new WC_REST_Exception('woocommerce_rest_invalid_item_id', __('Order item ID provided is not associated with order.', 'woocommerce'), 400);
         }
     }
     // Prepare item data
     $item = $this->{$method}($posted, $action);
     // Save or add to order
     if ('create' === $action) {
         $order->add_item($item);
     } else {
         $item->save();
     }
 }
/**
 * 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();
}
 /**
  * Process a pre-order payment when the pre-order is released.
  *
  * @param WC_Order $order
  * @return WP_Error|null
  */
 public function process_pre_order_release_payment($order)
 {
     try {
         $order_items = $order->get_items();
         $order_item = array_shift($order_items);
         /* translators: 1: site name 2: product name 3: order number */
         $pre_order_name = sprintf(__('%1$s - Pre-order for "%2$s" (Order #%3$s)', 'woocommerce'), esc_html(get_bloginfo('name', 'display')), $order_item['name'], $order->get_order_number());
         $customer_id = get_post_meta($order->get_id(), '_simplify_customer_id', true);
         if (!$customer_id) {
             return new WP_Error('simplify_error', __('Customer not found.', 'woocommerce'));
         }
         // Charge the customer
         $payment = Simplify_Payment::createPayment(array('amount' => $order->get_total() * 100, 'customer' => $customer_id, 'description' => trim(substr($pre_order_name, 0, 1024)), 'currency' => strtoupper(get_woocommerce_currency()), 'reference' => $order->get_id()));
         if ('APPROVED' == $payment->paymentStatus) {
             // Payment complete
             $order->payment_complete($payment->id);
             // Add order note
             $order->add_order_note(sprintf(__('Simplify payment approved (ID: %1$s, Auth Code: %2$s)', 'woocommerce'), $payment->id, $payment->authCode));
         } else {
             return new WP_Error('simplify_payment_declined', __('Payment was declined - the customer need to try another card.', 'woocommerce'));
         }
     } catch (Exception $e) {
         $order_note = sprintf(__('Simplify Transaction Failed (%s)', 'woocommerce'), $e->getMessage());
         // Mark order as failed if not already set,
         // otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
         if ('failed' != $order->get_status()) {
             $order->update_status('failed', $order_note);
         } else {
             $order->add_order_note($order_note);
         }
     }
 }
/**
 * 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;
}
 /**
  * Read order items of a specific type from the database for this order.
  *
  * @param  WC_Order $order
  * @param  string $type
  * @return array
  */
 public function read_items($order, $type)
 {
     global $wpdb;
     $get_items_sql = $wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s ORDER BY order_item_id;", $order->get_id(), $type);
     $items = $wpdb->get_results($get_items_sql);
     if (!empty($items)) {
         $items = array_map(array('WC_Order_Factory', 'get_order_item'), array_combine(wp_list_pluck($items, 'order_item_id'), $items));
     } else {
         $items = array();
     }
     return $items;
 }
 /**
  * Save important data from the IPN to the order.
  * @param WC_Order $order
  * @param array $posted
  */
 protected function save_paypal_meta_data($order, $posted)
 {
     if (!empty($posted['payer_email'])) {
         update_post_meta($order->get_id(), 'Payer PayPal address', wc_clean($posted['payer_email']));
     }
     if (!empty($posted['first_name'])) {
         update_post_meta($order->get_id(), 'Payer first name', wc_clean($posted['first_name']));
     }
     if (!empty($posted['last_name'])) {
         update_post_meta($order->get_id(), 'Payer last name', wc_clean($posted['last_name']));
     }
     if (!empty($posted['payment_type'])) {
         update_post_meta($order->get_id(), 'Payment type', wc_clean($posted['payment_type']));
     }
     if (!empty($posted['txn_id'])) {
         update_post_meta($order->get_id(), '_transaction_id', wc_clean($posted['txn_id']));
     }
     if (!empty($posted['payment_status'])) {
         update_post_meta($order->get_id(), '_paypal_status', wc_clean($posted['payment_status']));
     }
 }
 /**
  * Add content to the WC emails.
  *
  * @param WC_Order $order
  * @param bool $sent_to_admin
  * @param bool $plain_text
  */
 public function email_instructions($order, $sent_to_admin, $plain_text = false)
 {
     if (!$sent_to_admin && 'bacs' === $order->get_payment_method() && $order->has_status('on-hold')) {
         if ($this->instructions) {
             echo wpautop(wptexturize($this->instructions)) . PHP_EOL;
         }
         $this->bank_details($order->get_id());
     }
 }
 /**
  * Wrapper method to create/update order items
  *
  * When updating, the item ID provided is checked to ensure it is associated
  * with the order.
  *
  * @since  2.5.0
  * @param  WC_Order $order order
  * @param  string $item_type
  * @param  array $item item provided in the request body
  * @param  string $action either 'create' or 'update'
  * @throws WC_CLI_Exception if item ID is not associated with order
  */
 protected function set_item($order, $item_type, $item, $action)
 {
     global $wpdb;
     $set_method = "set_{$item_type}";
     // verify provided line item ID is associated with order
     if ('update' === $action) {
         $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d", absint($item['id']), absint($order->get_id())));
         if (is_null($result)) {
             throw new WC_CLI_Exception('woocommerce_invalid_item_id', __('Order item ID provided is not associated with order', 'woocommerce'));
         }
     }
     $this->{$set_method}($order, $item, $action);
 }