Skip to content

Commit

Permalink
Merge pull request #262 from woocommerce/fix/hpos
Browse files Browse the repository at this point in the history
Address HPOS compatibility
  • Loading branch information
mattallan committed Aug 21, 2023
2 parents fc6e9a8 + 040e51b commit ce32a9d
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 47 deletions.
27 changes: 19 additions & 8 deletions includes/class-wcs-export-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ public function export_page() {
* @since 1.0
*/
public function home_page() {

$statuses = wcs_get_subscription_statuses();
$status_counts = array();

foreach ( wp_count_posts( 'shop_subscription' ) as $status => $count ) {
if ( array_key_exists( $status, $statuses ) ) {
$status_counts[ $status ] = $count;
if ( wcsi_is_hpos_enabled() ) {
$wcs_datastore = WC_Data_Store::load( 'subscription' );

foreach ( array_keys( $statuses ) as $status ) {
$status_counts[ $status ] = $wcs_datastore->get_order_count( $status );
}
} else {
foreach ( wp_count_posts( 'shop_subscription' ) as $status => $count ) {
if ( array_key_exists( $status, $statuses ) ) {
$status_counts[ $status ] = $count;
}
}
}

Expand Down Expand Up @@ -398,10 +405,14 @@ public function filter_payment_method( $query_args, $args ) {
if ( isset( $_POST['payment'] ) && 'any' != $_POST['payment'] ) {
$payment_payment = ( 'none' == $_POST['payment'] ) ? '' : $_POST['payment'];

$query_args['meta_query'][] = array(
'key' => '_payment_method',
'value' => $payment_payment,
);
if ( wcsi_is_hpos_enabled() ) {
$query_args['payment_method'] = $payment_payment;
} else {
$query_args['meta_query'][] = array(
'key' => '_payment_method',
'value' => $payment_payment,
);
}
}

return $query_args;
Expand Down
6 changes: 1 addition & 5 deletions includes/class-wcs-exporter-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ public static function get_subscriptions_to_export( $post_data ) {
);

if ( ! empty( $post_data['status'] ) ) {
$statuses = array_keys( $post_data['status'] );

if ( ! empty( $statuses ) && is_array( $statuses ) ) {
$args['subscription_status'] = implode( ',', $statuses );
}
$args['subscription_status'] = array_keys( $post_data['status'] );
}

if ( ! empty( $post_data['customer'] ) && is_numeric( $post_data['customer'] ) ) {
Expand Down
103 changes: 69 additions & 34 deletions includes/class-wcs-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ class WCS_Importer {
'shipping_country',
);

private static $order_meta_setter_map = [
'cart_discount' => 'discount_total',
'cart_discount_tax' => 'discount_tax',
'customer_user' => 'customer_id',
'order_tax' => 'cart_tax',
'order_shipping' => 'shipping_total',
'order_currency' => 'currency',
'order_shipping_tax' => 'shipping_tax',
'order_total' => 'total',
];

/**
* Setup function for the import parse class
*
Expand Down Expand Up @@ -95,26 +106,52 @@ public static function import_data( $data ) {
public static function add_order_key_post_meta_if_missing() {
global $wpdb;

// Get the post_ids of the subscriptions whose order_key post meta is NULL or empty or missing
$subscription_ids_needing_order_key = $wpdb->get_results( "
SELECT ID FROM {$wpdb->prefix}posts WHERE
post_type = 'shop_subscription'
AND
ID NOT IN (
SELECT post_id FROM {$wpdb->prefix}postmeta WHERE
meta_key = '_order_key'
AND
meta_value IS NOT NULL
AND
meta_value <> ''
if ( ! wcsi_is_hpos_enabled() ) {
// Get the post_ids of the subscriptions whose order_key post meta is NULL or empty or missing
$subscription_ids_needing_order_key = $wpdb->get_col( "
SELECT ID FROM {$wpdb->prefix}posts WHERE
post_type = 'shop_subscription'
AND
ID NOT IN (
SELECT post_id FROM {$wpdb->prefix}postmeta WHERE
meta_key = '_order_key'
AND
meta_value IS NOT NULL
AND
meta_value <> ''
)
AND
post_status IN ( '" . implode( "','", array_keys( wcs_get_subscription_statuses() ) ) . "' )"
);
} else {
$subscription_ids_needing_order_key = wc_get_orders(
array(
'type' => 'shop_subscription',
'field_query' => array(
array(
'relation' => 'or',
array(
'field' => 'order_key',
'value' => 'NOT EXISTS',
),
array(
'field' => 'order_key',
'value' => '',
),
)
),
'status' => array_keys( wcs_get_subscription_statuses() ),
'return' => 'ids',
'limit' => -1,
)
AND
post_status IN ( '" . implode( "','", array_keys( wcs_get_subscription_statuses() ) ) . "' )"
);
);
}

//Set the order_key post meta for each of them
foreach( $subscription_ids_needing_order_key as $key => $post ) {
update_post_meta( $post->ID, '_order_key', uniqid( 'order_' ) );
foreach( $subscription_ids_needing_order_key as $order_id ) {
$order = wc_get_order( $order_id );
$order->set_order_key( wc_generate_order_key() );
$order->save();
}
}

Expand Down Expand Up @@ -180,7 +217,7 @@ public static function import_subscription( $data ) {

self::$row = $data;
$set_manual = $requires_manual_renewal = false;
$post_meta = $order_items = array();
$order_meta = $order_items = array();
$result = array(
'warning' => array(),
'error' => array(),
Expand Down Expand Up @@ -217,17 +254,14 @@ public static function import_subscription( $data ) {
case 'order_shipping':
case 'order_shipping_tax':
case 'order_total':
$value = ( ! empty( $data[ self::$fields[ $column ] ] ) ) ? $data[ self::$fields[ $column ] ] : 0;
$post_meta[] = array( 'key' => '_' . $column, 'value' => $value );
$order_meta[ $column ] = ( ! empty( $data[ self::$fields[ $column ] ] ) ) ? $data[ self::$fields[ $column ] ] : 0;
break;

case 'payment_method':
$payment_method = ( ! empty( $data[ self::$fields[ $column ] ] ) ) ? strtolower( $data[ self::$fields[ $column ] ] ) : '';
$title = ( ! empty( $data[ self::$fields['payment_method_title'] ] ) ) ? $data[ self::$fields['payment_method_title'] ] : $payment_method;

if ( ! empty( $payment_method ) && 'manual' != $payment_method ) {
$post_meta[] = array( 'key' => '_' . $column, 'value' => $payment_method );
$post_meta[] = array( 'key' => '_payment_method_title', 'value' => $title );
$order_meta[ $column ] = $payment_method;
$order_meta[ 'payment_method_title' ] = ( ! empty( $data[ self::$fields['payment_method_title'] ] ) ) ? $data[ self::$fields['payment_method_title'] ] : $payment_method;
} else {
$set_manual = true;
}
Expand All @@ -236,7 +270,6 @@ public static function import_subscription( $data ) {
$requires_manual_renewal = true;
}
break;

case 'shipping_address_1':
case 'shipping_city':
case 'shipping_postcode':
Expand Down Expand Up @@ -269,12 +302,11 @@ public static function import_subscription( $data ) {
}
}

$post_meta[] = array( 'key' => '_' . $column, 'value' => $value );
$order_meta[ $column ] = $value;
break;

default:
$value = ( ! empty( $data[ self::$fields[ $column ] ] ) ) ? $data[ self::$fields[ $column ] ] : '';
$post_meta[] = array( 'key' => '_' . $column, 'value' => $value );
$order_meta[ $column ] = ( ! empty( $data[ self::$fields[ $column ] ] ) ) ? $data[ self::$fields[ $column ] ] : '';
}
}

Expand Down Expand Up @@ -352,23 +384,25 @@ public static function import_subscription( $data ) {

$subscription_id = version_compare( WC()->version, '3.0', '>=' ) ? $subscription->get_id() : $subscription->id;

foreach ( $post_meta as $meta_data ) {
update_post_meta( $subscription_id, $meta_data['key'], $meta_data['value'] );
foreach ( $order_meta as $key => $value ) {
$subscription->{'set_' . ( self::$order_meta_setter_map[ $key ] ?? $key )}( $value );
}

foreach ( self::$fields['custom_post_meta'] as $meta_key ) {
if ( ! empty( $data[ $meta_key ] ) ) {
update_post_meta( $subscription_id, $meta_key, $data[ $meta_key ] );
$subscription->update_meta_data( $meta_key, $data[ $meta_key ] );
}
}

foreach ( self::$fields['custom_user_post_meta'] as $meta_key ) {
if ( ! empty( $data[ $meta_key ] ) ) {
update_post_meta( $subscription_id, $meta_key, $data[ $meta_key ] );
$subscription->update_meta_data( $meta_key, $data[ $meta_key ] );
update_user_meta( $user_id, $meta_key, $data[ $meta_key ] );
}
}

$subscription->save();

// Now that we've set all the meta data, reinit the object so the data is set
$subscription = wcs_get_subscription( $subscription_id );

Expand Down Expand Up @@ -931,8 +965,9 @@ public static function add_shipping_lines( $subscription, $data, $chosen_tax_rat
throw new Exception( __( 'An error occurred when trying to add the shipping item to the subscription, a subscription not been created for this row.', 'wcs-import-export' ) );
}

update_post_meta( $subscription->get_id(), '_shipping_method', $shipping_method );
update_post_meta( $subscription->get_id(), '_shipping_method_title', $shipping_title );
$subscription->update_meta_data( '_shipping_method', $shipping_method );
$subscription->update_meta_data( '_shipping_method_title', $shipping_title );
$subscription->save();
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions includes/wcsi-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,14 @@ function wcsi_check_customer( $data, $mapped_fields, $test_mode = false, $email_

return $found_customer;
}

/**
* Checks whether HPOS is in use in WooCommerce.
*
* @since 2.2.0
*
* @return bool TRUE if HPOS is enabled, FALSE otherwise.
*/
function wcsi_is_hpos_enabled() {
return function_exists( 'wcs_is_custom_order_tables_usage_enabled' ) && wcs_is_custom_order_tables_usage_enabled();
}
12 changes: 12 additions & 0 deletions wcs-importer-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@
require_once( 'woo-includes/woo-functions.php' );
}

/**
* Declare plugin compatibility with WooCommerce HPOS.
*/
add_action(
'before_woocommerce_init',
function() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
);

require_once( 'includes/wcsi-functions.php' );

class WCS_Importer_Exporter {
Expand Down

0 comments on commit ce32a9d

Please sign in to comment.