has_gtin( $product ) ) { $proposals[] = array( 'field' => 'gtin', 'label' => __( 'GTIN / UPC / EAN', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => __( 'Not set', 'agentictrack-agent-readiness-for-woocommerce' ), 'after' => '', 'needs_input' => true, 'auto' => false, 'note' => __( 'Enter the product\'s real barcode number (8–14 digits). We never make one up.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } if ( '' === trim( (string) $product->get_sku() ) ) { $proposals[] = array( 'field' => 'sku', 'label' => __( 'SKU', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => __( 'Not set', 'agentictrack-agent-readiness-for-woocommerce' ), 'after' => '', 'needs_input' => true, 'auto' => false, 'note' => __( 'Enter your internal stock-keeping unit.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } return $proposals; } /** * {@inheritDoc} */ public function apply( WC_Product $product, array $input ) { $result = $this->result(); $dirty = false; // GTIN. if ( isset( $input['gtin'] ) && '' !== trim( (string) $input['gtin'] ) ) { $gtin = $this->sanitize_gtin( $input['gtin'] ); if ( '' === $gtin ) { return new WP_Error( 'arwc_invalid_gtin', __( 'That GTIN does not look valid. Enter 8 to 14 digits.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $before = $this->current_gtin( $product ); $this->set_gtin( $product, $gtin ); $dirty = true; $result->add_change( 'gtin', __( 'GTIN / UPC / EAN', 'agentictrack-agent-readiness-for-woocommerce' ), $before, $gtin ); } // SKU. if ( isset( $input['sku'] ) && '' !== trim( (string) $input['sku'] ) ) { $sku = sanitize_text_field( wp_unslash( $input['sku'] ) ); $before = (string) $product->get_sku(); // wc_product_has_unique_sku guards duplicates; set_sku() also validates. try { $product->set_sku( $sku ); } catch ( Exception $e ) { return new WP_Error( 'arwc_invalid_sku', __( 'That SKU is already in use by another product. Choose a unique value.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $dirty = true; $result->add_change( 'sku', __( 'SKU', 'agentictrack-agent-readiness-for-woocommerce' ), $before, $sku ); } if ( $dirty ) { $product->save(); } return $result; } /** * Whether the product already has a GTIN (native field or fallback meta). * * @param WC_Product $product Product. * @return bool */ protected function has_gtin( WC_Product $product ) { return '' !== $this->current_gtin( $product ); } /** * Read the current GTIN value (native field preferred). * * @param WC_Product $product Product. * @return string */ protected function current_gtin( WC_Product $product ) { if ( is_callable( array( $product, 'get_global_unique_id' ) ) ) { $native = trim( (string) $product->get_global_unique_id() ); if ( '' !== $native ) { return $native; } } $meta = $product->get_meta( self::GTIN_META_FALLBACK, true ); return is_string( $meta ) ? trim( $meta ) : ''; } /** * Write the GTIN to the best available field. * * @param WC_Product $product Product. * @param string $gtin Sanitized GTIN. * @return void */ protected function set_gtin( WC_Product $product, $gtin ) { if ( is_callable( array( $product, 'set_global_unique_id' ) ) ) { $product->set_global_unique_id( $gtin ); return; } $product->update_meta_data( self::GTIN_META_FALLBACK, $gtin ); } /** * Sanitize a GTIN: digits only, 8–14 length (GTIN-8/12/13/14, UPC, EAN). * * @param string $raw Raw input. * @return string Sanitized GTIN, or '' if invalid. */ protected function sanitize_gtin( $raw ) { $digits = preg_replace( '/\D+/', '', (string) wp_unslash( $raw ) ); $len = strlen( $digits ); if ( $len < 8 || $len > 14 ) { return ''; } return $digits; } }