Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Move settings page to WooCommerce settings tab
Browse files Browse the repository at this point in the history
  • Loading branch information
Shellbot authored and Shellbot committed Jul 20, 2015
1 parent 599b018 commit b882a47
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 49 deletions.
18 changes: 12 additions & 6 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Replace the default WooCommerce category image placeholder with a relevant proje

== Description ==

By default, WooCommerce displays a neutral and uninteresting category image placeholder in those cases where a category has
By default, WooCommerce displays a neutral and uninteresting category image placeholder in those cases where a category has
no explicitly set thumbnail. This plugin simply replaces the placeholder image with a product image from that category.

User can choose whether to use a random product or the latest product from that category.
Expand All @@ -21,31 +21,37 @@ Feature requests welcome, please [post in the support forum](https://wordpress.o

**Usage**

There is no complicated setup, simply install the plugin and configure WooCommerce to display categories. Any categories containing products but without a
There is no complicated setup, simply install the plugin and configure WooCommerce to display categories. Any categories containing products but without a
category thumbnail will now display a product image.

To switch between random and latest product display, see the settings page under Settings > WC Auto Cat Thumbs.
To switch between random and latest product display, see the settings page under WooCommerce > Settings > Auto Category Thumbnails.

More options coming soon. [Plugin release page](http://codebyshellbot.com/wordpress-plugins/woocommerce-auto-category-thumbnails/ "WooCommerce Auto Category Thumbnails").

== Installation ==

1. Upload the 'woocommerce-auto-cat-thumbnails' folder to the '/wp-content/plugins/' directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. Change settings if required under 'Settings > WC Auto Cat Thumbs' in your WP admin panel
3. Change settings if required under 'WooCommerce > Settings > Auto Category Thumbnails' in your WP admin panel
4. Enjoy!

== Frequently Asked Questions ==

= My automatic category images are displaying at weird sizes? =

Make sure your settings are correct under WooCommerce > Settings > Products > Display
in your WP admin panel. You will need to regenerate your image sizes after changing these
Make sure your settings are correct under WooCommerce > Settings > Products > Display
in your WP admin panel. You will need to regenerate your image sizes after changing these
settings, or possibly on first install of the plugin. [Find the Regenerate Thumbnails plugin here](http://wordpress.org/plugins/regenerate-thumbnails/ "Regenerate Thumbnails").

== Changelog ==

= 1.1 =
* Moved settings page to WooCommerce settings tab

= 1.0 =
* First version

== Upgrade Notice ==

= 1.1 =
* Moved settings page to a more user-friendly place
129 changes: 86 additions & 43 deletions woocommerce-auto-cat-thumbnails.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: WooCommerce Auto Category Thumbnails
Plugin URI: http://codebyshellbot.com/wordpress-plugins/woocommerce-auto-category-thumbnails/
Description: Automatically display a relevant product image if category thumbnails are not set.
Version: 1.0
Version: 1.1
Author: Shellbot
Author URI: http://codebyshellbot.com
License: GPLv2 or later
Expand All @@ -28,21 +28,37 @@

class SB_WC_Auto_Category_Thumbnails {

/**
* Add our various hooks and filters as soon as possible.
*
* @since 1.0
*/
public function __construct() {
add_action( 'init', array( $this, 'load_settings' ) );
add_action( 'plugins_loaded', array( $this, 'remove_default' ) );
add_action( 'woocommerce_before_subcategory_title', array( $this, 'auto_category_thumbnail' ) );

add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'woocommerce_settings_tabs_sbo_wcact', array( $this, 'settings_tab' ) );
add_action( 'woocommerce_update_options_sbo_wcact', array( $this, 'update_settings' ) );

add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_tab' ), 50 );
}

/**
* Remove WooCommerce default action to replace with our own.
*
* @since 1.0
*/
public function remove_default() {
remove_action( 'woocommerce_before_subcategory_title', 'woocommerce_subcategory_thumbnail' );
}

/**
* Replace category placeholders with product images.
*
* @param object $cat
* @since 1.0
*/
public function auto_category_thumbnail( $cat ) {

//If a thumbnail is explicitly set for this category, we don't need to do anything else
if ( get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ) ) {
woocommerce_subcategory_thumbnail( $cat );
Expand All @@ -69,13 +85,15 @@ public function auto_category_thumbnail( $cat ) {
)
)
);


$wcact_settings = get_option( 'wcact_settings' );

//Random or latest image?
$query_args['orderby'] = $this->settings['orderby'];
$query_args['orderby'] = $wcact_settings['orderby'];

//Query DB
$product = get_posts( $query_args );

//If matching product found, check for a thumbnail, otherwise fall back
if( $product && has_post_thumbnail( $product[0]->ID ) ) {
echo get_the_post_thumbnail( $product[0]->ID, 'shop_thumbnail' );
Expand All @@ -84,45 +102,70 @@ public function auto_category_thumbnail( $cat ) {
return;
}
}

function add_settings_page() {
add_options_page( 'WC Auto Cat Thumbs', 'WC Auto Cat Thumbs', 'manage_options', 'wcact_settings', array( $this, 'show_settings_page' ) );
}

function show_settings_page() {
include( 'wcact-settings-page.php' );
/**
* Add plugin settings tab to WooCommerce settings page.
*
* @param array $settings_tab
* @return array $settings_tab
* @since 1.1
*/
function add_settings_tab( $settings_tabs ) {
$settings_tabs['sbo_wcact'] = __( 'Auto Category Thumbnails', 'wc-auto-category-thumbnails' );
return $settings_tabs;
}

function register_settings() {
register_setting( 'wcact_settings', 'wcact_settings' );

add_settings_section( 'wcact_global', 'Settings', '', 'wcact_settings' );
add_settings_field( 'orderby', 'Which product image should be displayed for each category?', array( &$this, 'field_orderby' ), 'wcact_settings', 'wcact_global' );
}

function load_settings() {
//Set defaults
$defaults = array(
'orderby' => 'rand',

/**
* Define fields for plugin settings tab.
*
* @since 1.1
*/
function get_settings() {

$settings = array(
'section_title' => array(
'name' => __( 'WC Auto Category Thumbnails', 'woocommerce-settings-tab-demo' ),
'type' => 'title',
'desc' => '',
'id' => 'wcact_settings[title]',
),
'orderby' => array(
'name' => __( 'Image to use', 'woocommerce-settings-tab-demo' ),
'type' => 'radio',
'desc' => __( 'Which product image should be displayed for each category?', 'woocommerce-settings-tab-demo' ),
'std' => 'rand',
'id' => 'wcact_settings[orderby]',
'options' => array(
'rand' => 'Random',
'date' => 'Latest',
),
),
'section_end' => array(
'type' => 'sectionend',
'id' => 'wcact_settings[sectionend]',
)
);

//Get existing settings, if any
$this->settings = (array) get_option( 'wcact_settings' );
return apply_filters( 'wcact_settings', $settings );
}

// Merge with defaults
$this->settings = array_merge( $defaults, $this->settings );
/**
* Add fields defined in get_settings() to plugin settings tab.
*
* @since 1.1
*/
function settings_tab() {
woocommerce_admin_fields( $this->get_settings() );
}

function field_orderby() {
$value = esc_attr( $this->settings['orderby'] );
?>
<label for="orderbyrand">Random</label>
<input type="radio" name="wcact_settings[orderby]" id="orderbyrand" value="rand" <?php echo ($value == 'rand' ? 'checked="checked"' : ''); ?>>
<label for="orderbydate">Latest</label>
<input type="radio" name="wcact_settings[orderby]" id="orderbydate" value="date" <?php echo ($value == 'date' ? 'checked="checked"' : ''); ?>>
<?php

/**
* Save settings from plugin tab.
*
* @since 1.1
*/
function update_settings() {
woocommerce_update_options( $this->get_settings() );
}

}

new SB_WC_Auto_Category_Thumbnails();
new SB_WC_Auto_Category_Thumbnails();

0 comments on commit b882a47

Please sign in to comment.