Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce Product Meta Widget
<?php
/**
* Plugin Name: WooCommerce Product Meta Widget
* Plugin URI: https://www.skyverge.com/blog/create-smart-woocommerce-widget/
* Description: Adds a widget to display product meta in a widget instead of on a page
* Author: SkyVerge
* Author URI: https://www.skyverge.com/
* Version: 1.0.0
* Text Domain: wc-product-meta
*
* Copyright: (c) 2015 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) 2015, 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 product meta in a widget instead of on the product page
*
* @since 1.0.0
* @extends \WP_Widget
*/
class WC_Product_Meta_Widget extends WP_Widget {
/**
* Setup the widget options
*
* @since 1.0.0
*/
public function __construct() {
// remove product meta from the page when this widget is used
add_action( 'wp', array( $this, 'remove_product_meta' ) );
add_action( 'wp_print_footer_scripts', array( $this, 'modify_product_meta_styles' ) );
// set widget options
$options = array(
'classname' => 'widget_wc_product_meta',
'description' => __( 'Displays product meta (SKU, categories, tags) on product pages.', 'wc-product-meta' ),
);
// instantiate the widget
parent::__construct( 'WC_Product_Meta_Widget', __( 'WooCommerce Product Meta', 'wc-product-meta' ), $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 meta
woocommerce_template_single_meta();
echo $args['after_widget'];
}
/**
* Update the widget title & selected product
*
* @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', 'wc-product-meta' ) ?>:</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 product meta from its current location on the product page
*
* @since 1.0.0
*/
public function remove_product_meta() {
if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
}
}
/**
* Add some specific CSS for the widget
*
* @since 1.0.0
*/
public function modify_product_meta_styles() {
if ( is_singular( 'product' ) && is_active_widget( false, false, $this->id_base ) ) {
echo '<style>
.widget_wc_product_meta .product_meta span.sku_wrapper,
.widget_wc_product_meta .product_meta span.posted_in,
.widget_wc_product_meta .product_meta span.tagged_as {
border-bottom: 1px dotted rgba(0,0,0,.1);
display: block;
padding: .53em 0;
}
</style>';
}
}
} // end \WC_Product_Meta_Widget class
/**
* Registers the new widget to add it to the available widgets
*
* @since 1.0.0
*/
function wc_product_meta_register_widget() {
register_widget( 'WC_Product_Meta_Widget' );
}
add_action( 'widgets_init', 'wc_product_meta_register_widget' );
@ercancoban

This comment has been minimized.

Copy link

commented Sep 7, 2018

How can we use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.