Skip to content

Instantly share code, notes, and snippets.

@zarankumar
Created October 6, 2015 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarankumar/05effe3fd7b604bde0d1 to your computer and use it in GitHub Desktop.
Save zarankumar/05effe3fd7b604bde0d1 to your computer and use it in GitHub Desktop.
// add a product type
add_filter( 'product_type_selector', 'wdm_add_custom_product_type' );
function wdm_add_custom_product_type( $types ){
$types[ 'wdm_custom_product' ] = __( 'Tour package' );
return $types;
}
add_action( 'plugins_loaded', 'wdm_create_custom_product_type' );
function wdm_create_custom_product_type(){
// declare the product class
class WC_Product_Wdm extends WC_Product{
public function __construct( $product ) {
$this->product_type = 'wdm_custom_product';
parent::__construct( $product );
// add additional functions here
}
}
}
// add the settings under ‘General’ sub-menu
add_action( 'woocommerce_product_options_general_product_data', 'wdm_add_custom_settings' );
function wdm_add_custom_settings() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Create a number field, for example for UPC
woocommerce_wp_text_input(
array(
'id' => 'wdm_upc_field',
'label' => __( 'UPC', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter Unique Product Code.', 'woocommerce' ),
'type' => 'number'
));
// Create a checkbox for product purchase status
woocommerce_wp_checkbox(
array(
'id' => 'wdm_is_purchasable',
'label' => __('Is Purchasable', 'woocommerce' )
));
echo '</div>';
}
add_action( 'woocommerce_process_product_meta', 'wdm_save_custom_settings' );
function wdm_save_custom_settings( $post_id ){
// save UPC field
$wdm_product_upc = $_POST['wdm_upc_field'];
if( !empty( $wdm_product_upc ) )
update_post_meta( $post_id, 'wdm_upc_field', esc_attr( $wdm_product_upc) );
// save purchasable option
$wdm_purchasable = isset( $_POST['wdm_is_purchasable'] ) ? 'yes' : 'no';
update_post_meta( $post_id, 'wdm_is_purchasable', $wdm_purchasable );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment