get_short_description() ) ); $long = trim( wp_strip_all_tags( (string) $product->get_description() ) ); if ( '' === $short && '' !== $long ) { $proposals[] = array( 'field' => 'short_description', 'label' => __( 'Short description (schema summary)', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => '', 'after' => $this->excerpt( $long ), 'needs_input' => false, 'auto' => true, 'note' => __( 'Derived from your existing long description.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } // Price: enter it right here (a real value, never invented). $price = $product->get_price(); if ( '' === (string) $price || ! is_numeric( $price ) || (float) $price <= 0 ) { $proposals[] = array( 'field' => 'price', 'label' => __( 'Price', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => __( 'Missing', 'agentictrack-agent-readiness-for-woocommerce' ), 'after' => '', 'needs_input' => true, 'auto' => false, 'input_type' => 'number', 'placeholder' => __( 'e.g. 29.99', 'agentictrack-agent-readiness-for-woocommerce' ), 'note' => __( 'Enter the product price so schema "offers" is complete.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } // Category: type a name; it is created if needed and assigned. if ( empty( $product->get_category_ids() ) ) { $proposals[] = array( 'field' => 'category', 'label' => __( 'Category', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => __( 'Missing', 'agentictrack-agent-readiness-for-woocommerce' ), 'after' => '', 'needs_input' => true, 'auto' => false, 'input_type' => 'text', 'placeholder' => __( 'e.g. Apparel', 'agentictrack-agent-readiness-for-woocommerce' ), 'note' => __( 'Type a category name — it will be created if it does not exist and assigned to this product.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } // Featured image lives in the media library. The Fix view opens the WordPress // media picker inline (media => true) so the merchant sets it without leaving // the page; the editor link stays as a graceful fallback if media JS is absent. if ( $product->get_image_id() <= 0 ) { $proposals[] = array( 'field' => 'image', 'label' => __( 'Featured image', 'agentictrack-agent-readiness-for-woocommerce' ), 'before' => __( 'Missing', 'agentictrack-agent-readiness-for-woocommerce' ), 'after' => '', 'needs_input' => false, 'auto' => false, 'media' => true, 'link' => admin_url( 'post.php?post=' . $product->get_id() . '&action=edit' ), 'link_label' => __( 'Set image in editor', 'agentictrack-agent-readiness-for-woocommerce' ), 'note' => __( 'Pick a featured image from your media library — no need to leave this page.', 'agentictrack-agent-readiness-for-woocommerce' ), ); } return $proposals; } /** * {@inheritDoc} */ public function apply( WC_Product $product, array $input ) { $result = $this->result(); // Merchant-entered price. if ( isset( $input['price'] ) && '' !== trim( (string) $input['price'] ) ) { $price = wc_format_decimal( wp_unslash( $input['price'] ) ); if ( '' === (string) $price || (float) $price <= 0 ) { return new WP_Error( 'arwc_bad_price', __( 'Enter a price greater than zero.', 'agentictrack-agent-readiness-for-woocommerce' ) ); } $before = (string) $product->get_regular_price(); $product->set_regular_price( (string) $price ); $product->save(); $result->add_change( 'price', __( 'Price', 'agentictrack-agent-readiness-for-woocommerce' ), '' === $before ? __( 'Missing', 'agentictrack-agent-readiness-for-woocommerce' ) : $before, (string) $price ); return $result; } // Merchant-entered category (created if new, then assigned). if ( isset( $input['category'] ) && '' !== trim( (string) $input['category'] ) ) { $name = sanitize_text_field( wp_unslash( $input['category'] ) ); $term = get_term_by( 'name', $name, 'product_cat' ); if ( $term instanceof WP_Term ) { $term_id = (int) $term->term_id; } else { $created = wp_insert_term( $name, 'product_cat' ); if ( is_wp_error( $created ) ) { return $created; } $term_id = (int) $created['term_id']; } $cats = $product->get_category_ids(); if ( ! in_array( $term_id, $cats, true ) ) { $cats[] = $term_id; $product->set_category_ids( $cats ); $product->save(); $result->add_change( 'category', __( 'Category', 'agentictrack-agent-readiness-for-woocommerce' ), __( 'Missing', 'agentictrack-agent-readiness-for-woocommerce' ), $name ); } return $result; } // Auto: derive the short description (schema summary) from the long one. $short = trim( wp_strip_all_tags( (string) $product->get_short_description() ) ); $long = trim( wp_strip_all_tags( (string) $product->get_description() ) ); if ( '' === $short && '' !== $long ) { $excerpt = $this->excerpt( $long ); if ( '' !== $excerpt ) { $product->set_short_description( $excerpt ); $product->save(); $result->add_change( 'short_description', __( 'Short description (schema summary)', 'agentictrack-agent-readiness-for-woocommerce' ), '', $excerpt ); } } return $result; } /** * Build a short, plain-text excerpt capped at EXCERPT_WORDS words. * * @param string $text Plain text. * @return string */ protected function excerpt( $text ) { return wp_trim_words( $text, self::EXCERPT_WORDS, '' ); } }