Skip to content

Instantly share code, notes, and snippets.

@GabrielGil
Last active August 29, 2015 14:06
Show Gist options
  • Save GabrielGil/3f5be7010fd3518a2ebb to your computer and use it in GitHub Desktop.
Save GabrielGil/3f5be7010fd3518a2ebb to your computer and use it in GitHub Desktop.
WooCoommerce Simple Product Tabs
<?php
/**
* GGWooCommerceProductTab class.
*
* Displays a new tab with an editor for every instance created and controls it saving.
*/
class GGWooCommerceProductTab
{
/**
* Variables
*
* Store some variables to avoid name mixing.
*/
private $name;
private $field_name;
private $anchor_id;
private $priority;
/**
* __construct function.
*
* Constructor sets variables and hooks functions with markup and the saving handler.
*
* @access public
* @param mixed $args
* @return void
*/
public function __construct ( $args )
{
// Needs an array of args to instanciate.
if ( !is_array( $args ) )
return;
// Set variables
$this->name = $args['name'];
$this->field_name = $args['field_name'];
$this->anchor_id = $args['anchor_id'];
$this->priority = $args['priority'] ? $args['priority'] : 100;
// Tab Title, content and saving handle.
add_action( 'woocommerce_product_write_panel_tabs', array( $this, 'outputTabTitle' ) );
add_action( 'woocommerce_product_write_panels', array( $this, 'outputTabEditContent' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'saveTab' ) );
// Display tabs in the front.
add_filter( 'woocommerce_product_tabs', array( $this, 'productTab' ) );
}
/**
* createTab function.
*
* Holds your tab title (Remember to add your own icon ;) )
*
* @access private
* @return void
*/
public function outputTabTitle ()
{
?>
<!-- The ID of the <a> tag is the link to the content. -->
<li class="custom_tab">
<a href="#<?= $this->anchor_id ?>"><?= $this->name ?></a>
</li>
<?php
}
/**
* outputTabEditContent function.
*
* Outputs the tab content
*
* @access private
* @return void
*/
public function outputTabEditContent ()
{
global $woocommerce, $post;
// This has to be the same ID as the <a> tag on the previous function
echo '<div id="' . $this->anchor_id . '" class="panel woocommerce_options_panel">';
// Just a div for grouping many fields
echo '<div class="options_group">';
// Get the current content of the field for giving it to the
// wp_editor. (Remember the ID. For saving, updating, displaying...)
$content = get_post_meta( $post->ID, $this->field_name, true );
wp_editor( $content, $this->field_name );
echo '</div>';
// Closes .options_group
echo '</div>';
// Closes #custom_tab_data
}
/**
* saveTab function.
*
* Saves the content of all the fields displayed on outputTabContent();
*
* @access public
* @return void
*/
public function saveTab ( $post_id )
{
$editor_content = $_POST[$this->field_name];
update_post_meta( $post_id, $this->field_name, $editor_content );
}
/**
* productTab function.
*
* Sets a new tab on the tabs array.
*
* @access public
* @param mixed $tabs
* @return void
*/
public function productTab ( $tabs )
{
global $post;
$content = get_post_meta( $post->ID, $this->field_name, true);
// Check if we have content, so we hide tabs with no content.
if ( $content ) :
$tabs[$this->field_name] = array(
'title' => $this->name,
'priority' => $this->priority,
'callback' => array( $this, 'outputProductTabContent' )
);
endif;
return $tabs;
}
/**
* outputProductTabContent function.
*
* Outputs the content of the tab.
*
* @access public
* @return void
*/
public function outputProductTabContent ()
{
global $post;
echo do_shortcode( get_post_meta( $post->ID, $this->field_name, true) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment