Skip to content

Instantly share code, notes, and snippets.

@ebinnion
Created April 11, 2016 23:06
Show Gist options
  • Save ebinnion/3ecb5bf3295c43a5b2cc05a1a397f648 to your computer and use it in GitHub Desktop.
Save ebinnion/3ecb5bf3295c43a5b2cc05a1a397f648 to your computer and use it in GitHub Desktop.
An integration between Woocommerce and Revolution Sliders that allows adding a slider to the top of a product category.
<?php
/*
Plugin Name: Kareless Product Sliders
Plugin URI: https://kareless.com
Description: Allows you to add a Revolution slider to the top of a WooCommerce product archive
Version: 0.1.0
Author: Eric Binnion
Author URI: https://manofhustle.com
Text Domain: kareless-product-sliders
*/
class KarelessProductSliders {
function __construct() {
add_action( 'wp_loaded', array( $this, 'init' ) );
}
function init() {
// Since this plugin integrates Revolution Slider with WooCommerce product categories,
// make sure those plugins are activated.
if ( class_exists( 'WooCommerce' ) && class_exists( 'RevSlider' ) ) {
if ( is_admin() ) {
add_action( 'product_cat_add_form_fields', array( $this, 'add_slider_select' ) );
add_action( 'product_cat_edit_form_fields', array( $this, 'add_slider_select' ) );
add_action( 'created_term', array( $this, 'save_field' ) , 10, 3 );
add_action( 'edit_term', array( $this, 'save_field' ) , 10, 3 );
} else {
add_action( 'storefront_content_top', array( $this, 'display_slider' ), 20 );
}
}
}
function get_sliders() {
$rev_slider = new RevSlider();
return $rev_slider->getAllSliderAliases();
}
function get_cat_slider() {
$category = get_query_var( 'product_cat' );
$term = get_term_by( 'slug', $category, 'product_cat' );
return get_term_meta( $term->term_id, 'kareless_product_slider', true );
}
function add_slider_select( $term ) {
$slider_aliases = $this->get_sliders();
if ( empty( $slider_aliases ) ) {
return;
}
$selected = get_term_meta( $term->term_id, 'kareless_product_slider', true );
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="kareless-slideshow-select">
<?php _e( 'Select Slideshow', 'kareless-product-sliders' ); ?>
</label>
</th>
<td>
<select class="postform" id="kareless-slideshow-select" name="kareless-slideshow-select">
<option value="0" <?php selected( false, $selected ); ?>>
<?php _e( 'None', 'kareless-product-sliders' ); ?>
</option>
<?php foreach ( $slider_aliases as $alias ) : ?>
<option value="<?php echo esc_attr( $alias ); ?>" <?php selected( $alias, $selected ); ?>>
<?php echo esc_attr( $alias ); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description">
<?php _e(
'If a slider is selected, it will be shown on the product archive page.',
'kareless-product-sliders'
); ?>
</p>
</td>
</tr>
<?php
}
function save_field( $term_id, $tt_id = '', $taxonomy_slug = '' ) {
if ( isset( $_POST['kareless-slideshow-select'] ) && 'product_cat' === $taxonomy_slug ) {
$slider_aliases = $this->get_sliders();
if ( empty( $slider_aliases ) ) {
return;
}
update_term_meta( $term_id, 'kareless_product_slider', sanitize_text_field( $_POST['kareless-slideshow-select'] ) );
}
}
function display_slider() {
if ( ! is_product_category() ) {
return;
}
$slider = $this->get_cat_slider();
if ( ! empty( $slider ) && checkRevSliderExists( $slider ) ) { ?>
<div class="kareless-product-sliders" style="margin-bottom: 4.236em;">
<?php putRevSlider( $slider ); ?>
</div>
<?php }
}
}
$karelessProductSliders = new KarelessProductSliders();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment