-`. Aha turns it * into the Woo product id and sends it as `product_id` (contract 1.1). When a * Connect order key signed the request, only that id counts: the product must * exist and be published or private. A variable product line also sends * `variation_id` (contract 1.2): the variation must be published or private and * its parent must be `product_id`, else the line is unknown. Without it the * parent product is used. * * @package Aha_Is_Woocommerce */ defined( 'ABSPATH' ) || exit; /** * Resolves a SKU to a WooCommerce product. */ final class Aha_Is_Woocommerce_Product_Resolver { /** * Resolve one SKU. * * @param string $sku SKU. * @param int|null $product_id Woo product id from a Connect-signed request, null otherwise. * @param mixed $variation_id line_items[].variation_id of a Connect request, null when absent. * @return array { * @type bool $ok False when the SKU is unknown or ambiguous. * @type int $product_id Parent product id. * @type int $variation_id Variation id, 0 when the product is simple. * @type int $stock_unit_id The id the stock belongs to. * @type string $name Product name. * @type bool $manages_stock Whether the store counts this item. * @type int|null $stock_quantity Stock count. * @type bool $in_stock Stock status flag. * @type string $backorders 'no', 'notify' or 'yes'. * } */ public static function resolve( $sku, $product_id = null, $variation_id = null ) { $sku = (string) $sku; if ( null === $product_id ) { $ids = self::find_ids_by_sku( $sku ); } else { $ids = self::find_connect_id( $product_id ); if ( null !== $variation_id && $ids ) { $ids = self::find_connect_variation_id( $product_id, $variation_id ); } } if ( 1 !== count( $ids ) ) { // Zero matches, or more than one. Both are 422 unknown_sku. return array( 'ok' => false ); } $product = wc_get_product( $ids[0] ); if ( ! $product ) { return array( 'ok' => false ); } $parent_id = $product->get_parent_id(); $variation_id = $parent_id ? $product->get_id() : 0; $product_id = $parent_id ? $parent_id : $product->get_id(); return array( 'ok' => true, 'sku' => $sku, 'product_id' => (int) $product_id, 'variation_id' => (int) $variation_id, 'stock_unit_id' => (int) $product->get_id(), 'name' => $product->get_name(), 'manages_stock' => (bool) $product->managing_stock(), 'stock_quantity' => $product->get_stock_quantity(), 'in_stock' => (bool) $product->is_in_stock(), 'backorders' => (string) $product->get_backorders(), ); } /** * The product a Connect request names by id. * * Only a published or private parent product counts. A variation id, a * trashed product or another post type gives no match. * * @param mixed $product_id Value of line_items[].product_id, null when absent. * @return int[] The id, or no id. */ private static function find_connect_id( $product_id ) { if ( ! is_int( $product_id ) || $product_id < 1 ) { return array(); } $status = get_post_status( $product_id ); if ( 'product' !== get_post_type( $product_id ) || ! in_array( $status, array( 'publish', 'private' ), true ) ) { return array(); } return array( $product_id ); } /** * The variation a Connect request names under its parent product. * * The variation must be published or private and its parent must be the * given product_id (already checked). Anything else gives no match. * * @param int $product_id Parent product id. * @param mixed $variation_id Value of line_items[].variation_id. * @return int[] The id, or no id. */ private static function find_connect_variation_id( $product_id, $variation_id ) { if ( ! is_int( $variation_id ) || $variation_id < 1 ) { return array(); } if ( 'product_variation' !== get_post_type( $variation_id ) || ! in_array( get_post_status( $variation_id ), array( 'publish', 'private' ), true ) || (int) wp_get_post_parent_id( $variation_id ) !== $product_id ) { return array(); } return array( $variation_id ); } /** * Find every product or variation id that carries a SKU. * * wc_get_product_id_by_sku() returns only the first match, so it cannot see * a duplicate SKU. This reads postmeta itself and finds all of them. * * Product meta stays in postmeta on every WooCommerce version. HPOS moves * orders only, so this query is HPOS safe. * * @param string $sku SKU. * @return int[] Ids of published or private products and variations. */ private static function find_ids_by_sku( $sku ) { global $wpdb; if ( '' === $sku ) { return array(); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- read-only lookup of WooCommerce product meta by value; no cached API $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} AS p INNER JOIN {$wpdb->postmeta} AS m ON m.post_id = p.ID WHERE m.meta_key = '_sku' AND m.meta_value = %s AND p.post_type IN ('product','product_variation') AND p.post_status NOT IN ('trash','auto-draft') ORDER BY p.ID ASC LIMIT 5", $sku ) ); return array_map( 'intval', (array) $rows ); } /** * Resolve every line of an order. * * @param array $line_items Line items from the request payload. * @param bool $connect True when a Connect order key signed the request. * @return array { * @type bool $ok False when a SKU did not resolve. * @type string $unknown_sku The SKU that failed. * @type array[] $lines Resolved lines, in the order they came in. * } */ public static function resolve_lines( array $line_items, $connect = false ) { $lines = array(); foreach ( $line_items as $item ) { $sku = isset( $item['sku'] ) ? (string) $item['sku'] : ''; $id = $connect ? ( isset( $item['product_id'] ) ? $item['product_id'] : 0 ) : null; $vid = $connect && array_key_exists( 'variation_id', $item ) ? $item['variation_id'] : null; $resolved = self::resolve( $sku, $id, $vid ); if ( empty( $resolved['ok'] ) ) { return array( 'ok' => false, 'unknown_sku' => $sku, 'lines' => array(), ); } $resolved['quantity'] = isset( $item['quantity'] ) ? (int) $item['quantity'] : 0; $resolved['subtotal'] = isset( $item['subtotal'] ) ? (string) $item['subtotal'] : '0'; $resolved['total'] = isset( $item['total'] ) ? (string) $item['total'] : '0'; $resolved['sent_name'] = isset( $item['name'] ) ? (string) $item['name'] : ''; $lines[] = $resolved; } return array( 'ok' => true, 'unknown_sku' => '', 'lines' => $lines, ); } }