Skip to content

Commit

Permalink
Merge pull request #26 from maxsakal/make-possible-filter-shipment-by…
Browse files Browse the repository at this point in the history
…-category-id

Make possible filter shipments by product categories
  • Loading branch information
dennisnissle authored Jan 19, 2024
2 parents 339c8d8 + edd2a98 commit c6b560c
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/ShipmentQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function get_default_query_vars() {
'order_id' => '',
'parent_id' => '',
'product_ids' => '',
'product_category' => '',
'type' => 'simple',
'country' => '',
'tracking_id' => '',
Expand Down Expand Up @@ -195,6 +196,11 @@ protected function parse_query() {
$this->args['product_ids'] = array_map( 'absint', $this->args['product_ids'] );
}

if ( isset( $this->args['product_category'] ) ) {
$this->args['product_category'] = (array) $this->args['product_category'];
$this->args['product_category'] = array_map( 'absint', $this->args['product_category'] );
}

if ( isset( $this->args['tracking_id'] ) ) {
$this->args['tracking_id'] = sanitize_key( $this->args['tracking_id'] );
}
Expand Down Expand Up @@ -313,6 +319,15 @@ protected function prepare_query() {
$this->query_where .= $wpdb->prepare( " AND shipment_items.shipment_item_product_id IN ({$product_ids_placeholders})", $this->args['product_ids'] ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
}

// product category
if ( isset( $this->args['product_category'] ) ) {
$product_category_placeholders = implode( ', ', array_fill( 0, count( $this->args['product_category'] ), '%d' ) );

$this->query_from .= " JOIN {$wpdb->prefix}woocommerce_gzd_shipment_items AS shipment_items ON {$wpdb->prefix}woocommerce_gzd_shipments.shipment_id = shipment_items.shipment_id ";
$this->query_from .= " JOIN {$wpdb->prefix}term_relationships AS term_relationships ON term_relationships.object_id = shipment_items.shipment_item_product_id ";
$this->query_where .= $wpdb->prepare( " AND term_relationships.term_taxonomy_id IN ({$product_category_placeholders})", $this->args['product_category']); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
}

// country
if ( isset( $this->args['country'] ) ) {
$this->query_where .= $wpdb->prepare( " AND shipment_country IN ('%s')", $this->args['country'] ); // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.QuotedSimplePlaceholder
Expand Down

0 comments on commit c6b560c

Please sign in to comment.