I've added a number field to the checkout page but it doesn't update the totals when selected and allow the order to be placed.
add_action( 'woocommerce_after_checkout_billing_form', 'donation_field' );
function donation_field( $checkout ) {
woocommerce_form_field( 'donation_amount', array(
'type' => 'number',
'required' => true,
'label' => 'Donate',
'description' => 'Please enter a donation amount',
), $checkout->get_value( 'donation_amount' ) );
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['donation_amount'] ) ) {
update_post_meta( $order_id, 'donation_amount', sanitize_text_field( $_POST['donation_amount'] ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'add_custom_fee', 10, 1 );
function add_custom_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = WC()->checkout->get_value('donation_amount');
$cart->add_fee( __( 'Donate Value', 'woocommerce' ) , $fee, false );
}
Related
I have added a DOB field to the WooCommerce My account section Account details. The field is a date field (date of birth).
The customer can edit and save. Problem is; the customer can edit and save over and over again.
This is a problem because of the discount that I would like to automatically apply based on the DOB date. If the customer can edit this field more than once; they can potentially get a discount each and every day. For obvious reasons, this cannot happen.
I need help in making the custom field editable ONCE and only once per customer. In wp-admin, admins can edit as they see fit - which is ok.
Here is my full code so far:
add_action( 'woocommerce_edit_account_form', 'dob_on_myaccount_form' );
function dob_on_myaccount_form() {
woocommerce_form_field( 'birthday_field', array (
'type' => 'date',
// how do I set the format to be y-m-d as in Year, Month, Day here
'label' => __('Date of birth', 'woocommerce' ),
'required' => false,
), get_user_meta( get_current_user_id(), 'birthday_field', true ) );
}
add_action( 'woocommerce_save_account_details_errors', 'dob_on_myaccount_form_error', 10, 1 );
function dob_on_myaccount_form_error( $args ) {
if (isset( $_POST['birthday_field'] ) && empty( $_POST['birthday_field'] ) ) {
$args->add('error', __( 'Please provide a date of birth', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details', 'dob_on_myaccount_form_save', 10, 1 );
function dob_on_myaccount_form_save( $user_id ) {
if ( isset( $_POST['birthday_field'] ) && !empty( $_POST['birthday_field'] ) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'give_birthday_discount', 10, 2 );
function give_birthday_discount( $cart, $date = 'now' ) {
if ( 'now' === $date ) {
$date = date( 'Y-m-d' );
$discount_percentage = 10;
$cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), -( $cart->subtotal * $discount_percentage / 100 ));
}
}
add_action( 'show_user_profile', 'dob_on_admin_profile', 10, 1 );
add_action( 'edit_user_profile', 'dob_on_admin_profile', 10, 1 );
function dob_on_admin_profile( $user ) { ?>
<h3><?php _e('Birthday','woocommerce'); ?></h3>
<table class="form-table">
<tr>
<th><label for="birthday_field"><?php _e('Date of Birth', 'woocommerce'); ?></label></th>
<td><input type="date" name="birthday_field" value="<?php echo esc_attr(get_the_author_meta('birthday_field', $user->ID)); ?>" class="regular-text" /></td>
</tr>
</table>
<br />
<?php
}
add_action( 'personal_options_update', 'dob_on_admin_profile_save', 10, 1 );
add_action( 'edit_user_profile_update', 'dob_on_admin_profile_save', 10, 1 );
function dob_on_admin_profile_save( $user_id ) {
if ( ! empty( $_POST['birthday_field'] ) ) {
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
}
}
While saving the date field you can save an extra value birthday_field_not_editable
Then you can check if this value is true, if this is the case, the field is no longer editable (readonly = true).
Explanation via comment tags added to the code
// Add field - my account
function dob_on_myaccount_form() {
// Label
$label = __( 'Date of birth', 'woocommerce' );
// Readonly (by default false)
$readonly = false;
// Get current user id
$user_id = get_current_user_id();
// Get value
$not_editable = get_user_meta( $user_id, 'birthday_field_not_editable', true );
// If TRUE
if ( $not_editable ) {
$label = __( 'Date of birth - adjustment is no longer possible', 'woocommerce' );
$readonly = true;
}
// Date field
woocommerce_form_field( 'birthday_field', array (
'type' => 'date',
'label' => $label,
'required' => true,
'custom_attributes' => array( 'readonly' => $readonly ),
), get_user_meta( $user_id, 'birthday_field', true ) );
}
add_action( 'woocommerce_edit_account_form', 'dob_on_myaccount_form', 10, 0 );
// Validate - my account
function dob_on_myaccount_form_error( $args ) {
if ( isset( $_POST['birthday_field'] ) && empty( $_POST['birthday_field'] ) ) {
$args->add('error', __( 'Please provide a date of birth', 'woocommerce' ) );
}
}
add_action( 'woocommerce_save_account_details_errors', 'dob_on_myaccount_form_error', 10, 1 );
// Save - my account
function dob_on_myaccount_form_save( $user_id ) {
if ( isset( $_POST['birthday_field'] ) && ! empty( $_POST['birthday_field'] ) ) {
// Update birthday field
update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
// Update not editable field
update_user_meta( $user_id, 'birthday_field_not_editable', true );
}
}
add_action( 'woocommerce_save_account_details', 'dob_on_myaccount_form_save', 10, 1 );
I am writing a WordPress plugin in which I need to add two radio buttons before the order total in the WooCommerce Order review section. I figured out how to add custom radio buttons to the order review section but I am unable to get the idea how to move delivery options just before the order total.
Please check the screenshot to understand what exactly I want to achieve.
Here's my code:
// Part 1 - Display Radio Buttons
add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice' );
function custom_checkout_radio_choice() {
$chosen = WC()->session->get( 'radio_chosen' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h3>Delivery Options</h3>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div>';
}
// Part 2 - Add Fee and Calculate Total
add_action( 'woocommerce_cart_calculate_fees', 'custom_checkout_radio_choice_fee', 20, 1 );
function custom_checkout_radio_choice_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$radio = WC()->session->get( 'radio_chosen' );
if ( $radio ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
// Part 3 - Add Radio Choice to Session
add_action( 'woocommerce_checkout_update_order_review', 'custom_checkout_radio_choice_set_session' );
function custom_checkout_radio_choice_set_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['radio_choice'] ) ){
WC()->session->set( 'radio_chosen', $output['radio_choice'] );
}
}
Please help me out with this.
To move your radio buttons before order total you will need to use another hook. But you can't have that delivery radio buttons on the fee total line…
I have simplified and revisited the code:
add_action( 'woocommerce_review_order_before_order_total', 'checkout_delivery_radio_buttons' );
function checkout_delivery_radio_buttons() {
echo '<tr class="delivery-radio">
<th>'.__("Delivery Options").'</th><td>';
$chosen = WC()->session->get( 'delivery' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'delivery' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
woocommerce_form_field( 'delivery', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'2.95' => '60 MINUTES: €2.95',
'0' => '24 - 48 HOURS',
),
), $chosen );
echo '</td></tr>';
}
add_action( 'woocommerce_cart_calculate_fees', 'checkout_delivery_fee', 20, 1 );
function checkout_delivery_fee( $cart ) {
if ( $radio = WC()->session->get( 'delivery' ) ) {
$cart->add_fee( 'Delivery Fee', $radio );
}
}
add_action( 'woocommerce_checkout_update_order_review', 'checkout_delivery_choice_to_session' );
function checkout_delivery_choice_to_session( $posted_data ) {
parse_str( $posted_data, $output );
if ( isset( $output['delivery'] ) ){
WC()->session->set( 'delivery', $output['delivery'] );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
To make this as simple and as easy as possible, I'm trying to add this checkbox on the top row next to the product type selector where you would normally find Virtual and Download.
The idea is to have the checkbox there so that no matter the product type, it is always available.
This is what I've tried:
add_action( 'woocommerce_product_type_options', 'remove_related_products_checkbox' );
function remove_related_products_checkbox() {
woocommerce_wp_checkbox( array(
'id' => '_remove_related_products',
'class' => '',
'label' => 'Remove Related Products?'
) );
}
add_action( 'save_post_product', 'related_products_checkbox_save' );
function remove_related_products_checkbox_save( $product_id ) {
global $pagenow, $typenow;
if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['_remove_related_products'] ) ) {
update_post_meta( $product_id, '_remove_related_products', $_POST['_remove_related_products'] );
} else
delete_post_meta( $product_id, '_remove_related_products' );
}
add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_checkbox_display', 1 );
function remove_related_products_checkbox_display() {
global $product;
if ( ! empty ( get_post_meta( $product->get_id(), '_remove_related_products', true ) ) ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
}
But it doesn't work… Any advice please?
Your code is a bit outdated since WooCommerce 3 and there are some mistakes.
Try the following instead:
add_filter( 'product_type_options', 'hide_related_products_option' );
function hide_related_products_option( $fields ) {
$fields['hide_related'] = array(
'id' => '_hide_related',
'wrapper_class' => '',
'label' => __('Remove Related Products'),
'description' => __( 'Remove/Hide related products.', 'woocommerce' ),
'default' => 'no'
);
return $fields;
}
add_action( 'woocommerce_admin_process_product_object', 'hide_related_products_option_save' );
function hide_related_products_option_save( $product ) {
$product->update_meta_data( '_hide_related', isset( $_POST['_hide_related'] ) ? 'yes' : 'no' );
}
add_action( 'woocommerce_after_single_product_summary', 'remove_related_products_checkbox_display', 1 );
function remove_related_products_checkbox_display() {
global $product;
if ( $product->get_meta('_hide_related') === 'yes' ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
Related: Add checkbox to product type option in Woocommerce backend product edit pages
In order to hide the Related Products section you can display a checkbox to disable related products. You just need to add the following snippet to your functions.php.
add_action( 'woocommerce_product_options_general_product_data', 'codeithub_add_related_checkbox_products' );
function codeithub_add_related_checkbox_products() {
woocommerce_wp_checkbox( array(
'id' => 'hide_related',
'class' => '',
'label' => 'Hide Related Products'
)
);
}
add_action( 'save_post_product', 'codeithub_save_related_checkbox_products' );
function codeithub_save_related_checkbox_products( $product_id ) {
global $pagenow, $typenow;
if ( 'post.php' !== $pagenow || 'product' !== $typenow ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['hide_related'] ) ) {
update_post_meta( $product_id, 'hide_related', $_POST['hide_related'] );
} else delete_post_meta( $product_id, 'hide_related' );
}
add_action( 'woocommerce_after_single_product_summary', 'codeithub_hide_related_checkbox_products', 1 );
function codeithub_hide_related_checkbox_products() {
global $product;
if ( ! empty ( get_post_meta( $product->get_id(), 'hide_related', true ) ) ) {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
}
I found this code on here earlier today and I tried modifying it but I get an error code when I try to add more emails to the code (it starts with 2) not sure what I'm doing wrong.
Here is the example
This is my modified Code (tried adding a third email to the bottom and I'm getting a php error)
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {
woocommerce_form_field( 'restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Select Location', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'4289 boul St-Jean' => __('4289 boul St-Jean', 'woocommerce' ),
'3559 boul St-Charles' => __('3559 boul St-Charles', 'woocommerce' ),
'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
)
), $checkout->get_value( 'restaurant_location' ));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['restaurant_location'] )
wc_add_notice( __( 'Please select a food option .' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_location'] ) ) {
update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
}
}
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
return $recipient;
}
This is the original Code I found
// Add the custom checkout field
add_filter( 'woocommerce_after_order_notes', 'restaurant_location_checkout_field' );
function restaurant_location_checkout_field( $checkout ) {
woocommerce_form_field( 'restaurant_location', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Food options', 'woocommerce'),
'required' => true,
'options' => array(
'' => __('Please select an option', 'woocommerce' ),
'New Orleans' => __('New Orleans', 'woocommerce' ),
'Baton Rouge' => __('Baton Rouge', 'woocommerce' )
)
), $checkout->get_value( 'restaurant_location' ));
}
// Process the checkout (checking)
add_action('woocommerce_checkout_process', 'restaurant_location_field_process');
function restaurant_location_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['restaurant_location'] )
wc_add_notice( __( 'Please select a food option .' ), 'error' );
}
// Update the order meta with field value
add_action( 'woocommerce_checkout_update_order_meta', 'restaurant_location_field_update_order_meta' );
function restaurant_location_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['restaurant_location'] ) ) {
update_post_meta( $order_id, '_restaurant_location', sanitize_text_field( $_POST['restaurant_location'] ) );
}
}
// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Food options', 'woocommerce').':</strong> ' . get_post_meta( $order->get_id(), '_restaurant_location', true ) . '</p>';
}
// Conditional Email recipient filter based on restaurant location
add_filter( 'woocommerce_email_recipient_new_order', 'conditional_email_recipient', 10, 2 );
function conditional_email_recipient( $recipient, $order ) {
$location = get_post_meta( $order->get_id(), '_restaurant_location', true );
$recipient = $location == 'New Orleans' ? ',test1#example.com' : ',test1#example.com';
return $recipient;
}
Change this line :
$recipient = $location == '4289 boul St-Jean' ? ',nicks#mtygroup.com' : ',nicsoti#yahoo.com' : ',nicks#mtygroup.com' ;
to :
switch ($location) {
case '4289 boul St-Jean':
$recipient = ',nicks#mtygroup.com';
break;
case '3559 boul St-Charles':
$recipient = ',nicsoti#yahoo.com';
break;
default:
$recipient = ',nicks#mtygroup.com';
break;
}
Or just modify it to suits your needs
I need to replace some character in my custom checkout field.
this is the whole code of my custom checkout field , (maybe we could use str_replace here)
/* Add the field to the checkout */
add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'phone_sabet', array(
'type' => 'tel',
'required' => true,
'clear' => true,
'class' => array('my-field-class form-row-first'),
'label' => __(''),
'placeholder' => __(''),
'description' => '',
), $checkout->get_value(('phone_sabet')));
echo '</div>';
}
this is the part of code when the custom field going to update
/* Update the order meta with field value */
add_action( 'woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
}
}
i tired to use str_replace and change it to below but no luck.
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
update_post_meta( $order_id, 'Phone', sanitize_text_field( $_POST['phone_sabet'] ) );
$getMeta = get_post_meta( get_the_ID(), 'Phone', true);
$newMeta = str_replace(array('۱'), '1', $getMeta);
update_post_meta(get_the_ID(), 'Phone', $newMeta);
}
}
and this is the part of when checkout field going to process. its okay if we could done it with str_replace here.
/* Process the checkout */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
if ( $_POST['phone_sabet'] )
// do something
}
The correct hook is woocommerce_checkout_update_order_meta, so you could try this:
## Save the order meta with custom field value
add_action( 'woocommerce_checkout_update_order_meta', 'custom_update_order_meta' );
function custom_update_order_meta( $order_id ) {
if ( ! empty( $_POST['phone_sabet'] ) ) {
// Replace before saving translating )
$phone_sabet = str_replace( array('۱'), array('1'), $_POST['phone_sabet'] );
update_post_meta( $order_id, 'phone', sanitize_text_field( $phone_sabet ) );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works