2013-08-09 16:11:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Order Functions
|
|
|
|
*
|
|
|
|
* Functions for order specific things.
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Core
|
|
|
|
* @package WooCommerce/Functions
|
|
|
|
* @version 2.1.0
|
|
|
|
*/
|
|
|
|
|
2014-09-20 19:10:04 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-05-30 15:03:11 +00:00
|
|
|
/**
|
|
|
|
* Get all order statuses
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_order_statuses() {
|
|
|
|
$order_statuses = array(
|
2014-10-29 11:15:00 +00:00
|
|
|
'wc-pending' => _x( 'Pending Payment', 'Order status', 'woocommerce' ),
|
2014-06-03 10:04:56 +00:00
|
|
|
'wc-processing' => _x( 'Processing', 'Order status', 'woocommerce' ),
|
2014-10-29 11:15:00 +00:00
|
|
|
'wc-on-hold' => _x( 'On Hold', 'Order status', 'woocommerce' ),
|
2014-06-03 10:04:56 +00:00
|
|
|
'wc-completed' => _x( 'Completed', 'Order status', 'woocommerce' ),
|
|
|
|
'wc-cancelled' => _x( 'Cancelled', 'Order status', 'woocommerce' ),
|
|
|
|
'wc-refunded' => _x( 'Refunded', 'Order status', 'woocommerce' ),
|
|
|
|
'wc-failed' => _x( 'Failed', 'Order status', 'woocommerce' ),
|
2014-05-30 15:03:11 +00:00
|
|
|
);
|
|
|
|
return apply_filters( 'wc_order_statuses', $order_statuses );
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:37:42 +00:00
|
|
|
/**
|
|
|
|
* See if a string is an order status.
|
|
|
|
* @param string $maybe_status Status, including any wc- prefix
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_is_order_status( $maybe_status ) {
|
|
|
|
$order_statuses = wc_get_order_statuses();
|
|
|
|
return isset( $order_statuses[ $maybe_status ] );
|
|
|
|
}
|
|
|
|
|
2014-06-13 13:21:10 +00:00
|
|
|
/**
|
|
|
|
* Main function for returning orders, uses the WC_Order_Factory class.
|
|
|
|
*
|
2014-09-12 13:01:49 +00:00
|
|
|
* @since 2.2
|
|
|
|
* @param mixed $the_order Post object or post ID of the order.
|
2014-06-13 13:21:10 +00:00
|
|
|
* @return WC_Order
|
|
|
|
*/
|
2014-08-15 12:56:43 +00:00
|
|
|
function wc_get_order( $the_order = false ) {
|
|
|
|
return WC()->order_factory->get_order( $the_order );
|
2014-06-13 13:21:10 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 16:43:21 +00:00
|
|
|
/**
|
2014-09-12 13:01:49 +00:00
|
|
|
* Get the nice name for an order status
|
|
|
|
*
|
|
|
|
* @since 2.2
|
2014-05-30 16:43:21 +00:00
|
|
|
* @param string $status
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_get_order_status_name( $status ) {
|
|
|
|
$statuses = wc_get_order_statuses();
|
2014-09-12 13:01:49 +00:00
|
|
|
$status = 'wc-' === substr( $status, 0, 3 ) ? substr( $status, 3 ) : $status;
|
|
|
|
$status = isset( $statuses[ 'wc-' . $status ] ) ? $statuses[ 'wc-' . $status ] : $status;
|
|
|
|
|
2014-10-29 11:15:00 +00:00
|
|
|
return $status;
|
2014-05-30 16:43:21 +00:00
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Finds an Order ID based on an order key.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param string $order_key An order key has generated by
|
|
|
|
* @return int The ID of an order, or 0 if the order could not be found
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_get_order_id_by_order_key( $order_key ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
// Faster than get_posts()
|
2014-09-16 09:45:28 +00:00
|
|
|
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = %s", $order_key ) );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
return $order_id;
|
|
|
|
}
|
|
|
|
|
2014-07-11 11:43:42 +00:00
|
|
|
/**
|
|
|
|
* Get all registered order types
|
|
|
|
*
|
|
|
|
* $for optionally define what you are getting order types for so only relevent types are returned.
|
|
|
|
*
|
|
|
|
* e.g. for 'order-meta-boxes', 'order-count'
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param string $for
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function wc_get_order_types( $for = '' ) {
|
|
|
|
global $wc_order_types;
|
|
|
|
|
|
|
|
if ( ! is_array( $wc_order_types ) ) {
|
|
|
|
$wc_order_types = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$order_types = array();
|
|
|
|
|
|
|
|
switch ( $for ) {
|
|
|
|
case 'order-count' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( ! $args['exclude_from_order_count'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'order-meta-boxes' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( $args['add_order_meta_boxes'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'view-orders' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( ! $args['exclude_from_order_views'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'reports' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( ! $args['exclude_from_order_reports'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-10-06 12:39:49 +00:00
|
|
|
case 'sales-reports' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( ! $args['exclude_from_order_sales_reports'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-05-12 04:01:23 +00:00
|
|
|
case 'order-webhooks' :
|
|
|
|
foreach ( $wc_order_types as $type => $args ) {
|
|
|
|
if ( ! $args['exclude_from_order_webhooks'] ) {
|
|
|
|
$order_types[] = $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2014-07-11 11:43:42 +00:00
|
|
|
default :
|
|
|
|
$order_types = array_keys( $wc_order_types );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return apply_filters( 'wc_order_types', $order_types, $for );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an order type by post type name
|
|
|
|
* @param string post type name
|
|
|
|
* @return bool|array of datails about the order type
|
|
|
|
*/
|
|
|
|
function wc_get_order_type( $type ) {
|
|
|
|
global $wc_order_types;
|
|
|
|
|
|
|
|
if ( isset( $wc_order_types[ $type ] ) ) {
|
|
|
|
return $wc_order_types[ $type ];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-22 13:30:42 +00:00
|
|
|
* Register order type. Do not use before init.
|
2014-07-11 11:43:42 +00:00
|
|
|
*
|
2014-07-11 18:39:20 +00:00
|
|
|
* Wrapper for register post type, as well as a method of telling WC which
|
2014-07-11 11:43:42 +00:00
|
|
|
* post types are types of orders, and having them treated as such.
|
|
|
|
*
|
|
|
|
* $args are passed to register_post_type, but there are a few specific to this function:
|
2014-07-11 18:39:20 +00:00
|
|
|
* - exclude_from_orders_screen (bool) Whether or not this order type also get shown in the main
|
2014-07-11 11:43:42 +00:00
|
|
|
* orders screen.
|
|
|
|
* - add_order_meta_boxes (bool) Whether or not the order type gets shop_order meta boxes.
|
|
|
|
* - exclude_from_order_count (bool) Whether or not this order type is excluded from counts.
|
|
|
|
* - exclude_from_order_views (bool) Whether or not this order type is visible by customers when
|
|
|
|
* viewing orders e.g. on the my account page.
|
|
|
|
* - exclude_from_order_reports (bool) Whether or not to exclude this type from core reports.
|
2014-10-06 12:39:49 +00:00
|
|
|
* - exclude_from_order_sales_reports (bool) Whether or not to exclude this type from core sales reports.
|
2014-07-11 11:43:42 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @see register_post_type for $args used in that function
|
|
|
|
* @param string $type Post type. (max. 20 characters, can not contain capital letters or spaces)
|
|
|
|
* @param array $args An array of arguments.
|
|
|
|
* @return bool Success or failure
|
|
|
|
*/
|
|
|
|
function wc_register_order_type( $type, $args = array() ) {
|
|
|
|
if ( post_type_exists( $type ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wc_order_types;
|
|
|
|
|
|
|
|
if ( ! is_array( $wc_order_types ) ) {
|
|
|
|
$wc_order_types = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register as a post type
|
|
|
|
if ( is_wp_error( register_post_type( $type, $args ) ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register for WC usage
|
|
|
|
$order_type_args = array(
|
2014-10-06 12:39:49 +00:00
|
|
|
'exclude_from_orders_screen' => false,
|
|
|
|
'add_order_meta_boxes' => true,
|
|
|
|
'exclude_from_order_count' => false,
|
|
|
|
'exclude_from_order_views' => false,
|
2015-05-12 04:01:23 +00:00
|
|
|
'exclude_from_order_webhooks' => false,
|
2014-10-06 12:39:49 +00:00
|
|
|
'exclude_from_order_reports' => false,
|
|
|
|
'exclude_from_order_sales_reports' => false,
|
|
|
|
'class_name' => 'WC_Order'
|
2014-07-11 11:43:42 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$args = array_intersect_key( $args, $order_type_args );
|
|
|
|
$args = wp_parse_args( $args, $order_type_args );
|
|
|
|
$wc_order_types[ $type ] = $args;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2015-02-17 14:03:42 +00:00
|
|
|
* @param int $qty purchased
|
2013-11-29 18:50:31 +00:00
|
|
|
* @return int|bool insert id or false on failure
|
2013-08-09 16:11:15 +00:00
|
|
|
*/
|
2015-02-17 14:03:42 +00:00
|
|
|
function wc_downloadable_file_permission( $download_id, $product_id, $order, $qty = 1 ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$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 ) );
|
|
|
|
|
2015-02-17 14:03:42 +00:00
|
|
|
$limit = empty( $limit ) ? '' : absint( $limit ) * $qty;
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
// Default value is NULL in the table schema
|
|
|
|
$expiry = empty( $expiry ) ? null : absint( $expiry );
|
|
|
|
|
2013-12-17 05:07:19 +00:00
|
|
|
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' ) );
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-10 11:26:31 +00:00
|
|
|
$data = apply_filters( 'woocommerce_downloadable_file_permission_data', array(
|
|
|
|
'download_id' => $download_id,
|
2013-08-09 16:11:15 +00:00
|
|
|
'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
|
2013-09-10 11:26:31 +00:00
|
|
|
));
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-10 11:26:31 +00:00
|
|
|
$format = apply_filters( 'woocommerce_downloadable_file_permission_format', array(
|
|
|
|
'%s',
|
2013-08-09 16:11:15 +00:00
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%s',
|
|
|
|
'%d'
|
2013-09-24 08:10:46 +00:00
|
|
|
), $data);
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-10 11:26:31 +00:00
|
|
|
if ( ! is_null( $expiry ) ) {
|
|
|
|
$data['access_expires'] = $expiry;
|
|
|
|
$format[] = '%s';
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
// Downloadable product - give access to the customer
|
|
|
|
$result = $wpdb->insert( $wpdb->prefix . 'woocommerce_downloadable_product_permissions',
|
2013-09-10 11:26:31 +00:00
|
|
|
$data,
|
2013-09-10 14:08:20 +00:00
|
|
|
$format
|
2013-09-10 11:26:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
do_action( 'woocommerce_grant_product_download_access', $data );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-10 11:26:31 +00:00
|
|
|
return $result ? $wpdb->insert_id : false;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
2013-12-16 23:27:57 +00:00
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
|
|
|
* Order Status completed - GIVE DOWNLOADABLE PRODUCT ACCESS TO CUSTOMER
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $order_id
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_downloadable_product_permissions( $order_id ) {
|
2014-02-17 10:50:36 +00:00
|
|
|
if ( get_post_meta( $order_id, '_download_permissions_granted', true ) == 1 ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
return; // Only do this once
|
2014-02-17 10:50:36 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2014-08-15 12:29:21 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2015-08-12 16:21:07 +00:00
|
|
|
if ( $order && $order->has_status( 'processing' ) && get_option( 'woocommerce_downloads_grant_access_after_payment' ) == 'no' ) {
|
2014-02-17 10:49:55 +00:00
|
|
|
return;
|
2014-02-17 10:50:36 +00:00
|
|
|
}
|
2014-02-17 10:49:55 +00:00
|
|
|
|
2013-09-20 16:01:09 +00:00
|
|
|
if ( sizeof( $order->get_items() ) > 0 ) {
|
|
|
|
foreach ( $order->get_items() as $item ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$_product = $order->get_product_from_item( $item );
|
|
|
|
|
2013-09-20 16:01:09 +00:00
|
|
|
if ( $_product && $_product->exists() && $_product->is_downloadable() ) {
|
2013-12-16 23:27:57 +00:00
|
|
|
$downloads = $_product->get_files();
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-12-16 23:27:57 +00:00
|
|
|
foreach ( array_keys( $downloads ) as $download_id ) {
|
2015-02-17 14:03:42 +00:00
|
|
|
wc_downloadable_file_permission( $download_id, $item['variation_id'] > 0 ? $item['variation_id'] : $item['product_id'], $order, $item['qty'] );
|
2013-12-16 23:27:57 +00:00
|
|
|
}
|
2013-09-20 16:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2013-09-20 16:01:09 +00:00
|
|
|
update_post_meta( $order_id, '_download_permissions_granted', 1 );
|
2013-09-10 11:26:31 +00:00
|
|
|
|
|
|
|
do_action( 'woocommerce_grant_product_download_permissions', $order_id );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
2014-02-17 10:49:55 +00:00
|
|
|
add_action( 'woocommerce_order_status_completed', 'wc_downloadable_product_permissions' );
|
|
|
|
add_action( 'woocommerce_order_status_processing', 'wc_downloadable_product_permissions' );
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a item to an order (for example a line item).
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $order_id
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_add_order_item( $order_id, $item ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-28 03:21:16 +00:00
|
|
|
/**
|
|
|
|
* Update an item for an order
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param int $item_id
|
|
|
|
* @param array $args either `order_item_type` or `order_item_name`
|
|
|
|
* @return bool true if successfully updated, false otherwise
|
|
|
|
*/
|
|
|
|
function wc_update_order_item( $item_id, $args ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$update = $wpdb->update( $wpdb->prefix . 'woocommerce_order_items', $args, array( 'order_item_id' => $item_id ) );
|
|
|
|
|
|
|
|
if ( false === $update ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_action( 'woocommerce_update_order_item', $item_id, $args );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-09 16:11:15 +00:00
|
|
|
/**
|
2013-11-25 13:54:52 +00:00
|
|
|
* Delete an item from the order it belongs to based on item id
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param int $item_id
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_delete_order_item( $item_id ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
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
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_update_order_item_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
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
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique = false ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
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
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_delete_order_item_meta( $item_id, $meta_key, $meta_value = '', $delete_all = false ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
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
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_get_order_item_meta( $item_id, $key, $single = true ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
return get_metadata( 'order_item', $item_id, $key, $single );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-25 13:54:52 +00:00
|
|
|
* Cancel all unpaid orders after held duration to prevent stock lock for those products
|
2013-08-09 16:11:15 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_cancel_unpaid_orders() {
|
2013-08-09 16:11:15 +00:00
|
|
|
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
|
2014-08-12 15:14:38 +00:00
|
|
|
WHERE posts.post_type IN ('" . implode( "','", wc_get_order_types() ) . "')
|
2014-06-03 10:29:01 +00:00
|
|
|
AND posts.post_status = 'wc-pending'
|
2013-08-09 16:11:15 +00:00
|
|
|
AND posts.post_modified < %s
|
|
|
|
", $date ) );
|
|
|
|
|
|
|
|
if ( $unpaid_orders ) {
|
|
|
|
foreach ( $unpaid_orders as $unpaid_order ) {
|
2014-08-15 12:29:21 +00:00
|
|
|
$order = wc_get_order( $unpaid_order );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
2015-04-01 13:21:07 +00:00
|
|
|
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) ) {
|
2013-08-09 16:11:15 +00:00
|
|
|
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
|
2015-04-01 13:21:07 +00:00
|
|
|
}
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
|
2014-03-07 19:17:11 +00:00
|
|
|
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
2013-11-25 13:54:52 +00:00
|
|
|
add_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
|
2013-08-09 16:11:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the count of processing orders.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-11-25 13:54:52 +00:00
|
|
|
function wc_processing_order_count() {
|
2015-03-04 08:51:06 +00:00
|
|
|
return wc_orders_count( 'processing' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the orders count of a specific order status.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
function wc_orders_count( $status ) {
|
2014-07-11 11:43:42 +00:00
|
|
|
$count = 0;
|
|
|
|
|
2015-03-04 08:51:06 +00:00
|
|
|
$order_statuses = array_keys( wc_get_order_statuses() );
|
|
|
|
|
|
|
|
if ( ! in_array( 'wc-' . $status, $order_statuses ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-11 11:43:42 +00:00
|
|
|
foreach ( wc_get_order_types( 'order-count' ) as $type ) {
|
2015-03-04 08:51:06 +00:00
|
|
|
$this_count = wp_count_posts( $type, 'readable' );
|
|
|
|
$count += isset( $this_count->{'wc-' . $status} ) ? $this_count->{'wc-' . $status} : 0;
|
2014-07-11 11:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
2013-08-09 16:11:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 15:43:19 +00:00
|
|
|
/**
|
|
|
|
* Clear all transients cache for order data.
|
|
|
|
*
|
|
|
|
* @param int $post_id (default: 0)
|
|
|
|
*/
|
|
|
|
function wc_delete_shop_order_transients( $post_id = 0 ) {
|
2014-10-07 10:01:22 +00:00
|
|
|
$post_id = absint( $post_id );
|
|
|
|
$transients_to_clear = array();
|
2014-02-26 15:43:19 +00:00
|
|
|
|
2014-03-20 11:10:25 +00:00
|
|
|
// Clear report transients
|
|
|
|
$reports = WC_Admin_Reports::get_reports();
|
|
|
|
|
|
|
|
foreach ( $reports as $report_group ) {
|
|
|
|
foreach ( $report_group['reports'] as $report_key => $report ) {
|
|
|
|
$transients_to_clear[] = 'wc_report_' . $report_key;
|
2014-03-18 10:48:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-22 19:01:08 +00:00
|
|
|
// clear API report transient
|
|
|
|
$transients_to_clear[] = 'wc_admin_report';
|
|
|
|
|
2014-03-18 10:48:18 +00:00
|
|
|
// Clear transients where we have names
|
2014-02-26 15:43:19 +00:00
|
|
|
foreach( $transients_to_clear as $transient ) {
|
|
|
|
delete_transient( $transient );
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:17:54 +00:00
|
|
|
// Increments the transient version to invalidate cache
|
|
|
|
WC_Cache_Helper::get_transient_version( 'orders', true );
|
|
|
|
|
2014-02-26 15:43:19 +00:00
|
|
|
do_action( 'woocommerce_delete_shop_order_transients', $post_id );
|
|
|
|
}
|
2014-06-17 20:39:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* See if we only ship to billing addresses
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function wc_ship_to_billing_address_only() {
|
|
|
|
return 'billing_only' === get_option( 'woocommerce_ship_to_destination' );
|
|
|
|
}
|
2014-07-08 14:32:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new order refund programmatically
|
|
|
|
*
|
2015-02-11 14:14:42 +00:00
|
|
|
* Returns a new refund object on success which can then be used to add additional data.
|
2014-07-08 14:32:47 +00:00
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param array $args
|
2015-02-03 15:08:36 +00:00
|
|
|
* @return WC_Order_Refund|WP_Error
|
2014-07-08 14:32:47 +00:00
|
|
|
*/
|
|
|
|
function wc_create_refund( $args = array() ) {
|
|
|
|
$default_args = array(
|
2014-07-24 14:34:14 +00:00
|
|
|
'amount' => '',
|
|
|
|
'reason' => null,
|
|
|
|
'order_id' => 0,
|
|
|
|
'refund_id' => 0,
|
2015-05-15 15:27:11 +00:00
|
|
|
'line_items' => array(),
|
|
|
|
'date' => current_time( 'mysql', 0 )
|
2014-07-08 14:32:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $default_args );
|
|
|
|
$refund_data = array();
|
|
|
|
|
2015-06-15 20:20:31 +00:00
|
|
|
// prevent negative refunds
|
|
|
|
if ( 0 > $args['amount'] ) {
|
|
|
|
$args['amount'] = 0;
|
|
|
|
}
|
|
|
|
|
2014-07-08 14:32:47 +00:00
|
|
|
if ( $args['refund_id'] > 0 ) {
|
|
|
|
$updating = true;
|
|
|
|
$refund_data['ID'] = $args['refund_id'];
|
|
|
|
} else {
|
|
|
|
$updating = false;
|
2014-07-11 11:43:42 +00:00
|
|
|
$refund_data['post_type'] = 'shop_order_refund';
|
2014-07-24 14:34:14 +00:00
|
|
|
$refund_data['post_status'] = 'wc-completed';
|
2014-07-08 14:32:47 +00:00
|
|
|
$refund_data['ping_status'] = 'closed';
|
2014-10-23 10:36:12 +00:00
|
|
|
$refund_data['post_author'] = get_current_user_id() ? get_current_user_id() : 1;
|
2014-07-08 14:32:47 +00:00
|
|
|
$refund_data['post_password'] = uniqid( 'refund_' );
|
|
|
|
$refund_data['post_parent'] = absint( $args['order_id'] );
|
|
|
|
$refund_data['post_title'] = sprintf( __( 'Refund – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) );
|
2015-05-15 15:27:11 +00:00
|
|
|
$refund_data['post_date'] = $args['date'];
|
2014-07-08 14:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! is_null( $args['reason'] ) ) {
|
|
|
|
$refund_data['post_excerpt'] = $args['reason'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $updating ) {
|
|
|
|
$refund_id = wp_update_post( $refund_data );
|
|
|
|
} else {
|
|
|
|
$refund_id = wp_insert_post( apply_filters( 'woocommerce_new_refund_data', $refund_data ), true );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_wp_error( $refund_id ) ) {
|
|
|
|
return $refund_id;
|
|
|
|
}
|
2014-07-23 16:56:43 +00:00
|
|
|
|
2014-07-08 14:32:47 +00:00
|
|
|
if ( ! $updating ) {
|
2014-07-23 16:41:35 +00:00
|
|
|
// Default refund meta data
|
2014-07-08 14:32:47 +00:00
|
|
|
update_post_meta( $refund_id, '_refund_amount', wc_format_decimal( $args['amount'] ) );
|
|
|
|
|
2014-10-06 12:39:49 +00:00
|
|
|
// Get refund object
|
|
|
|
$refund = wc_get_order( $refund_id );
|
2015-06-04 07:55:24 +00:00
|
|
|
$order = wc_get_order( $args['order_id'] );
|
|
|
|
|
|
|
|
// Refund currency is the same used for the parent order
|
|
|
|
update_post_meta( $refund_id, '_order_currency', $order->get_order_currency() );
|
2014-10-06 12:39:49 +00:00
|
|
|
|
2014-07-23 16:41:35 +00:00
|
|
|
// Negative line items
|
2014-07-24 14:34:14 +00:00
|
|
|
if ( sizeof( $args['line_items'] ) > 0 ) {
|
|
|
|
$order_items = $order->get_items( array( 'line_item', 'fee', 'shipping' ) );
|
2014-07-23 16:41:35 +00:00
|
|
|
|
2014-07-24 14:34:14 +00:00
|
|
|
foreach ( $args['line_items'] as $refund_item_id => $refund_item ) {
|
|
|
|
if ( isset( $order_items[ $refund_item_id ] ) ) {
|
|
|
|
if ( empty( $refund_item['qty'] ) && empty( $refund_item['refund_total'] ) && empty( $refund_item['refund_tax'] ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-28 13:59:55 +00:00
|
|
|
|
|
|
|
// Prevents errors when the order has no taxes
|
|
|
|
if ( ! isset( $refund_item['refund_tax'] ) ) {
|
|
|
|
$refund_item['refund_tax'] = array();
|
|
|
|
}
|
|
|
|
|
2014-07-24 14:34:14 +00:00
|
|
|
switch ( $order_items[ $refund_item_id ]['type'] ) {
|
|
|
|
case 'line_item' :
|
2014-10-06 12:39:49 +00:00
|
|
|
$line_item_args = array(
|
2014-07-24 14:34:14 +00:00
|
|
|
'totals' => array(
|
|
|
|
'subtotal' => wc_format_refund_total( $refund_item['refund_total'] ),
|
|
|
|
'total' => wc_format_refund_total( $refund_item['refund_total'] ),
|
|
|
|
'subtotal_tax' => wc_format_refund_total( array_sum( $refund_item['refund_tax'] ) ),
|
|
|
|
'tax' => wc_format_refund_total( array_sum( $refund_item['refund_tax'] ) ),
|
|
|
|
'tax_data' => array( 'total' => array_map( 'wc_format_refund_total', $refund_item['refund_tax'] ), 'subtotal' => array_map( 'wc_format_refund_total', $refund_item['refund_tax'] ) )
|
|
|
|
)
|
|
|
|
);
|
2014-10-06 12:39:49 +00:00
|
|
|
$new_item_id = $refund->add_product( $order->get_product_from_item( $order_items[ $refund_item_id ] ), isset( $refund_item['qty'] ) ? $refund_item['qty'] : 0, $line_item_args );
|
2014-07-25 15:48:19 +00:00
|
|
|
wc_add_order_item_meta( $new_item_id, '_refunded_item_id', $refund_item_id );
|
2014-07-24 14:34:14 +00:00
|
|
|
break;
|
|
|
|
case 'shipping' :
|
|
|
|
$shipping = new stdClass();
|
|
|
|
$shipping->label = $order_items[ $refund_item_id ]['name'];
|
|
|
|
$shipping->id = $order_items[ $refund_item_id ]['method_id'];
|
|
|
|
$shipping->cost = wc_format_refund_total( $refund_item['refund_total'] );
|
|
|
|
$shipping->taxes = array_map( 'wc_format_refund_total', $refund_item['refund_tax'] );
|
|
|
|
|
2014-07-25 15:48:19 +00:00
|
|
|
$new_item_id = $refund->add_shipping( $shipping );
|
|
|
|
wc_add_order_item_meta( $new_item_id, '_refunded_item_id', $refund_item_id );
|
2014-07-24 14:34:14 +00:00
|
|
|
break;
|
|
|
|
case 'fee' :
|
|
|
|
$fee = new stdClass();
|
|
|
|
$fee->name = $order_items[ $refund_item_id ]['name'];
|
|
|
|
$fee->tax_class = $order_items[ $refund_item_id ]['tax_class'];
|
|
|
|
$fee->taxable = $fee->tax_class !== '0';
|
|
|
|
$fee->amount = wc_format_refund_total( $refund_item['refund_total'] );
|
|
|
|
$fee->tax = wc_format_refund_total( array_sum( $refund_item['refund_tax'] ) );
|
|
|
|
$fee->tax_data = array_map( 'wc_format_refund_total', $refund_item['refund_tax'] );
|
2014-07-28 13:59:55 +00:00
|
|
|
|
2014-07-25 15:48:19 +00:00
|
|
|
$new_item_id = $refund->add_fee( $fee );
|
|
|
|
wc_add_order_item_meta( $new_item_id, '_refunded_item_id', $refund_item_id );
|
2014-07-24 14:34:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-07-23 16:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 14:34:14 +00:00
|
|
|
$refund->update_taxes();
|
2014-07-23 16:41:35 +00:00
|
|
|
}
|
2014-10-06 12:39:49 +00:00
|
|
|
|
|
|
|
$refund->calculate_totals( false );
|
|
|
|
|
|
|
|
// Set total to total refunded which may vary from order items
|
|
|
|
$refund->set_total( wc_format_decimal( $args['amount'] ) * -1, 'total' );
|
2014-10-07 22:58:21 +00:00
|
|
|
|
2015-06-17 14:35:54 +00:00
|
|
|
// Figure out if this is just a partial refund
|
|
|
|
$max_remaining_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() );
|
2015-07-29 12:13:41 +00:00
|
|
|
$max_remaining_items = absint( $order->get_item_count() - $order->get_item_count_refunded() );
|
2015-07-16 19:55:48 +00:00
|
|
|
|
2015-07-29 12:13:41 +00:00
|
|
|
if ( $max_remaining_refund > 0 || $max_remaining_items > 0 ) {
|
2015-06-18 10:34:21 +00:00
|
|
|
do_action( 'woocommerce_order_partially_refunded', $args['order_id'], true, $refund_id );
|
2015-06-17 14:35:54 +00:00
|
|
|
}
|
2015-07-16 19:55:48 +00:00
|
|
|
|
2015-06-22 15:54:45 +00:00
|
|
|
do_action( 'woocommerce_refund_created', $refund_id, $args );
|
2014-07-23 16:41:35 +00:00
|
|
|
}
|
2014-07-23 16:56:43 +00:00
|
|
|
|
2014-10-06 12:39:49 +00:00
|
|
|
// Clear transients
|
|
|
|
wc_delete_shop_order_transients( $args['order_id'] );
|
|
|
|
|
2014-07-08 14:32:47 +00:00
|
|
|
return new WC_Order_Refund( $refund_id );
|
|
|
|
}
|
2014-07-19 04:08:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get tax class by tax id.
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param int $tax_id
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function wc_get_tax_class_by_tax_id( $tax_id ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$tax_class = $wpdb->get_var( $wpdb->prepare( "SELECT tax_rate_class FROM {$wpdb->prefix}woocommerce_tax_rates WHERE tax_rate_id = %d", $tax_id ) );
|
|
|
|
|
|
|
|
return wc_clean( $tax_class );
|
|
|
|
}
|
2014-07-22 15:13:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get payment gateway class by order data.
|
|
|
|
*
|
|
|
|
* @since 2.2
|
|
|
|
* @param int|WC_Order $order
|
|
|
|
* @return WC_Payment_Gateway|bool
|
|
|
|
*/
|
|
|
|
function wc_get_payment_gateway_by_order( $order ) {
|
|
|
|
if ( WC()->payment_gateways() ) {
|
|
|
|
$payment_gateways = WC()->payment_gateways->payment_gateways();
|
|
|
|
} else {
|
|
|
|
$payment_gateways = array();
|
|
|
|
}
|
|
|
|
|
2014-08-25 19:11:39 +00:00
|
|
|
if ( ! is_object( $order ) ) {
|
|
|
|
$order_id = absint( $order );
|
2015-07-30 18:07:34 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2014-07-22 15:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $payment_gateways[ $order->payment_method ] ) ? $payment_gateways[ $order->payment_method ] : false;
|
|
|
|
}
|
2015-04-24 14:58:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When refunding an order, create a refund line item if the partial refunds do not match order total.
|
|
|
|
*
|
|
|
|
* This is manual; no gateway refund will be performed.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
* @param int $order_id
|
|
|
|
*/
|
|
|
|
function wc_order_fully_refunded( $order_id ) {
|
|
|
|
$order = wc_get_order( $order_id );
|
|
|
|
$max_refund = wc_format_decimal( $order->get_total() - $order->get_total_refunded() );
|
|
|
|
|
|
|
|
if ( ! $max_refund ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the refund object
|
|
|
|
$refund = wc_create_refund( array(
|
|
|
|
'amount' => $max_refund,
|
|
|
|
'reason' => __( 'Order Fully Refunded', 'woocommerce' ),
|
|
|
|
'order_id' => $order_id,
|
|
|
|
'line_items' => array()
|
|
|
|
) );
|
|
|
|
|
|
|
|
wc_delete_shop_order_transients( $order_id );
|
|
|
|
|
|
|
|
do_action( 'woocommerce_order_fully_refunded', $order_id, $refund->id );
|
|
|
|
}
|
|
|
|
add_action( 'woocommerce_order_status_refunded', 'wc_order_fully_refunded' );
|