Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JeroenSormani
Last active August 29, 2015 14:25
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 JeroenSormani/aa813b750a9f651502c2 to your computer and use it in GitHub Desktop.
Save JeroenSormani/aa813b750a9f651502c2 to your computer and use it in GitHub Desktop.
WooCommerce Custom Fields Front End
<?php
/**
* Plugin Name: WooCommerce Custom Product Fields Front End
* Plugin URI: http://jeroensormani.com
* Description: A simple demo plugin on how to add a custom product fields on the front end.
*/
/**
* Output custom WooCommerce fields.
*/
function output_custom_woocommerce_fields() {
$allow_personal_message = get_post_meta( get_the_ID(), '_allow_personal_message', true );
$gift_card_valid = get_post_meta( get_the_ID(), '_valid_for_days', true );
$gift_card_redeemable = get_post_meta( get_the_ID(), '_redeem_in_stores', true );
?><div class='custom_woocommerce_fields'><?php
if ( 'yes' === $allow_personal_message ) :
?><p>
<label><strong><?php _e( 'Personal message', 'woocommerce' ); ?></strong>&nbsp;<em><small><?php _e( '(optional)', 'woocommerce' ); ?></small></em>
<textarea name='personal_message' id='gift-card-personal-message' rows='2' cols='40'></textarea>
</label>
</p><?php
endif;
?><p class='gift-card-validity'>
<strong><?php _e( 'Gift card validity', 'woocommerce' ); ?></strong><br/><?php
echo sprintf( __( 'This gift card is valid for %d days', 'woocommerce' ), absint( $gift_card_valid ) );
?></p><?php
?><p class='gift-card-redeemable'>
<strong><?php _e( 'Redeemable in stores in', 'woocommerce' ); ?></strong><br/><?php
foreach ( $gift_card_redeemable as $store ) :
switch( $store ) :
case 'AL' :
_e( 'Alabama', 'woocommerce' );
break;
case 'NY' :
_e( 'New York', 'woocommerce' );
break;
case 'TX' :
_e( 'Texas', 'woocommerce' );
break;
case 'WY' :
_e( 'Wyoming', 'woocommerce' );
break;
endswitch;
// Add a ', ' if its not the last line
echo ( $store != end( $gift_card_redeemable ) ) ? ', ' : '';
endforeach;
?></p><?php
?></div><?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'output_custom_woocommerce_fields', 15 );
/**
* Add cart item meta.
*/
function woocommerce_custom_fields_add_cart_item_meta( $cart_item_data, $product_id, $variation_id ) {
if ( isset( $_POST['personal_message'] ) ) :
$cart_item_data['personal_message'] = sanitize_text_field( $_POST['personal_message'] );
endif;
$cart_item_data['gift_card_validity'] = absint( get_post_meta( $product_id, '_valid_for_days', true ) );
$cart_item_data['redeem_in_stores'] = get_post_meta( $product_id, '_redeem_in_stores', true );
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'woocommerce_custom_fields_add_cart_item_meta', 10, 3 );
/**
* Re-add the custom fields when getting the data via the session.
*/
function woocommerce_custom_fields_re_add_item_data_from_session( $cart_item_data, $values, $key ) {
if ( isset( $values['personal_message'] ) ) :
$cart_item_data['personal_message'] = $values['personal_message'];
endif;
if ( isset( $values['gift_card_validity'] ) ) :
$cart_item_data['gift_card_validity'] = $values['gift_card_validity'];
endif;
if ( isset( $values['redeem_in_stores'] ) ) :
$cart_item_data['redeem_in_stores'] = $values['redeem_in_stores'];
endif;
return $cart_item;
}
add_filter( 'woocommerce_get_cart_item_from_session', 'woocommerce_custom_fields_re_add_item_data_from_session', 10, 3 );
/**
* Display custom fields in the cart
*/
function woocommerce_custom_fields_display_meta_in_cart( $meta, $cart_item ) {
if ( isset( $cart_item['personal_message'] ) ) :
$meta[] = array(
'name' => __( 'Personal message', 'woocommerce' ),
'value' => $cart_item['personal_message'],
);
endif;
if ( isset( $cart_item['gift_card_validity'] ) ) :
$meta[] = array(
'name' => __( 'Gift card validity', 'woocommerce' ),
'value' => $cart_item['gift_card_validity'] . ' ' . __( 'days', 'woocommerce' ),
);
endif;
if ( isset( $cart_item['redeem_in_stores'] ) ) :
$meta[] = array(
'name' => __( 'Redeemable in stores in', 'woocommerce' ),
'value' => implode( ', ', $cart_item['redeem_in_stores'] ),
);
endif;
return $meta;
}
add_filter( 'woocommerce_get_item_data', 'woocommerce_custom_fields_display_meta_in_cart', 10, 2 );
/**
* Save cart item in the order.
*/
function woocommerce_custom_fields_add_order_item_meta( $item_id, $values, $cart_item_key ) {
error_log( print_r( $values, 1 ) );
if ( isset( $values['personal_message'] ) ) :
wc_add_order_item_meta( $item_id, '_personal_message', $values['personal_message'] );
endif;
if ( isset( $values['gift_card_validity'] ) ) :
wc_add_order_item_meta( $item_id, '_valid_for_days', $values['gift_card_validity'] );
endif;
if ( isset( $values['redeem_in_stores'] ) ) :
// Save a plain text as arrays are not displayed
wc_add_order_item_meta( $item_id, '_redeem_in_stores', implode( ', ', $values['redeem_in_stores'] ) );
endif;
}
add_action( 'woocommerce_add_order_item_meta', 'woocommerce_custom_fields_add_order_item_meta', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment