get_var( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = '{$order_key}'" ); return $order_id; } /** * 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 * @return int insert id | bool false on failure */ function woocommerce_downloadable_file_permission( $download_id, $product_id, $order ) { global $wpdb; if ( $order->status == 'processing' && get_option( 'woocommerce_downloads_grant_access_after_payment' ) == 'no' ) return; $user_email = sanitize_email( $order->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 ); // Default value is NULL in the table schema $expiry = empty( $expiry ) ? null : absint( $expiry ); if ( $expiry ) $expiry = date_i18n( "Y-m-d", strtotime( 'NOW + ' . $expiry . ' DAY' ) ); $data = array( 'download_id' => $download_id, 'product_id' => $product_id, 'user_id' => absint( $order->user_id ), 'user_email' => $user_email, 'order_id' => $order->id, 'order_key' => $order->order_key, 'downloads_remaining' => $limit, 'access_granted' => current_time( 'mysql' ), 'download_count' => 0 ); $format = array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d' ); 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', apply_filters( 'woocommerce_downloadable_file_permission_data', $data ), apply_filters( 'woocommerce_downloadable_file_permission_format', $format ) ); return $result ? $wpdb->insert_id : false; } add_action('woocommerce_order_status_completed', 'woocommerce_downloadable_product_permissions'); add_action( 'woocommerce_order_status_processing', 'woocommerce_downloadable_product_permissions' ); /** * Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER * * @access public * @param int $order_id * @return void */ function woocommerce_downloadable_product_permissions( $order_id ) { if ( get_post_meta( $order_id, __( 'Download Permissions Granted', 'woocommerce' ), true ) == 1 ) return; // Only do this once $order = new WC_Order( $order_id ); if (sizeof($order->get_items())>0) foreach ($order->get_items() as $item) : if ($item['product_id']>0) : $_product = $order->get_product_from_item( $item ); if ( $_product && $_product->exists() && $_product->is_downloadable() ) : $product_id = ($item['variation_id']>0) ? $item['variation_id'] : $item['product_id']; $file_download_paths = apply_filters( 'woocommerce_file_download_paths', get_post_meta( $product_id, '_file_paths', true ), $product_id, $order_id, $item ); if ( ! empty( $file_download_paths ) ) { foreach ( $file_download_paths as $download_id => $file_path ) { woocommerce_downloadable_file_permission( $download_id, $product_id, $order ); } } endif; endif; endforeach; update_post_meta( $order_id, __( 'Download Permissions Granted', 'woocommerce' ), 1); } /** * Add a item to an order (for example a line item). * * @access public * @param int $order_id * @param array $data * @return mixed */ function woocommerce_add_order_item( $order_id, $item ) { global $wpdb; $order_id = absint( $order_id ); if ( ! $order_id ) return false; $defaults = array( 'order_item_name' => '', 'order_item_type' => 'line_item', ); $item = wp_parse_args( $item, $defaults ); $wpdb->insert( $wpdb->prefix . "woocommerce_order_items", array( 'order_item_name' => $item['order_item_name'], 'order_item_type' => $item['order_item_type'], 'order_id' => $order_id ), array( '%s', '%s', '%d' ) ); $item_id = absint( $wpdb->insert_id ); do_action( 'woocommerce_new_order_item', $item_id, $item, $order_id ); return $item_id; } /** * woocommerce_delete_order_item function. * * @access public * @param int $item_id * @return bool */ function woocommerce_delete_order_item( $item_id ) { global $wpdb; $item_id = absint( $item_id ); if ( ! $item_id ) return false; do_action( 'woocommerce_before_delete_order_item', $item_id ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d", $item_id ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $item_id ) ); do_action( 'woocommerce_delete_order_item', $item_id ); return true; } /** * WooCommerce Order Item Meta API - Update term meta * * @access public * @param mixed $item_id * @param mixed $meta_key * @param mixed $meta_value * @param string $prev_value (default: '') * @return bool */ function woocommerce_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) { return update_metadata( 'order_item', $item_id, $meta_key, $meta_value, $prev_value ); } /** * WooCommerce Order Item Meta API - Add term meta * * @access public * @param mixed $item_id * @param mixed $meta_key * @param mixed $meta_value * @param bool $unique (default: false) * @return bool */ function woocommerce_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ){ return add_metadata( 'order_item', $item_id, $meta_key, $meta_value, $unique ); } /** * WooCommerce Order Item Meta API - Delete term meta * * @access public * @param mixed $item_id * @param mixed $meta_key * @param string $meta_value (default: '') * @param bool $delete_all (default: false) * @return bool */ function woocommerce_delete_order_item_meta( $item_id, $meta_key, $meta_value = '', $delete_all = false ) { return delete_metadata( 'order_item', $item_id, $meta_key, $meta_value, $delete_all ); } /** * WooCommerce Order Item Meta API - Get term meta * * @access public * @param mixed $item_id * @param mixed $key * @param bool $single (default: true) * @return mixed */ function woocommerce_get_order_item_meta( $item_id, $key, $single = true ) { return get_metadata( 'order_item', $item_id, $key, $single ); } /** * woocommerce_cancel_unpaid_orders function. * * @access public * @return void */ function woocommerce_cancel_unpaid_orders() { global $wpdb; $held_duration = get_option( 'woocommerce_hold_stock_minutes' ); if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' ) return; $date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) ); $unpaid_orders = $wpdb->get_col( $wpdb->prepare( " SELECT posts.ID FROM {$wpdb->posts} AS posts LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) LEFT JOIN {$wpdb->terms} AS term USING( term_id ) WHERE posts.post_type = 'shop_order' AND posts.post_status = 'publish' AND tax.taxonomy = 'shop_order_status' AND term.slug = 'pending' AND posts.post_modified < %s ", $date ) ); if ( $unpaid_orders ) { foreach ( $unpaid_orders as $unpaid_order ) { $order = new WC_Order( $unpaid_order ); if ( apply_filters( 'woocommerce_cancel_unpaid_order', true, $order ) ) $order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) ); } } wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' ); wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' ); } add_action( 'woocommerce_cancel_unpaid_orders', 'woocommerce_cancel_unpaid_orders' ); /** * Return the count of processing orders. * * @access public * @return int */ function woocommerce_processing_order_count() { if ( false === ( $order_count = get_transient( 'woocommerce_processing_order_count' ) ) ) { $order_statuses = get_terms( 'shop_order_status' ); $order_count = false; foreach ( $order_statuses as $status ) { if( $status->slug === 'processing' ) { $order_count += $status->count; break; } } $order_count = apply_filters( 'woocommerce_admin_menu_count', intval( $order_count ) ); set_transient( 'woocommerce_processing_order_count', $order_count ); } return $order_count; }