Skip to content

Instantly share code, notes, and snippets.

@jaredwilli
Created October 31, 2010 11:54
Show Gist options
  • Save jaredwilli/656485 to your computer and use it in GitHub Desktop.
Save jaredwilli/656485 to your computer and use it in GitHub Desktop.
Products custom post type Class
<?php
// Initialize the Class and add the action
add_action('init', 'pTypesInit');
function pTypesInit() {
global $products;
$products = new TypeProducts();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* * * * * * * * * Best Products Post Type Class * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* Create a post type class for 'Product' posts
*/
class TypeProducts {
// Store the data
public $prod_meta_fields = array(
'title', 'description', 'product_type', 'post_tag',
'p_sku', 'p_price', 'p_discnt_price',
'p_weight', 'p_size_w', 'p_size_h',
'p_clr_1', 'p_scnt_1',
'p_clr_2', 'p_scnt_2',
);
private $p_sku;
private $p_price;
private $p_discnt_price;
private $p_weight;
private $p_size_w;
private $p_size_h;
// The post type constructor
public function TypeProducts() {
$productArgs = array(
'labels' => array(
'name' => __( 'Products', 'post type general name' ),
'singular_name' => __( 'Product', 'post type singular name' ),
'add_new' => __( 'Add New Product', 'product' ),
'add_new_item' => __( 'Add New Product' ),
'edit_item' => __( 'Edit Product' ),
'new_item' => __( 'New Product' ),
'view_item' => __( 'View Product' ),
'search_items' => __( 'Search Products' ),
'not_found' => __( 'No products found in search' ),
'not_found_in_trash' => __( 'No products found in Trash' ),
),
'public' => true,
'show_ui' => true,
'_builtin' => false,
'hierarchical' => false,
'menu_position' => 1,
'query_var' => 'product',
'capability_type' => 'post',
'exclude_from_search' => false,
'rewrite' => array( 'slug' => 'product' ),
'taxonomies' => array( 'post_tag','category' ),
'supports' => array( 'title','editor','excerpt','thumbnail','revisions','sticky' )
// 'page-attributes'
);
register_post_type( 'product', $productArgs );
/* Product Type Taxonomy */
$prod_tax = array(
'labels' => array(
'name' => __( 'Product Types', 'taxonomy general name' ),
'singular_name' => __( 'Product Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Product Types' ),
'popular_items' => __( 'Popular Product Types' ),
'all_items' => __( 'All Product Types' ),
'parent_item' => null, 'parent_item_colon' => null,
'edit_item' => __( 'Edit Product Type' ),
'update_item' => __( 'Update Product Type' ),
'add_new_item' => __( 'Add New Product Type' ),
'new_item_name' => __( 'New Product Type Name' ),
'separate_items_with_commas' => __( 'Separate product types with commas' ),
'add_or_remove_items' => __( 'Add or remove product type' ),
'choose_from_most_used' => __( 'Choose from the most used product types' )
),
'show_ui' => true,
'hierarchical' => true,
'show_tagcloud' => true,
'query_var' => 'product_type',
'rewrite' => array( 'slug' => 'product_type' )
);
register_taxonomy( 'product_type', 'product', $prod_tax );
// Initialize the methods
add_action( 'admin_init', array( &$this, 'product_meta_boxes' ));
add_action( 'template_redirect', array( &$this, 'template_redirect' ));
add_action( 'wp_insert_post', array( &$this, 'wp_insert_post' ), 10, 2 );
add_filter( 'manage_posts_custom_column', array( &$this, 'product_custom_columns' ));
add_action( 'manage_edit-product_columns', array( &$this, 'product_edit_columns' ));
}
// Create the columns and heading title text
public function product_edit_columns($columns) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Product Title',
'p_price' => 'Price',
'prod_type' => 'Product Type',
'post_tag' => 'Tags',
'thumbnail' => '',
);
return $columns;
}
// switching cases based on which $column we show the content of it
public function product_custom_columns($column) {
global $post, $p_price, $product_type;
$p_price = get_post_meta( $post->ID, 'p_price', true );
$product_type = get_post_meta( $post->ID, 'product_type', true );
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
print_r($term);
$termtax = print_r($term->taxonomy);
$termslug = print_r($term->slug);
switch ($column) {
case 'title' : the_title();
break;
case 'p_price' : echo $p_price;
break;
case 'prod_type' : echo $product_type;
break;
case 'post_tag' : ;
break;
case 'thumbnail' : ;
break;
}
}
/**
* Causes all pages to show a 404 page so commented out for now
*/
// Template redirect for custom templates
public function template_redirect() {
global $wp_query;
if ( $wp_query->query_vars['post_type'] == 'product' ) {
get_template_part( 'single-product' ); // a custom page-slug.php template
die();
}
}
// For inserting new 'product' post type posts
public function wp_insert_post($post_id, $post = null) {
if ($post->post_type == 'product') {
foreach ($this->prod_meta_fields as $key) {
$value = @$_POST[$key];
if (empty($value)) {
delete_post_meta($post_id, $key);
continue;
}
if (!is_array($value)) {
if (!update_post_meta($post_id, $key, $value)) {
add_post_meta($post_id, $key, $value);
}
} else {
delete_post_meta($post_id, $key);
foreach ($value as $entry) add_post_meta($post_id, $key, $entry);
}
}
}
}
/**
* Add product meta boxes
*/
function product_meta_boxes() {
add_meta_box( 'products-price', 'Product SKU# &amp; Price', array( &$this, 'prod_price_box' ), 'product', 'side', 'high' );
add_meta_box( 'products-colors', 'Product Colors', array( &$this, 'prod_colors_box' ), 'product', 'normal', 'high' );
add_meta_box( 'products-scents', 'Product Fragrances', array( &$this, 'prod_scents_box' ), 'product', 'normal', 'high' );
// add_meta_box( 'products-details', 'Product Weight &amp; Size', array( &$this, 'prod_details_box' ), 'product', 'normal', 'high' );
// add_filter( 'admin_head', array( &$this, 'metabox_scripts' ));
}
/**
* Admin product sku and price
*/
public function prod_price_box() {
global $post, $p_price, $p_discnt_price, $p_sku,
$p_weight, $p_size_w, $p_size_h;
$p_sku = get_post_meta( $post->ID, 'p_sku', true );
$p_price = get_post_meta( $post->ID, 'p_price', true );
$p_discnt_price = get_post_meta( $post->ID, 'p_discnt_price', true );
$p_weight = get_post_meta( $post->ID, 'p_weight', true );
$p_size_w = get_post_meta( $post->ID, 'p_size_w', true );
$p_size_h = get_post_meta( $post->ID, 'p_size_h', true );
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#FFCCCC">
<p>
<label for="p_price"><strong>Price $:</strong> (USD)<br />
<input id="p_price" size="8" name="p_price" value="<?php echo $p_price; ?>" /></label>
</p>
</td>
<td bgcolor="#FFCCCC">
<p>
<label for="p_discnt_price"><strong>Sale Price $:</strong><br />
<input id="p_discnt_price" size="8" name="p_discnt_price" value="<?php echo $p_discnt_price; ?>" /></label>
</p>
</td>
</tr>
<tr>
<td>
<p>
<label for="p_sku"><strong>SKU #:</strong><br />
<input id="p_sku" size="8" name="p_sku" value="<?php echo $p_sku; ?>" /></label>
</p>
</td>
<td>
<p>
<label for="p_weight"><strong>Weight:</strong><br />
<input id="p_weight" size="8" name="p_weight" value="<?php echo $p_weight; ?>" /> <strong>Oz.</strong></label>
</p>
</td>
</tr>
<tr>
<td colspan="2"><strong>Size:</strong></td>
</tr>
<tr>
<td>
<p>
<label for="p_size_w"><strong>Width</strong><br />
<input id="p_size_w" size="8" name="p_size_w" value="<?php echo $p_size_w; ?>" />
</label>
</p>
</td>
<td>
<p>
<label for="p_size_h"><strong>Height</strong><br />
<input id="p_size_h" size="8" name="p_size_h" value="<?php echo $p_size_h; ?>" /></label>
</p>
</td>
</tr>
</table>
<?php
} // end product price boxes
/**
* Product colors box
*/
public function prod_colors_box() {
global $post, $p_clr;
$p_clr = array();
$p_clrs = get_post_meta( $post->ID, 'p_clr', true );
?>
<div id="p_colors">
<p>
<label for="p_clr_1">
<strong>Color:</strong> <input id="p_clr_1" name="p_clr" value="<?php echo $p_clrs; ?>" />
</label>
</p>
</div>
<h4><a href="#" id="addClr">Add Another Color</a></h4>
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(function() {
var clrDiv = $('#p_colors');
var j = $('#p_colors p').size() + 1;
$('#addClr').live('click', function() {
$('<p><label for="p_clr_' + j + '"><strong>Color:</strong> <input id="p_clr_' + j + '" name="p_clr[]" size="20" value="<?php echo $p_clrs; ?>" /></label> <a href="#" id="remClr">Remove</a></p>').appendTo(clrDiv);
j++;
return false;
});
$('#remClr').live('click', function() {
if( j > 2 ) {
$(this).parents('p').remove();
j--;
} return false;
});
});
</script>
<?php
}
/**
* Product scents box
*/
public function prod_scents_box() {
global $post, $p_scnt;
$p_scnt = get_post_meta( $post->ID, 'p_scnt', true );
?>
<div id="p_scents">
<p>
<label for="p_scnt_1">
<strong>Scent:</strong> <input id="p_scnt_1" name="p_scnt" size="20" value="<?php echo $p_scnt; ?>" />
</label>
</p>
</div>
<h4><a href="#" id="addScnt">Add Another Scent</a></h4>
<?php
} // end product details box
function metabox_scripts() {
global $post, $p_clr, $p_scnt; ?>
<script src="http://code.jquery.com/jquery-1.4.3.min.js"></script>
<script type="text/javascript">
$(function() {
var clrDiv = $('#p_colors');
var j = $('#p_colors p').size() + 1;
$('#addClr').live('click', function() {
$('<p><label for="p_clr_' + j + '"><strong>Color:</strong> <input id="p_clr_' + j + '" name="p_clr" size="20" value="<?php echo $p_clr; ?>" /></label> <a href="#" id="remClr">Remove</a></p>').appendTo(clrDiv);
j++;
return false;
});
$('#remClr').live('click', function() {
if( j > 2 ) {
$(this).parents('p').remove();
j--;
} return false;
});
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').live('click', function() {
$('<p><label for="p_scnt_' + i + '"><strong>Scent:</strong> <input id="p_scnt_' + i + '" name="p_scnt" size="20" value="<?php echo $p_scnt; ?>" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
} return false;
});
});
</script>
<?php
}
/**
* Product details box
*/
public function prod_details_box() {
global $post, $p_weight, $p_size, $p_clr, $p_clr_sku, $p_scnt, $p_scnt_sku;
$p_weight = get_post_meta( $post->ID, 'p_weight', true );
$p_size_w = get_post_meta( $post->ID, 'p_size_w', true );
$p_size_h = get_post_meta( $post->ID, 'p_size_h', true );
?>
<div style="overflow:hidden;">
<p>
<span style="float:left;">
<label for="p_weight"><strong>Weight:</strong> (in ounces)<br />
<input id="p_weight" size="10" name="p_weight" value="<?php echo $p_weight; ?>" /> <strong>Oz.</strong></label></span>
<span style="float:right;">
<label for="p_size"><strong>Size:</strong> Width &times; Height<br />
<input id="p_size_w" size="10" name="p_size_w" value="<?php echo $p_size_w; ?>" /> &times;
<input id="p_size_h" size="10" name="p_size_h" value="<?php echo $p_size_h; ?>" /></label></span>
</p>
</div>
<?php
} // end product details box
} // end of TypeProducts{} class
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment