Instantly share code, notes, and snippets.
Last active
January 7, 2020 20:44
-
Save bekarice/84e2d8719543aec77059 to your computer and use it in GitHub Desktop.
WooCommerce Related Products Widget: on product pages, displays related items in the sidebar instead of below the product description.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Related Products Widget | |
* Plugin URI: http://swwp.co/6W | |
* Description: Adds a widget to display related product in the sidebar instead of below the product description | |
* Author: Sell with WP | |
* Author URI: https://www.sellwithwp.com/ | |
* Version: 1.0.0 | |
* Text Domain: woocommerce-related-products-widget | |
* | |
* Copyright: (c) 2016 SkyVerge, Inc. (info@skyverge.com) | |
* | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
* | |
* @author SkyVerge | |
* @copyright Copyright (c) 2016, SkyVerge, Inc. | |
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 | |
* | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
/** | |
* A widget for displaying related products in a widget instead of on the product page | |
* | |
* @since 1.0.0 | |
* @extends \WP_Widget | |
*/ | |
class WC_Related_Products_Widget extends WP_Widget { | |
/** | |
* Setup the widget options | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
// remove related products from the page when this widget is used | |
add_action( 'wp', array( $this, 'remove_related_products' ) ); | |
// adjust the display of related products when used in the widget | |
add_action( 'wp_print_footer_scripts', array( $this, 'modify_related_products_styles' ) ); | |
add_filter( 'woocommerce_output_related_products_args', array( $this, 'change_related_product_columns' ) ); | |
// set widget options | |
$options = array( | |
'classname' => 'widget_wc_related_products', | |
'description' => __( 'Displays related products when on product pages.', 'woocommerce-related-products-widget' ), | |
); | |
// instantiate the widget | |
parent::__construct( 'WC_Related_Products_Widget', __( 'WooCommerce Related Products', 'woocommerce-related-products-widget' ), $options ); | |
} | |
/** | |
* Render the widget | |
* | |
* @since 1.0 | |
* @see WP_Widget::widget() | |
* @param array $args | |
* @param array $instance | |
*/ | |
public function widget( $args, $instance ) { | |
// Only show this if we're looking at a product page | |
if ( ! is_singular( 'product' ) ) { | |
return; | |
} | |
// get the widget configuration | |
$title = $instance['title']; | |
echo $args['before_widget']; | |
if ( $title ) { | |
echo $args['before_title'] . wp_kses_post( $title ) . $args['after_title']; | |
} | |
// Show the product's related items | |
woocommerce_output_related_products(); | |
echo $args['after_widget']; | |
} | |
/** | |
* Update the widget title | |
* | |
* @since 1.0 | |
* @see WP_Widget::update() | |
* @param array $new_instance new widget settings | |
* @param array $old_instance old widget settings | |
* @return array updated widget settings | |
*/ | |
public function update( $new_instance, $old_instance ) { | |
$instance['title'] = strip_tags( $new_instance['title'] ); | |
return $instance; | |
} | |
/** | |
* Render the admin form for the widget | |
* | |
* @since 1.0.0 | |
* @see WP_Widget::form() | |
* @param array $instance the widget settings | |
* @return string|void | |
*/ | |
public function form( $instance ) { | |
?> | |
<p> | |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title', 'woocommerce-related-products-widget' ) ?>:</label> | |
<input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( isset( $instance['title'] ) ? $instance['title'] : '' ); ?>" /> | |
</p> | |
<?php | |
} | |
/** | |
* Removes the related products from their current location on the product page | |
* | |
* @since 1.0.0 | |
*/ | |
public function remove_related_products() { | |
if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) { | |
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 ); | |
} | |
} | |
/** | |
* Changes the number of related products columns when displayed in the widget | |
* | |
* @since 1.0 | |
* @param array $args arguments for related display | |
* @return array the updated arguments | |
*/ | |
public function change_related_product_columns( $args ) { | |
if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) { | |
$args['columns'] = 2; | |
} | |
return $args; | |
} | |
/** | |
* Add some specific CSS for the widget | |
* | |
* @since 1.0.0 | |
*/ | |
public function modify_related_products_styles() { | |
if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) { | |
echo '<style> | |
.woocommerce .widget_wc_related_products ul.products li.product, | |
.woocommerce-page .widget_wc_related_products ul.products li.product { | |
width: 48%; | |
margin-top: 1em; | |
} | |
</style>'; | |
} | |
} | |
} // end \WC_Related_Products_Widget class | |
/** | |
* Registers the new widget to add it to the available widgets | |
* | |
* @since 1.0.0 | |
*/ | |
function wc_related_products_register_widget() { | |
register_widget( 'WC_Related_Products_Widget' ); | |
} | |
add_action( 'widgets_init', 'wc_related_products_register_widget' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment