Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Add order item meta.
*/
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta' , 10, 2);
function add_order_item_meta ( $item_id, $values ) {
<?php
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
<?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'] ) );
<?php
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;
}
<?php
add_action( 'plugins_loaded', 'wcpt_register_gift_card_type' );
function wcpt_register_gift_card_type () {
class WC_Product_Gift_Card extends WC_Product {
public function __construct( $product ) {
$this->product_type = 'gift_card'; // name of your custom product type
parent::__construct( $product );
<?php
/**
* Simple custom product
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
<?php
add_filter( 'woocommerce_product_data_tabs', 'gift_card_tab' );
function gift_card_tab( $tabs) {
// Key should be exactly the same as in the class product_type
$tabs['gift_card'] = array(
'label' => __( 'Gift Card', 'wcpt' ),
'target' => 'gift_card_options',
'class' => ('show_if_gift_card'),
);
<?php
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',
<?php
/**
* Add data to cart item
*/
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
if ( isset( $_POST ['customer_name'] ) && isset( $_POST ['customer_message'] ) ) {
$custom_data = array() ;
<?php
/**
* Display custom data on cart and checkout page.
*/
add_filter( 'woocommerce_get_item_data', 'get_item_data' , 25, 2 );
function get_item_data ( $other_data, $cart_item ) {
if ( isset( $cart_item [ 'custom_data' ] ) ) {