Function replacements for class methods
This commit is contained in:
parent
a1adf0cf78
commit
7b3a9b27ed
|
@ -1054,3 +1054,134 @@ function wc_order_search( $term ) {
|
|||
|
||||
return $post_ids;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update total sales amount for each product within a paid order.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param int $order_id
|
||||
*/
|
||||
function wc_update_total_sales_counts( $order_id ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
if ( ! $order || 'yes' === get_post_meta( $order_id, '_recorded_sales', true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( sizeof( $order->get_items() ) > 0 ) {
|
||||
foreach ( $order->get_items() as $item ) {
|
||||
if ( $item['product_id'] > 0 ) {
|
||||
update_post_meta( $item['product_id'], 'total_sales', absint( get_post_meta( $item['product_id'], 'total_sales', true ) ) + absint( $item['qty'] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
update_post_meta( $order_id, '_recorded_sales', 'yes' );
|
||||
|
||||
/**
|
||||
* Called when sales for an order are recorded
|
||||
*
|
||||
* @param int $order_id order id
|
||||
*/
|
||||
do_action( 'woocommerce_recorded_sales', $order_id );
|
||||
}
|
||||
add_action( 'woocommerce_order_status_completed', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_processing', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_on-hold', 'wc_update_total_sales_counts' );
|
||||
|
||||
/**
|
||||
* Update used coupon amount for each coupon within an order.
|
||||
*
|
||||
* @since 2.6.0
|
||||
* @param int $order_id
|
||||
*/
|
||||
function wc_update_coupon_usage_counts( $order_id ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
$has_recorded = get_post_meta( $order_id, '_recorded_coupon_usage_counts', true );
|
||||
|
||||
if ( ! $order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $order->has_status( 'cancelled' ) && 'yes' === $has_recorded ) {
|
||||
$action = 'reduce';
|
||||
delete_post_meta( $order_id, '_recorded_coupon_usage_counts' );
|
||||
} elseif ( ! $order->has_status( 'cancelled' ) && 'yes' !== $has_recorded ) {
|
||||
$action = 'increase';
|
||||
update_post_meta( $order_id, '_recorded_coupon_usage_counts', 'yes' );
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( sizeof( $order->get_used_coupons() ) > 0 ) {
|
||||
foreach ( $order->get_used_coupons() as $code ) {
|
||||
if ( ! $code ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$coupon = new WC_Coupon( $code );
|
||||
|
||||
if ( ! $used_by = $order->get_user_id() ) {
|
||||
$used_by = $order->get_billing_email();
|
||||
}
|
||||
|
||||
switch ( $action ) {
|
||||
case 'reduce' :
|
||||
$coupon->dcr_usage_count( $used_by );
|
||||
break;
|
||||
case 'increase' :
|
||||
$coupon->inc_usage_count( $used_by );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
add_action( 'woocommerce_order_status_completed', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_processing', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_on-hold', 'wc_update_total_sales_counts' );
|
||||
add_action( 'woocommerce_order_status_cancelled', 'wc_update_total_sales_counts' );
|
||||
|
||||
/**
|
||||
* When a payment is complete, we can reduce stock levels for items within an order.
|
||||
* @since 2.6.0
|
||||
* @param int $order_id
|
||||
*/
|
||||
function wc_maybe_reduce_stock_levels( $order_id ) {
|
||||
if ( apply_filters( 'woocommerce_payment_complete_reduce_order_stock', ! get_post_meta( $order_id, '_order_stock_reduced', true ), $order_id ) ) {
|
||||
wc_reduce_stock_levels( $order_id );
|
||||
add_post_meta( $order_id, '_order_stock_reduced', '1', true );
|
||||
}
|
||||
}
|
||||
add_action( 'woocommerce_payment_complete', 'wc_maybe_reduce_stock_levels' );
|
||||
|
||||
/**
|
||||
* Reduce stock levels for items within an order.
|
||||
* @since 2.6.0
|
||||
* @param int $order_id
|
||||
*/
|
||||
function wc_reduce_stock_levels( $order_id ) {
|
||||
$order = wc_get_order( $order_id );
|
||||
|
||||
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) && $order && apply_filters( 'woocommerce_can_reduce_order_stock', true, $order ) && sizeof( $order->get_items() ) > 0 ) {
|
||||
foreach ( $order->get_items() as $item ) {
|
||||
if ( $item->is_type( 'line_item' ) && ( $product = $item->get_product() ) && $product->managing_stock() ) {
|
||||
$qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $order, $item );
|
||||
$new_stock = $product->reduce_stock( $qty );
|
||||
$item_name = $product->get_sku() ? $product->get_sku(): $item['product_id'];
|
||||
|
||||
if ( ! empty( $item['variation_id'] ) ) {
|
||||
$order->add_order_note( sprintf( __( 'Item %s variation #%s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $item['variation_id'], $new_stock + $qty, $new_stock ) );
|
||||
} else {
|
||||
$order->add_order_note( sprintf( __( 'Item %s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $new_stock + $qty, $new_stock ) );
|
||||
}
|
||||
|
||||
if ( $new_stock < 0 ) {
|
||||
do_action( 'woocommerce_product_on_backorder', array( 'product' => $product, 'order_id' => $order_id, 'quantity' => $qty_ordered ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
do_action( 'woocommerce_reduce_order_stock', $order );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2216,3 +2216,124 @@ if ( ! function_exists( 'woocommerce_account_edit_account' ) ) {
|
|||
WC_Shortcode_My_Account::edit_account();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( ! function_exists( 'wc_get_email_order_items' ) ) {
|
||||
/**
|
||||
* Get HTML for the order items to be shown in emails.
|
||||
* @param WC_Order $order
|
||||
* @param array $args
|
||||
* @since 2.7.0
|
||||
*/
|
||||
function wc_get_email_order_items( $order, $args = array() ) {
|
||||
ob_start();
|
||||
|
||||
$defaults = array(
|
||||
'show_sku' => false,
|
||||
'show_image' => false,
|
||||
'image_size' => array( 32, 32 ),
|
||||
'plain_text' => false
|
||||
);
|
||||
|
||||
$args = wp_parse_args( $args, $defaults );
|
||||
$template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
|
||||
|
||||
wc_get_template( $template, array(
|
||||
'order' => $order,
|
||||
'items' => $order->get_items(),
|
||||
'show_download_links' => $order->is_download_permitted(),
|
||||
'show_sku' => $args['show_sku'],
|
||||
'show_purchase_note' => $order->is_paid(),
|
||||
'show_image' => $args['show_image'],
|
||||
'image_size' => $args['image_size'],
|
||||
) );
|
||||
|
||||
return apply_filters( 'woocommerce_email_order_items_table', ob_get_clean(), $order );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'wc_display_item_meta' ) ) {
|
||||
/**
|
||||
* Display item meta data.
|
||||
* @param WC_Item $item
|
||||
* @param array $args
|
||||
* @return string|void
|
||||
*/
|
||||
function wc_display_item_meta( $item, $args = array() ) {
|
||||
$strings = array();
|
||||
$html = '';
|
||||
$args = wp_parse_args( $args, array(
|
||||
'before' => '<ul class="wc-item-meta"><li>',
|
||||
'after' => '</li></ul>',
|
||||
'separator' => '</li><li>',
|
||||
'echo' => true,
|
||||
'autop' => false,
|
||||
) );
|
||||
|
||||
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
|
||||
if ( '_' === substr( $meta->key, 0, 1 ) ) {
|
||||
continue;
|
||||
}
|
||||
$value = $args['autop'] ? wp_kses_post( wpautop( make_clickable( $meta->display_value ) ) ) : wp_kses_post( make_clickable( $meta->display_value ) );
|
||||
$strings[] = '<strong class="wc-item-meta-label">' . wp_kses_post( $meta->display_key ) . ':</strong> ' . $value;
|
||||
}
|
||||
|
||||
if ( $strings ) {
|
||||
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
|
||||
}
|
||||
|
||||
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
|
||||
|
||||
if ( $args['echo'] ) {
|
||||
echo $html;
|
||||
} else {
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'wc_display_item_downloads' ) ) {
|
||||
/**
|
||||
* Display item download links.
|
||||
* @param WC_Item $item
|
||||
* @param array $args
|
||||
* @return string|void
|
||||
*/
|
||||
function wc_display_item_downloads( $item, $args = array() ) {
|
||||
$strings = array();
|
||||
$html = '';
|
||||
$args = wp_parse_args( $args, array(
|
||||
'before' => '<ul class ="wc-item-downloads"><li>',
|
||||
'after' => '</li></ul>',
|
||||
'separator' => '</li><li>',
|
||||
'echo' => true,
|
||||
'show_url' => false,
|
||||
) );
|
||||
|
||||
if ( is_object( $item ) && $item->is_type( 'line_item' ) && ( $downloads = $item->get_item_downloads() ) ) {
|
||||
$i = 0;
|
||||
foreach ( $downloads as $file ) {
|
||||
$i ++;
|
||||
|
||||
if ( $args['show_url'] ) {
|
||||
$strings[] = '<strong class="wc-item-download-label">' . esc_html( $file['name'] ) . ':</strong> ' . esc_html( $file['download_url'] );
|
||||
} else {
|
||||
$prefix = sizeof( $downloads ) > 1 ? sprintf( __( 'Download %d', 'woocommerce' ), $i ) : __( 'Download', 'woocommerce' );
|
||||
$strings[] = '<strong class="wc-item-download-label">' . $prefix . ':</strong> <a href="' . esc_url( $file['download_url'] ) . '" target="_blank">' . esc_html( $file['name'] ) . '</a>';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( $strings ) {
|
||||
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
|
||||
}
|
||||
|
||||
$html = apply_filters( 'woocommerce_display_item_downloads', $html, $item, $args );
|
||||
|
||||
if ( $args['echo'] ) {
|
||||
echo $html;
|
||||
} else {
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue