*/ public static function defaults() { return array( 'instock_first_enabled' => 'yes', 'instock_first_on_shop' => 'yes', 'instock_first_on_terms' => 'yes', 'instock_first_on_search' => 'yes', 'instock_first_on_short' => 'yes', 'instock_first_backorders' => 'instock', // instock | middle | last. ); } /** * Read an option with its default. * * @param string $key Option name. * @return string */ public static function get( $key ) { $defaults = self::defaults(); $value = get_option( $key, isset( $defaults[ $key ] ) ? $defaults[ $key ] : '' ); return is_string( $value ) ? $value : ''; } /** * @param string $key Option name. * @return bool */ public static function is_on( $key ) { return 'yes' === self::get( $key ); } /** Hook into WooCommerce settings. */ public static function init() { add_filter( 'woocommerce_get_sections_products', array( __CLASS__, 'add_section' ) ); add_filter( 'woocommerce_get_settings_products', array( __CLASS__, 'settings' ), 10, 2 ); } /** * @param array $sections Sections. * @return array */ public static function add_section( $sections ) { $sections[ self::SECTION ] = __( 'Sold-out order', 'instock-first' ); return $sections; } /** * @param array $settings Existing settings. * @param string $section Current section id. * @return array */ public static function settings( $settings, $section ) { if ( self::SECTION !== $section ) { return $settings; } $d = self::defaults(); $desc = __( 'In-stock products are listed first and sold-out products move to the end. Your normal sorting (default, popularity, price, newest…) is kept inside each group. When a product is restocked it moves back up automatically. Nothing in your catalog is changed: the order is applied when the page is shown.', 'instock-first' ); $out = array( array( 'title' => __( 'Sold-out products last', 'instock-first' ), 'type' => 'title', 'desc' => $desc, 'id' => 'instock_first_options', ), array( 'title' => __( 'Enable', 'instock-first' ), 'desc' => __( 'Move sold-out products to the end of product listings', 'instock-first' ), 'id' => 'instock_first_enabled', 'type' => 'checkbox', 'default' => $d['instock_first_enabled'], ), array( 'title' => __( 'Apply on', 'instock-first' ), 'desc' => __( 'Main shop page', 'instock-first' ), 'id' => 'instock_first_on_shop', 'type' => 'checkbox', 'default' => $d['instock_first_on_shop'], 'checkboxgroup' => 'start', ), array( 'desc' => __( 'Category, tag and attribute pages', 'instock-first' ), 'id' => 'instock_first_on_terms', 'type' => 'checkbox', 'default' => $d['instock_first_on_terms'], 'checkboxgroup' => '', ), array( 'desc' => __( 'Product search results', 'instock-first' ), 'id' => 'instock_first_on_search', 'type' => 'checkbox', 'default' => $d['instock_first_on_search'], 'checkboxgroup' => '', ), array( 'desc' => __( '[products] shortcode lists', 'instock-first' ), 'id' => 'instock_first_on_short', 'type' => 'checkbox', 'default' => $d['instock_first_on_short'], 'checkboxgroup' => 'end', ), array( 'title' => __( 'Backorder products', 'instock-first' ), 'id' => 'instock_first_backorders', 'type' => 'select', 'default' => $d['instock_first_backorders'], 'options' => array( 'instock' => __( 'Treat as in stock', 'instock-first' ), 'middle' => __( 'After in-stock, before sold-out', 'instock-first' ), 'last' => __( 'Treat as sold out', 'instock-first' ), ), ), array( 'type' => 'sectionend', 'id' => 'instock_first_options', ), ); return apply_filters( 'instock_first_settings', $out ); } /** Shown when WooCommerce is not active. */ public static function missing_woocommerce_notice() { if ( ! current_user_can( 'activate_plugins' ) ) { return; } echo '
' . esc_html__( 'InStock First needs WooCommerce to be installed and active.', 'instock-first' ) . '