Skip to content

Instantly share code, notes, and snippets.

@bhavik-kiri
Last active September 28, 2022 22:20
Show Gist options
  • Save bhavik-kiri/cbbe6c3a72e142e35d8858de7a7306d6 to your computer and use it in GitHub Desktop.
Save bhavik-kiri/cbbe6c3a72e142e35d8858de7a7306d6 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Add Gift Card as Custom Product Type
Description: A simple demo plugin on how to add Gift Card as your custom product type
Author: Bhavik Kiri
Version: 1.0
*/
add_action( 'plugins_loaded', 'wcpt_register_gift_card_type' );
function wcpt_register_gift_card_type () {
// declare the product class
class WC_Product_Gift_Card extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'gift_card';
parent::__construct( $product );
// add additional functions here
}
}
}
add_filter( 'product_type_selector', 'wcpt_add_gift_card_type' );
function wcpt_add_gift_card_type ( $type ) {
// Key should be exactly the same as in the class product_type
$type[ 'gift_card' ] = __( 'Gift Card' );
return $type;
}
add_filter( 'woocommerce_product_data_tabs', 'gift_card_tab' );
function gift_card_tab( $tabs) {
$tabs['gift_card'] = array(
'label' => __( 'Gift Card', 'wcpt' ),
'target' => 'gift_card_options',
'class' => ('show_if_gift_card'),
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'wcpt_gift_card_options_product_tab_content' );
function wcpt_gift_card_options_product_tab_content() {
// Dont forget to change the id in the div with your target of your product tab
?><div id='gift_card_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_checkbox( array(
'id' => '_enable_gift_card',
'label' => __( 'Enable Gift Card Product', 'wcpt' ),
) );
woocommerce_wp_text_input( array(
'id' => '_gift_card_price',
'label' => __( 'Price', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Enter Gift Card Price.', 'wcpt' ),
));
?></div>
</div><?php
}
add_action( 'woocommerce_process_product_meta', 'save_gift_card_options_field' );
function save_gift_card_options_field( $post_id ) {
$enable_gift_card = isset( $_POST['_enable_gift_card'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_enable_gift_card', $enable_gift_card );
if ( isset( $_POST['_gift_card_price'] ) ) :
update_post_meta( $post_id, '_gift_card_price', sanitize_text_field( $_POST['_gift_card_price'] ) );
endif;
}
add_action( 'woocommerce_single_product_summary', 'gift_card_template', 60 );
function gift_card_template () {
global $product;
if ( 'gift_card' == $product->get_type() ) {
$template_path = plugin_dir_path( __FILE__ ) . 'templates/';
// Load the template
wc_get_template( 'single-product/add-to-cart/gift_card.php',
'',
'',
trailingslashit( $template_path ) );
}
}
@Leonard02
Copy link

Please can you help to find what i am doing wrong because I am not getting the fields front end. I have test it only for price and is not working. Something is wrong with the path.

`add_action( 'plugins_loaded', 'wcpt_register_boiler_type' );

function wcpt_register_boiler_type () {

class WC_Product_Boiler extends WC_Product {

	public function __construct( $product ) {
		$this->product_type = 'boiler'; // name of your custom product type
		parent::__construct( $product );
		// add additional functions here
	}
	}

}

add_filter( 'product_type_selector', 'wcpt_add_boiler_type' );

function wcpt_add_boiler_type ( $type ) {
// Key should be exactly the same as in the class product_type
$type[ 'boiler' ] = __( 'Boiler' );
return $type;
}

add_filter( 'woocommerce_product_data_tabs', 'boiler_tab' );

function boiler_tab( $tabs) {
// Key should be exactly the same as in the class product_type
$tabs['boiler'] = array(
'label' => __( 'Boiler', 'wcpt' ),
'target' => 'boiler_options',
'class' => ('show_if_boiler'),
);
return $tabs;
}

//Adding the fileds of the boiler custom product type
add_action( 'woocommerce_product_data_panels', 'wcpt_boiler_options_product_tab_content' );

function wcpt_boiler_options_product_tab_content() {
// Dont forget to change the id in the div with your target of your product tab
?>

<?php

		woocommerce_wp_checkbox( array(
			'id' 	=> '_enable_boiler',
			'label' => __( 'Enable Gift Card Product', 'wcpt' ),
		) );

		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_price',
	       		'label'       => __( 'Price', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       		'description' => __( 'Enter Gift Card Price.', 'wcpt' ),
	       ));
		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_nr',
	       		'label'       => __( 'NRF nr.(Produktnummer)', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       ));
		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_product_name',
	       		'label'       => __( 'Product Name', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       ));
		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_volume',
	       		'label'       => __( 'Volume', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       ));
		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_capacity_people',
	       		'label'       => __( 'Capacity/Peeople', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       ));
		woocommerce_wp_text_input( array(
	       		'id'          => '_boiler_v40l',
	       		'label'       => __( 'V-40-L', 'wcpt' ),
	       		'placeholder' => '',
	       		'desc_tip'    => 'true',
	       ));
	?></div>
</div><?php

}

//save the settings of the product_type
add_action( 'woocommerce_process_product_meta', 'save_boiler_options_field' );

function save_boiler_options_field( $post_id ) {

$enable_boiler = isset( $_POST['_enable_boiler'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_enable_boiler', $enable_boiler );

if ( isset( $_POST['_boiler_price'] ) ) :
	update_post_meta( $post_id, '_boiler_price', sanitize_text_field( $_POST['_boiler_price'] ) );
endif;
if ( isset( $_POST['_boiler_nr'] ) ) :
	update_post_meta( $post_id, '_boiler_nr', sanitize_text_field( $_POST['_boiler_nr'] ) );
endif;
if ( isset( $_POST['_boiler_product_name'] ) ) :
	update_post_meta( $post_id, '_boiler_product_name', sanitize_text_field( $_POST['_boiler_product_name'] ) );
endif;
if ( isset( $_POST['_boiler_volume'] ) ) :
	update_post_meta( $post_id, '_boiler_volume', sanitize_text_field( $_POST['_boiler_volume'] ) );
endif;
if ( isset( $_POST['_boiler_capacity_people'] ) ) :
	update_post_meta( $post_id, '_boiler_capacity_people', sanitize_text_field( $_POST['_boiler_capacity_people'] ) );
endif;
if ( isset( $_POST['_boiler_v40l'] ) ) :
	update_post_meta( $post_id, '_boiler_v40l', sanitize_text_field( $_POST['_boiler_v40l'] ) );
endif;

}

add_action( 'woocommerce_single_product_summary', 'boiler_template', 60 );

function boiler_template () {

global $product;
if ( 'boiler' == $product->get_type() ) {

	$template_path = plugin_dir_path( __FILE__ ) . 'templates/';

	// Load the template
	wc_get_template( 'single-product/add-to-cart/boiler.php',
		'',
		'',
		trailingslashit( $template_path ) );
}

}`

`//Here it is the template code

get_id(), '_boiler_price' ); $price = 0; if ( isset( $get_price[0] ) ) { $price = wc_price( $get_price[0] ) ; } echo $price; ?>
single_add_to_cart_text() ); ?>

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment