I've added an additional variation option to my products with the following code:
add_action('woocommerce_variation_options', 'he_add_to_variation_option', 10, 3);
function he_add_to_variation_option( $loop, $variation_data, $variation){
$is_trial = (get_post_meta($variation->ID, '_trialversion', true)) ? ' checked' : '';
?>
<label class="tips" data-tip="<?php esc_attr_e( 'Enable this option to make as a trial version', 'woocommerce' ); ?>">
<?php esc_html_e( 'Trial Version?', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_is_trial_version" name="_trialversion[<?php echo esc_attr( $variation->ID ); ?>]"<?php echo $is_trial;?>/>
</label>
<?php
}
add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields' );
function save_trialversion_option_fields( $post_id ) {
if ( isset( $_POST['_trialversion'] ) ){
foreach ( $_POST['_trialversion'] as $productid=>$checked ){
update_post_meta( $productid, '_trialversion', 'yes' );
}
}
}
This works great, it's displayed within the variants and it's saved correctly in the database.
So far, so good.
Now, I would like to add an additional checkbox at checkout, if a product is flagged as "trial version". I'm using the "Germanized" plugin as well, which has options for custom checkboxes, but I can't get it to recognize the changes I've made with the above code.
How would I accomplish the custom checkbox for my trial version variants? With or without Germanized, at this point I just want to get it to work. Maybe there's a free plugin, but if I can just do it by adding some code, that would probably be easier.
The checkbox would have to be a required one to complete the purchase of the trial version.
Hopefully someone has an idea on how to do this. Looking forward to your replies!
When a product variant has the "trialversion" enabled, a new checkbox will be added at the checkout page
woocommerce_save_product_variation should not contain a foreach loop, the 2nd parameter of the function already contains a counter $i
Normally the problem with the checkboxes should also be solved
function add_to_variation_option( $loop, $variation_data, $variation){
$is_trial = get_post_meta( $variation->ID, '_trialversion', true);
if ( $is_trial == 'yes' ) {
$is_trial = 'checked';
} else {
$is_trial = '';
}
?>
<label class="tips" data-tip="<?php esc_attr_e( 'Enable this option to make as a trial version', 'woocommerce' ); ?>">
<?php esc_html_e( 'Trial Version?', 'woocommerce' ); ?>
<input type="checkbox" class="checkbox variable_is_trial_version" name="_trialversion[<?php echo esc_attr( $loop ); ?>]"<?php echo $is_trial;?>/>
</label>
<?php
}
add_action('woocommerce_variation_options', 'add_to_variation_option', 10, 3);
function save_trialversion_option_fields( $variation_id, $i ) {
if ( !empty($_POST['_trialversion']) && !empty( $_POST['_trialversion'][$i] ) ) {
update_post_meta( $variation_id, '_trialversion', 'yes' );
} else {
update_post_meta( $variation_id, '_trialversion', 'no' );
}
}
add_action( 'woocommerce_save_product_variation', 'save_trialversion_option_fields', 10, 2 );
/**
* Add checkbox field to the checkout
**/
function my_custom_checkout_field( $checkout ) {
// Get $product object from Cart object
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item ) {
// The WC_Product object
$product = wc_get_product( $cart_item['product_id'] );
// Checks the product type, 'variable', returns boolean
if ( $product->is_type( 'variable' ) ) {
// Get variation id
$variation_id = $cart_item['data']->get_id();
// Get post meta
$trialversion = get_post_meta( $variation_id, '_trialversion', true);
// Found
if ( $trialversion == 'yes' ) {
$trialversion = 'found';
// Break loop
break;
}
}
}
// Found
if ( isset($trialversion) && $trialversion == 'found' ) {
echo '<div id="my-new-field">';
woocommerce_form_field( 'my_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I agree'),
'required' => true,
), $checkout->get_value( 'my_checkbox' ));
echo '</div>';
}
}
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field', 10, 1 );
Related
I'm struggling with a basic php function.
I implemented a backend checkbox in wordpress / woocommerce general tab which works fine:
// Add checkbox in Backend
function action_woocommerce_product_options_general_product_data() {
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_prevent_add_to_cart_button', // Required, it's the meta_key for storing the value (is checked or not)
'label' => __( 'Warenkorb Button', 'woocommerce' ), // Text in the editor label
'desc_tip' => false, // true or false, show description directly or as tooltip
'description' => __( 'Warenkorb Button ausblenden', 'woocommerce' ) // Provide something useful here
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset, yes or no
$checkbox = isset( $_POST['_prevent_add_to_cart_button'] ) ? 'yes' : 'no';
// Update meta
$product->update_meta_data( '_prevent_add_to_cart_button', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
Now i want to ask for the value of that checkbox and do things based on it.
It works in this function as expected:
// Add function for category / listing page add to cart button hook
function hide_listingpage_button( $add_to_cart_html, $product, $args ){
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
$before = '<span style="display:none;">';
$after = '</span>';
return $before . $add_to_cart_html . $after;
}
else {
return $add_to_cart_html;
}
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'hide_listingpage_button', 10, 3 );
but it does not work in this function(s):
// Add functions for singlepage before / after add to cart button hook
function beforehide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
function afterhide_singlepage_button( $product ) {
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
if ( $hide_add_to_cart_button == 'yes' ) {
echo 'test after';
}
}
add_action( 'woocommerce_before_add_to_cart_button', 'beforehide_singlepage_button' );
add_action( 'woocommerce_after_add_to_cart_button', 'afterhide_singlepage_button' );
Im my opinion im doing basically the same things?
I give the $product object as argument in the function, ask then for yes / no boolean with
$hide_add_to_cart_button = $product->get_meta( '_prevent_add_to_cart_button' );
and then echo something with the if clause.
however, i get no output of the page from that line, and also no error message.
What am i missing?
thank you!
I am using the code generated based on Add a custom multi-select field to admin product options settings in Woocommerce
// Display a multiselect field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_handles_product_field' );
function display_handles_product_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="handles_product"><?php _e( 'Handles Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_handles_product_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 );
function save_handles_product_field_value( $product ){
$data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array();
$product->update_meta_data( '_handles_product_ids', $data );
}
This code adds a custom search box and adds related products in the admin area when creating or editing a product.
I also created a new tab on the product page to show the added products.
/* New Handles Product Tab */
add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' );
function new_handles_product_tab( $tabs ) {
/* Add new tab */
$tabs['new_handles_product_tab'] = array(
'title' => __( 'Handles Product', 'woocommerce' ),
'priority' => 50,
'callback' => 'new_handles_product_tab_content'
);
return $tabs;
}
/* Display content in new tab */
function new_handles_product_tab_content() {
// Content
}
Tell me how to add these products to the new tab correctly? And is my code correct?
I will be glad for your help!
Using [products] shortcode with ids argument, the function displaying the product handles in a custom product tab will be:
function new_handles_product_tab_content() {
global $product;
$product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles
if( ! empty($product_ids) )
$product_ids = implode(',', $product_ids);
echo do_shortcode( "[products ids='$product_ids']" ); // Using [products] shortcode for display.
}
Untested it should work.
I have the below custom metakey which is an optin checkbox during checkout:
//1. ADD OPT IN OPTION IN CHECKOUT AND SAVE IN THE ORDER
// Add checkbox optin before T&Cs
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'marketing_opting_field' );
function marketing_opting_field() {
echo '<div id="marketing_opting_field">';
woocommerce_form_field( 'marketing_opting', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, sign me up'),
'default' => 1,
), WC()->checkout->get_value( 'marketing_opting' ) );
echo '</div>';
}
// Save the optin field in the order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 1 );
function custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['marketing_opting'] ) )
update_post_meta( $order_id, 'marketing_opting', $_POST['marketing_opting'] );
}
// Display the result of the checked optin in the order under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
$marketing_opting = get_post_meta( $order->get_id(), 'marketing_opting', true );
if( $marketing_opting == 1 )
echo '<p><strong>Has opted in for marketing purposes.</p>';
}
// 2. SHOW CUSTOM COLUMN FOR THE OPTIN OPTION
// Adding custom column title
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );
function custom_shop_order_column($columns)
{
$action_column = $columns['order_actions'];
unset($columns['order_actions']);
//add the new column "Opt in"
$columns['order_marketing'] = '<p align="center">Opted in?</p>'; // title
$columns['order_actions'] = $action_column;
return $columns;
}
// Add the data for each order
add_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id ){
$marketing_opting = get_post_meta( $post_id, 'marketing_opting', true );
if( $marketing_opting == 1)
switch($column){
case 'order_marketing' : echo '<p align="center"><span class="dashicons dashicons-yes"></span><span style="color: #F21891; font-weight: 600;">Signed Up</span></p>';
break;
}
}
So above is working and shows below the column, but I would like to have a filter in the admin bar and a search for Signed Up gives the desired result:
The search is not working, because the value of the checkbox for checked is 1 and does not resognise other words. I have added below, but it's not giving the result:
add_filter( 'woocommerce_shop_order_search_fields', 'marketing_search_fields', 10, 1 );
function marketing_search_fields( $meta_keys ){
$meta_keys[] = 'marketing_opting';
return $meta_keys;
}
The admin bar filter; the only related posts I could find are all about order statuses and not a custom metakey. I am not sure how to add this correctly, I started with the below, but there are obvious errors, and I am stuck.
add_filter( 'views_edit-shop_order' , 'marketing_opt_in_filter', 10, 1);
function marketing_opt_in_filter( $views ) {
$marketing_opting = get_post_meta( $post_id, 'marketing_opting', true );
if( $marketing_opting == 1)
$query_string = admin_url( 'edit.php?post_type=shop_order' ) ;
$query_string = add_query_arg( 'marketing_opting' , 'yes' , $query_string ) ;
$views[ 'marketing_opting' ] = 'Opted In (%s)' ;
return $views ;
}
I have revisited your existing code a bit and added a dropdown filter for the "marketing optin" custom field:
//1. ADD OPT IN OPTION IN CHECKOUT AND SAVE IN THE ORDER
// Add checkbox optin before T&Cs
add_action( 'woocommerce_checkout_before_terms_and_conditions', 'marketing_opting_field' );
function marketing_opting_field() {
echo '<div id="marketing_opting_field">';
woocommerce_form_field( 'marketing_opting', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Yes, sign me up'),
'default' => 1,
), WC()->checkout->get_value( 'marketing_opting' ) );
echo '</div>';
}
// Save the optin field as custom order meta, when checkbox has been checked
add_action( 'woocommerce_checkout_create_order', 'action_checkout_update_order_meta', 10, 2 );
function action_checkout_update_order_meta( $order, $data ) {
if( isset($_POST['marketing_opting']) )
$order->update_meta_data( '_marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );
}
// Save the optin field as custom user meta, when checkbox has been checked
add_action( 'woocommerce_checkout_update_customer', 'action_checkout_update_customer_meta', 10, 2 );
function action_checkout_update_customer_meta( $customer, $data ) {
if( isset($_POST['marketing_opting']) )
$customer->update_meta_data( 'marketing_opting', empty($_POST['marketing_opting']) ? 'no' : 'yes' );
}
// Display the result of the checked optin in the order under billing address
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_custom_field_on_order_edit_pages', 10, 1 );
function display_custom_field_on_order_edit_pages( $order ){
if( $order->get_meta( '_marketing_opting' ) === 'yes' )
echo '<p><strong>Has opted in for marketing purposes.</p>';
}
// 2. SHOW CUSTOM COLUMN FOR THE OPTIN OPTION
// Adding custom column title
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 12, 1 );
function custom_shop_order_column($columns)
{
$action_column = $columns['order_actions'];
unset($columns['order_actions']);
//add the new column "Opt in"
$columns['order_marketing'] = '<div align="center">' .__("Opted in?") . '</div>'; // title
$columns['order_actions'] = $action_column;
return $columns;
}
// Add the data for each order
add_action( 'manage_shop_order_posts_custom_column' , 'custom_order_list_column_content', 10, 2 );
function custom_order_list_column_content( $column, $post_id ){
global $post, $the_order;
if ($column ==='order_marketing') {
$value = $the_order->get_meta( '_marketing_opting' );
$label = $value === 'yes' ? __('Signed Up') : ucfirst($value);
$color = $value === 'yes' ? 'color:#00cc00;' : 'color:#bbbbbb;';
echo '<p align="center" style="'.$color.'"><span class="dashicons dashicons-'.$value.'"></span><span style="font-weight:600;">'.$label.'</span></p>';
}
}
// 3. Make marketing optin meta searchable from search field (can't work very well for 'yes' or 'no' values!)
// Make a custom meta field searchable from the admin order list search field
add_filter( 'woocommerce_shop_order_search_fields', 'marketing_search_fields', 10, 1 );
function marketing_search_fields( $meta_keys ){
$meta_keys[] = '_marketing_opting';
return $meta_keys;
}
// 4. Add a dropdown filter to get orders by marketing optin meta value
// Add a dropdown to filter orders by Marketing optin
add_action( 'restrict_manage_posts', 'display_admin_shop_order_marketing_opting_filter' );
function display_admin_shop_order_marketing_opting_filter(){
global $pagenow, $post_type;
if( 'shop_order' === $post_type && 'edit.php' === $pagenow ) {
$domain = 'woocommerce';
$current = isset($_GET['filter_shop_order_marketing'])? $_GET['filter_shop_order_marketing'] : '';
echo '<select name="filter_shop_order_marketing">
<option value="">' . __('Filter Marketing optin', $domain) . '</option>';
$options = ['yes' => __('Signed Up'), 'no' => __('No')];
foreach ( $options as $key => $label ) {
printf( '<option value="%s"%s>%s</option>', $key,
$key === $current ? '" selected="selected"' : '', $label );
}
echo '</select>';
}
}
// Process the filter dropdown for orders by Marketing optin
add_filter( 'request', 'process_admin_shop_order_marketing_opting_filter', 99 );
function process_admin_shop_order_marketing_opting_filter( $vars ) {
global $pagenow, $typenow;
if ( $pagenow == 'edit.php' && isset( $_GET['filter_shop_order_marketing'] )
&& $_GET['filter_shop_order_marketing'] != '' && 'shop_order' === $typenow ) {
$vars['meta_key'] = '_marketing_opting';
$vars['meta_value'] = wc_clean( $_GET['filter_shop_order_marketing'] );
}
return $vars;
}
Note: I have change the order meta_key to _marketing_opting starting with an underscore as most other existing metakeys...
Also I have added a function that register that "marketing optin" value in user meta data, as it will be used by checkout on WC()->checkout->get_value( 'marketing_opting' ) for customers that have already made an order.
Field validation (optional)
If you make this checkout field required, you will need field validation… Then add the following:
// Custom Checkout field validation
add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation() {
if ( isset($_POST['marketing_opting']) ) {
wc_add_notice( '<strong>'. __("Please select a value", "woocommerce") . '</strong> | '.$_POST['marketing_opting'], 'error' );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
I found the below code the other day, and now my client wants two more fields. If I add it again to my snippet plugin it says “Cannot redeclare function set_custom_edit_shop_order_columns.”. Could someone kindly help me to change the code with two more custom fields?
I have changed custom_column to courier_reference and want another text field tracking_number and one more is shipping_date with a date picker rather than a text box.
Any help is appreciated.
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );
Updated... You just need to have multiple different slugs in the same functions. For your desired 3 custom fields use the following:
// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['courier_reference'] = __( 'Courier reference', 'woocommerce' );
$columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
$columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'courier_reference' :
echo esc_html( get_post_meta( $post_id, 'courier_reference', true ) );
break;
case 'tracking_number' :
echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
break;
case 'shipping_date' :
echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
break;
}
}
// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_meta_box',
__( 'Tracking information', 'woocommerce' ),
'shop_order_content_callback',
'shop_order'
);
}
// For displaying metabox content
function shop_order_content_callback( $post ) {
// Textarea Field
$courier_reference = get_post_meta( $post->ID, 'courier_reference', true );
echo '<p>' . __( 'Courier reference', 'woocommerce' ) . '<br>
<textarea style="width:100%" id="courier_reference" name="courier_reference">' . esc_attr( $courier_reference ) . '</textarea></p>';
// Text field
$tracking_number = get_post_meta( $post->ID, 'tracking_number', true );
echo '<p>' . __( 'Tracking number', 'woocommerce' ) . '<br>
<input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';
// Date picker field
$shipping_date = get_post_meta( $post->ID, 'shipping_date', true );
echo '<p>' . __( 'shipping_date', 'woocommerce' ) . '<br>
<input type="date" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';
}
// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $post_id ) {
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['courier_reference'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'courier_reference', sanitize_textarea_field( $_POST['courier_reference'] ) );
}
// Make sure that 'tracking_number' it is set.
if ( isset( $_POST['tracking_number'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['shipping_date'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Based on "Add a checkbox on single product pages that adds an additional cost in Woocommerce" answer code, I am trying to add an "extra warranty" option to my products (checkbox in product page):
/*
* add warrenty
*/
// Backend: Additional pricing option custom field
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
woocommerce_wp_text_input( array(
'id' => '_warrenty_price',
'class' => 'wc_input_price short',
'label' => __( 'Warrenty', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')'
));
}
// Backend: Saving product pricing option custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_meta_data', 100, 1 );
function save_product_custom_meta_data( $product ){
if ( isset( $_POST['_warrenty_price'] ) )
$product->update_meta_data( '_warrenty_price', sanitize_text_field($_POST['_warrenty_price']) );
}
// Front: Add a text input field inside the add to cart form on single product page
add_action('woocommerce_single_product_summary','add_warrenty_price_option_to_single_product', 2 );
function add_warrenty_price_option_to_single_product(){
global $product;
if( $product->is_type('variable') || ! $product->get_meta( '_warrenty_price' ) ) return;
add_action('woocommerce_before_add_to_cart_button', 'product_option_custom_field', 30 );
}
function product_option_custom_field(){
global $product;
$active_price = (float) $product->get_price();
$warrenty_price = (float) $product->get_meta( '_warrenty_price' );
$warrenty_price_html = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $warrenty_price ) ) ) );
$active_price_html = wc_price( wc_get_price_to_display( $product ) );
$disp_price_sum_html = wc_price( wc_get_price_to_display( $product, array('price' => $active_price + $warrenty_price ) ) );
echo '<div class="hidden-field">
<p class="form-row form-row-wide" id="warrenty_option_field" data-priority="">
<span class="woocommerce-input-wrapper"><span class="war-title"> ' . __("Warrenty price:", "Woocommerce") .
'</span><label class="checkbox"><input type="checkbox" class="input-checkbox " name="warrenty_option" id="warrenty_option" value="1"> Add Warrenty for ' . $warrenty_price_html .
'</label></span></p>
<input type="hidden" name="warrenty_price" value="' . $warrenty_price . '">
<input type="hidden" name="active_price" value="' . $active_price . '"></div>';
// Jquery: Update displayed price
?>
<script type="text/javascript">
jQuery(function($) {
var cb = 'input[name="warrenty_option"]'
pp = 'p.price';
// On change / select a variation
$('form.cart').on( 'change', cb, function(){
if( $(cb).prop('checked') === true )
$(pp).html('<?php echo $disp_price_sum_html; ?>');
else
$(pp).html('<?php echo $active_price_html; ?>');
})
});
</script>
<?php
}
// Front: Calculate new item price and add it as custom cart item data
add_filter('woocommerce_add_cart_item_data', 'add_custom_product_data', 10, 3);
function add_custom_product_data( $cart_item_data, $product_id, $variation_id ) {
if (isset($_POST['warrenty_option']) && !empty($_POST['warrenty_option'])) {
$cart_item_data['new_price'] = (float) ($_POST['active_price'] + $_POST['warrenty_price']);
$cart_item_data['warrenty_price'] = (float) $_POST['warrenty_price'];
$cart_item_data['active_price'] = (float) $_POST['active_price'];
$cart_item_data['unique_key'] = md5(microtime().rand());
}
return $cart_item_data;
}
// Front: Set the new calculated cart item price
add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1);
function extra_price_add_custom_price($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach($cart->get_cart() as $cart_item) {
if (isset($cart_item['new_price']))
$cart_item['data']->set_price((float) $cart_item['new_price']);
}
}
// Front: Display option in cart item
add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);
function display_custom_item_data($cart_item_data, $cart_item) {
if (isset($cart_item['warrenty_price'])) {
$cart_item_data[] = array(
'name' => __("Extra Warrenty", "woocommerce"),
'value' => strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['warrenty_price'] ) ) ) )
);
}
return $cart_item_data;
}
It's work fine and the price is calculated in the payment.
The problem is that the field value doesn't appear in the order details table (and email notifications). So I don't have a way to know if the customer paid for the warranty or not (except calculated the final price of the product).
What should I add to the code so that the field will appear in the order details and mails?
You just need this little peace of code to display this warranty option everywhere:
// Save warranty as order item custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_warranty', 10, 4 );
function save_order_item_product_warranty( $item, $cart_item_key, $values, $order ) {
if( isset($values['warrenty_price']) && $values['warrenty_price'] > 0 ) {
$key = __("Extra Warrenty", "woocommerce");
$value = strip_tags( '+ '. wc_price( wc_get_price_to_display( $values['data'], array('price' => $values['warrenty_price']) ) ) );
$item->update_meta_data( $key, $value );
}
}
Code goes in function.php file of your active child theme (or active theme). tested and works.
In order received page (and all other order pages):
In admin order pages:
In email notifications:
add_action( 'woocommerce_admin_order_data_after_order_details', 'warrenty_price_order_meta_general' );
function warrenty_price_order_meta_general( $order ){ ?>
<br class="clear" />
<h4>Gift Order Edit</h4>
<?php
/*
* get all the meta data values we need
*/
$_warrenty_price = get_post_meta( $order->get_id(), '_warrenty_price', true );
?>
<div class="address">
<p><strong>Warranty</strong></p>
<?php
if( $_warrenty_price ) :
?>
<p><strong>Price:</strong> <?php echo $_warrenty_price ?></p>
<?php
endif;
?>
</div>
<?php } ?>
For Email
add_action('woocommerce_email_order_meta', 'warrenty_price_email_order_meta', 10, 3);
function warrenty_price_email_order_meta($order_obj, $sent_to_admin, $plain_text) {
$warrenty_price = get_post_meta($order_obj->get_id(), '_warrenty_price', true);
if (empty($warrenty_price))
return;
if ($plain_text === false) {
echo '<h2>Warranty</h2>
<ul>
<li><strong>Price:</strong> ' . $warrenty_price . '</li>
</ul>';
} else {
echo "Warranty\n
Price: $warrenty_price";
}
}