Skip to content

Commit

Permalink
Index orders
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Feb 5, 2025
1 parent 45fff9c commit de0ec89
Showing 1 changed file with 99 additions and 23 deletions.
122 changes: 99 additions & 23 deletions includes/classes/Feature/WooCommerce/OrdersHPOS.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,14 @@ public function sync_order( $order_id ) {
* @return array
*/
public function set_order_data( $post_args, $post_id ) {
if ( \Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE !== $post_args['post_type'] ) {
if (
\Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer::PLACEHOLDER_ORDER_POST_TYPE !== $post_args['post_type']
&& ! in_array( $post_args['post_type'], $this->orders->get_supported_post_types(), true ) ) {
return $post_args;
}

/**
* Post Indexable instance.
*
* @var \ElasticPress\Indexable\Post\Post
*/
$post_indexable = Indexables::factory()->get( 'post' );
$order = wc_get_order( $post_id );
$order_class = get_class( $order );
$post_order = new $order_class();

$post_args['post_type'] = $order->get_type();
$post_args['post_status'] = $order->get_status( 'edit' );
Expand All @@ -93,11 +88,7 @@ public function set_order_data( $post_args, $post_id ) {
$post_args['edit_date'] = true;
$post_args['post_excerpt'] = method_exists( $order, 'get_customer_note' ) ? $order->get_customer_note() : '';

$post_order = new $order_class();
$post_order->set_id( $order->get_id() );
$post_order->set_props( $order->get_data() );

error_log( var_export( $post_order, true ) );
$post_order = new \WP_Post( (object) $post_args );

add_filter( 'ep_prepare_meta_data', [ $this, 'prepare_meta_data' ], 10, 2 );
$post_args['meta'] = $post_indexable->prepare_meta_types( $post_indexable->prepare_meta( $post_order ) );
Expand All @@ -107,25 +98,110 @@ public function set_order_data( $post_args, $post_id ) {
}

/**
* Format meta data
* Get meta data from an order as it would be stored in the post_meta table.
*
* This method is a copy of WC_Order_Data_Store_CPT::update_post_meta() with some simplifications and returning data as an array,
* instead of actually storing it in the database.
*
* @param array $order_meta Meta data
* @param WP_Post $order_post Order object
* @return array
*/
public function prepare_meta_data( $order_meta, $order_post ) {
$order = wc_get_order( $order_post->get_id() );

if ( is_null( $order->get_meta() ) ) {
return $order_meta;
$data_store = new \WC_Order_Data_Store_CPT();
$order = wc_get_order( $order_post->ID );

$meta_data = [];
$meta_key_to_props = [
'_order_key' => 'order_key',
'_customer_user' => 'customer_id',
'_payment_method' => 'payment_method',
'_payment_method_title' => 'payment_method_title',
'_transaction_id' => 'transaction_id',
'_customer_ip_address' => 'customer_ip_address',
'_customer_user_agent' => 'customer_user_agent',
'_created_via' => 'created_via',
'_date_completed' => 'date_completed',
'_date_paid' => 'date_paid',
'_cart_hash' => 'cart_hash',
'_download_permissions_granted' => 'download_permissions_granted',
'_recorded_sales' => 'recorded_sales',
'_recorded_coupon_usage_counts' => 'recorded_coupon_usage_counts',
'_new_order_email_sent' => 'new_order_email_sent',
'_order_stock_reduced' => 'order_stock_reduced',
];

foreach ( $meta_key_to_props as $meta_key => $prop ) {
$value = $order->{"get_$prop"}( 'edit' );
$value = is_string( $value ) ? wp_slash( $value ) : $value;
switch ( $prop ) {
case 'date_paid':
case 'date_completed':
$value = ! is_null( $value ) ? $value->getTimestamp() : '';
break;
case 'download_permissions_granted':
case 'recorded_sales':
case 'recorded_coupon_usage_counts':
case 'order_stock_reduced':
if ( is_null( $value ) || '' === $value ) {
break;
}
$value = is_bool( $value ) ? wc_bool_to_string( $value ) : $value;
break;
case 'new_order_email_sent':
if ( is_null( $value ) || '' === $value ) {
break;
}
$value = is_bool( $value ) ? wc_bool_to_string( $value ) : $value;
$value = 'yes' === $value ? 'true' : 'false'; // For backward compatibility, we store as true/false in DB.
break;
}

// We want to persist internal data store keys as 'yes' or 'no' if they are boolean to maintain compatibility.
if ( is_bool( $value ) && in_array( $prop, array_values( $data_store->get_internal_data_store_key_getters() ), true ) ) {
$value = wc_bool_to_string( $value );
}

$meta_data[ $meta_key ] = [ $value ];
}

foreach ( $order->get_meta_data() as $meta_data ) {
$order_meta[ $meta_data->key ] = ( is_object( $meta_data->value ) && '__PHP_Incomplete_Class' === get_class( $meta_data->value ) )
? maybe_serialize( $meta_data->value )
: $meta_data->value;
$address_props = array(
'billing' => array(
'_billing_first_name' => 'billing_first_name',
'_billing_last_name' => 'billing_last_name',
'_billing_company' => 'billing_company',
'_billing_address_1' => 'billing_address_1',
'_billing_address_2' => 'billing_address_2',
'_billing_city' => 'billing_city',
'_billing_state' => 'billing_state',
'_billing_postcode' => 'billing_postcode',
'_billing_country' => 'billing_country',
'_billing_email' => 'billing_email',
'_billing_phone' => 'billing_phone',
),
'shipping' => array(
'_shipping_first_name' => 'shipping_first_name',
'_shipping_last_name' => 'shipping_last_name',
'_shipping_company' => 'shipping_company',
'_shipping_address_1' => 'shipping_address_1',
'_shipping_address_2' => 'shipping_address_2',
'_shipping_city' => 'shipping_city',
'_shipping_state' => 'shipping_state',
'_shipping_postcode' => 'shipping_postcode',
'_shipping_country' => 'shipping_country',
'_shipping_phone' => 'shipping_phone',
),
);

foreach ( $address_props as $props ) {
foreach ( $props as $meta_key => $prop ) {
$value = $order->{"get_$prop"}( 'edit' );
$value = is_string( $value ) ? wp_slash( $value ) : $value;

$meta_data[ $meta_key ] = [ $value ];
}
}

return $order_meta;
return $meta_data;
}
}

0 comments on commit de0ec89

Please sign in to comment.