Yak e-commerce and Custom Post Types

Yak is an ecommerce plugin for WordPress. It treats posts as products using shortcodes like this [yak_buy] to place the buy now buttons wherever you want.

Since products are posts you can create (with a little conditional formatting) an online store in pretty much any layout and structure you want! But you can take it a step further and create a custom post type for products.

Custom Post Types

So here’s what it does:

  1. Creates a custom post type of Shop for you,
  2. Hides the yak shop options for normal posts and pages and applies them only in the Shop custom post type, and
  3. Stops product variations from appearing in normal posts (they normally get listed with post categories).

Example of yak commerce online store and wordpress custom post types in action

Copy-paste this into your WordPress theme’s functions.php file and you’re ready to roll. Functions.php is located in your theme folder.

<?php
// ---------------------------------------------------------------------------------------------------------
//
// Hide Yak Category
// - hides type/variations categories from the Categories dropdown in the dashboard
//  so that normal posts aren't flooded with variation categories.
//
// ---------------------------------------------------------------------------------------------------------
//*
function hide_yak_category() {
if ( function_exists( 'yak_get_option' ) ){
$yak_shop_category = yak_get_option(PRODUCT_CATEGORY_NAME, 'products');
if ($yak_shop_category){
$idObj = get_category_by_slug($yak_shop_category);
$yak_shop_id = $idObj->term_id;
$css = '<style>#category-'.$yak_shop_id.' {display:none !important;}</style>' . "\n";
print($css);
}
}
} //function
add_filter('admin_head', 'hide_yak_category');
//*/
// ---------------------------------------------------------------------------------------------------------
//
// Custom Shop Post Type
// - creates a custom post type called Shop
// - note that this new post type is referred to as "shop_products" -- change it if you like,
//  but update it throughout.
//
// ---------------------------------------------------------------------------------------------------------
// Shop
register_post_type('shop_products',
array(
'labels' => array(
'name' => __( 'Shop' ),
'singular_name' => __( 'Product' ),
'add_new' => __( 'Add Product' ),
'add_new_item' => __( 'Add Product' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Product' ),
'new_item' => __( 'New Product' ),
'view' => __( 'View Product' ),
'view_item' => __( 'View Product' ),
'search_items' => __( 'Search Products' ),
'not_found' => __( 'No Products found' ),
'not_found_in_trash' => __( 'No Products found in Trash' ),
),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'rewrite' => true,
'register_meta_box_cb' => 'custom_yak_meta_box',
'supports' => array(
'title',
'editor'
)
)
);
// ---------------------------------------------------------------------------------------------------------
//
// Custom Shop Post Type Categories
// - creates categories for our products
//
// ---------------------------------------------------------------------------------------------------------
register_taxonomy('shop_products-taxonomy',
array('shop_products'),
array(
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'rewrite' => array(
'slug' => 'topics',
'with_front' => true
),
'hierarchical' => true,
'query_var' => true,
'labels' => array(
'name' => __( 'Categories' ),
'singular_name' => __( 'Shop Category' ),
'search_items' => __( 'Search Shop Category' ),
'popular_items' => __( 'Popular Shop Topics' ),
'all_items' => __( 'Shop Categories' ),
'parent_item' => __( 'Parent Shop Category' ),
'parent_item_colon' => __( 'Parent Shop Category:' ),
'edit_item' => __( 'Edit Shop Categories' ),
'update_item' => __( 'Update' ),
'add_new_item' => __( 'Add New Shop Category' ),
'new_item_name' => __( 'New Shop Category' ),
)
)
);
register_taxonomy_for_object_type('shop_products', 'shop_products-taxonomy');
// ---------------------------------------------------------------------------------------------------------
//
// Yak Meta Box Magic
// - here we're first adding the yak options to our new custom post "shop_products".
// - then removing the yak options from the default post and page
//
// ---------------------------------------------------------------------------------------------------------
// used in the custom post type "register_meta_box_cb" callback to add yak
function custom_yak_meta_box() {
add_meta_box('yak','YAK Product Details','yak_editproduct','shop_products','advanced'); // !!!
}
// Remove yak box from posts and pages
function remove_yak_meta_box() {
remove_meta_box('yak','post','advanced');
remove_meta_box('yak','page','advanced');
}
add_action('admin_menu', 'remove_yak_meta_box');
Apr 28, 2011