add_section( ... ) ); add_filter( 'woocommerce_get_settings_products', $this->section_settings( ... ), 10, 2 ); add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, $this->render_panel( ... ) ); add_filter( 'woocommerce_admin_settings_sanitize_option_' . Settings::OPTION, $this->sanitize_wc_field( ... ), 10, 3 ); add_action( 'admin_enqueue_scripts', $this->enqueue_assets( ... ) ); } /** * URL of the settings screen. * * @return string */ public static function url(): string { return admin_url( 'admin.php?page=wc-settings&tab=products§ion=' . self::SECTION ); } /** * Adds the section to WooCommerce › Settings › Products. * * @param mixed $sections Sections. * @return array */ public function add_section( mixed $sections ): array { $sections = is_array( $sections ) ? $sections : array(); $sections[ self::SECTION ] = __( 'Add to cart text', 'add-to-cart-text' ); return $sections; } /** * Settings fields of the section. * * @param mixed $settings Settings of the current section. * @param mixed $current_section Current section slug. * @return array */ public function section_settings( mixed $settings, mixed $current_section = '' ): array { if ( self::SECTION !== $current_section ) { return is_array( $settings ) ? $settings : array(); } return array( array( 'id' => Settings::OPTION, 'type' => self::FIELD_TYPE, ), ); } /** * Sanitizes the panel value when WooCommerce saves the settings screen. * * WooCommerce has already verified the "woocommerce-settings" nonce and the * manage_woocommerce capability at this point. * * @param mixed $value Value after wc_clean() (ignored). * @param mixed $option Field definition (ignored). * @param mixed $raw_value Unslashed raw value. * @return array */ public function sanitize_wc_field( mixed $value = null, mixed $option = null, mixed $raw_value = null ): array { unset( $value, $option ); return $this->settings->sanitize( $raw_value ); } /** * Loads the admin stylesheet on the settings section and the product editor only. * * @param mixed $hook_suffix Current admin page. * @return void */ public function enqueue_assets( mixed $hook_suffix = '' ): void { if ( $this->is_product_editor( $hook_suffix ) || $this->is_settings_section( $hook_suffix ) ) { wp_enqueue_style( self::STYLE, plugins_url( 'assets/admin.css', PLUGIN_FILE ), array(), VERSION ); } } /** * Whether the current screen is the product editor. * * @param mixed $hook_suffix Current admin page. * @return bool */ private function is_product_editor( mixed $hook_suffix ): bool { return in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) && 'product' === get_current_screen()?->post_type; } /** * Whether the current screen is this settings section. * * @param mixed $hook_suffix Current admin page. * @return bool */ private function is_settings_section( mixed $hook_suffix ): bool { if ( 'woocommerce_page_wc-settings' !== $hook_suffix ) { return false; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only detection of the current screen. $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; $section = isset( $_GET['section'] ) ? sanitize_key( wp_unslash( $_GET['section'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Recommended return 'products' === $tab && self::SECTION === $section; } /** * Rows of the type matrix: slug => [label, available]. * * @return array */ private function type_rows(): array { $rows = array(); $settings = $this->settings->all(); foreach ( $this->settings->type_keys() as $key ) { $type = Product_Type::tryFrom( $key ); $has_value = '' !== implode( '', $settings['texts'][ $key ] ?? array() ); if ( $type && ! $type->is_available() && ! $has_value ) { continue; } $rows[ $key ] = array( 'label' => $type?->label() ?? $key, 'type' => $type, ); } return $rows; } /** * Renders the settings panel (custom WooCommerce field type). * * @param mixed $field Field definition (unused). * @return void */ public function render_panel( mixed $field = null ): void { unset( $field ); $settings = $this->settings->all(); $name = Settings::OPTION; $max = (string) Settings::MAX_LENGTH; ?>

label() ); ?>
type_rows() as $key => $row ) : ?>
value; /* translators: 1: product type, e.g. "Simple product", 2: "Product page" or "Shop & archives". */ $label = sprintf( __( '%1$s – %2$s', 'add-to-cart-text' ), $row['label'], $context->label() ); ?>

{products}', '{count}' ); ?>

  • {name}
  • {price}