I would like to prevent (make these fields readonly, for example) users from changing their billing information on the WooCommerce checkout form.
I'm currently using this code snippet:
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
function mycustom_woocommerce_billing_fields($fields)
{
$fields['billing_first_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_last_name']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_email']['custom_attributes'] = array('readonly'=>'readonly');
$fields['billing_phone']['custom_attributes'] = array('readonly'=>'readonly');
return $fields;
}
But the problem is: If the user has not filled in any of these fields in the registration, he is unable to insert his data in the checkout form because these fields are not editable.
My question is:
If the fields are not empty, how to make them readonly (or disabled)
Someone who can help me with this?
The answer of 7uc1f3r certainly works getting the user data… But since WooCommerce 3, you can use WC_Checkout get_value() dedicated method as follow:
add_filter( 'woocommerce_billing_fields', 'filter_wc_billing_fields', 10, 1 );
function filter_wc_billing_fields( $fields ) {
// On checkout and if user is logged in
if ( is_checkout() && is_user_logged_in() ) {
// Define your key fields below
$keys_fields = ['billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone'];
// Loop through your specific defined fields
foreach ( $keys_fields as $key ) {
// Check that a value exist for the current field
if( ( $value = WC()->checkout->get_value($key) ) && ! empty( $value ) ) {
// Make it readonly if a value exist
$fields[$key]['custom_attributes'] = ['readonly'=>'readonly'];
}
}
}
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this code to be also active in My account > Addresses > edit…, you will just have to remove is_checkout() && from the first IF statement.
I believe this is what you are looking for, comment with explanation added in code
function mycustom_woocommerce_billing_fields( $fields ) {
// Get current user
$user = wp_get_current_user();
// User id
$user_id = $user->ID;
// User id is found
if ( $user_id > 0 ) {
// Fields
$read_only_fields = array ( 'billing_first_name', 'billing_last_name', 'billing_email', 'billing_phone' );
// Loop
foreach ( $fields as $key => $field ) {
if( in_array( $key, $read_only_fields ) ) {
// Get key value
$key_value = get_user_meta($user_id, $key, true);
if( strlen( $key_value ) > 0 ) {
$fields[$key]['custom_attributes'] = array(
'readonly'=>'readonly'
);
}
}
}
}
return $fields;
}
add_filter('woocommerce_billing_fields', 'mycustom_woocommerce_billing_fields', 10, 1 );
Related
I cannot figure out what i am missing here.
I am creating a hidden field on the checkout page, that contains a value after a customer's choice.
This part is working, as i can see in the inspector on the checkout page.
The hidden field should be saved to the logged-in user, as i need it on another place in the website.
I have the following:
//This part is working!!
add_action( 'woocommerce_after_checkout_billing_form', function() {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
if( isset($values['programmakeuze']) ){
echo '<input type="hidden" name="programchoice" id="programchoice" class="input-hidden" value="'.$values['programmakeuze'].'">';
}
}
});
//Save hidden field to user
function elearning_checkout_update_user_meta( $customer_id, $posted ) {
if (!empty($_POST['programchoice'])) {
$program = intval($_POST['programchoice'] );
update_user_meta( $customer_id, 'programchoice', $program);
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'elearning_checkout_update_user_meta', 10, 2 );
function testing(){
$id = get_current_user_id();
$value = get_user_meta($id,'programchoice',true);
if ( !empty($value)) {
var_dump ($value);
}
}
add_action('wp_head','testing');
The $value returns nothing. What am i missing here?
I've partly rewritten your code. Including the use of woocommerce_checkout_update_customer action hook.
Also note the use of break in the for loop, as this is about a specific ID, and therefore about 1 unique field
However, I wouldn't use the wp_head action hook for debugging. See How to debug in WooCommerce instead.
But this should suffice, to answer your question:
// Display a custom hidden field after checkout billing form
function action_woocommerce_after_checkout_billing_form( $checkout ) {
// Loop through cart items
foreach( WC()->cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['programmakeuze'] ) ) {
echo '<input type="hidden" name="programchoice" id="programchoice" class="input-hidden" value="' . $cart_item['programmakeuze'] . '">';
break;
}
}
}
add_action( 'woocommerce_after_checkout_billing_form', 'action_woocommerce_after_checkout_billing_form', 10, 1 );
// Save/update user data from custom field value
function action_woocommerce_checkout_update_customer( $customer, $data ) {
// Isset
if ( isset( $_POST['programchoice'] ) ) {
$customer->update_meta_data( '_programchoice', sanitize_text_field( $_POST['programchoice'] ) );
}
}
add_action( 'woocommerce_checkout_update_customer', 'action_woocommerce_checkout_update_customer', 10, 2 );
// Debugging purposes
function action_wp_head(){
// Get user id
$user_id = get_current_user_id();
// Get user meta
$value = get_user_meta( $user_id, '_programchoice', true );
// NOT empty
if ( ! empty( $value ) ) {
var_dump ( $value );
}
}
add_action( 'wp_head', 'action_wp_head' );
I want to add a filter option in my custom post type with a custom field value which i am getting from ACF(advanced custom field), i tried a lot but unable to add filter in my admin columns here are some snippet i had tried but not succeed:
in my functions.php
add_filter('manage_edit-managemeetings_columns', 'extra_managemeetings_columns');
function extra_managemeetings_columns($columns){
$columns['meeting_year'] = "Meeting Year";
// echo $columns['date'];
return $columns;
}
add_action( 'manage_managemeetings_posts_custom_column', 'managemeetings_column', 10, 2);
function managemeetings_column( $column, $post_id ) {
// Image column
if ( 'meeting_year' === $column ) {
$meeting_data = get_field('meeting_date', $post_id);
echo date('Y',strtotime($meeting_data));
}
}
add_filter( 'manage_edit-managemeetings_sortable_columns', 'hcwdb_managemeetings_sortable_columns');
function hcwdb_managemeetings_sortable_columns( $columns ) {
$columns['meeting_year'] = 'meeting_date';
return $columns;
}
function filter_managemeetings( $query ) {
global $pagenow;
// Get the post type
//modify the query only if it admin and main query.
if( !(is_admin() AND $query->is_main_query()) ){
return $query;
}
//we want to modify the query for the targeted custom post and filter option
if( !('managemeetings' === $query->query['post_type'] AND isset($_REQUEST['meeting_date']) ) ){
return $query;
}
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : "";
if ( is_admin() && $pagenow=='edit.php' && $post_type == 'managemeetings' && isset( $_GET['meeting_date'] ) && $_GET['meeting_date'] !='all' ) {
$query->query_vars['meta_key'] = 'meeting_date';
$query->query_vars['meta_value'] = $_GET['meeting_date'];
$query->query_vars['meta_compare'] = 'LIKE';
var_dump($query);
// error_log(var_export($query, true));
}
}
add_action( 'restrict_manage_posts', 'filter_managemeetings') ;
my custom post type name is: managemeetings
my custom field key name is: meeting_date (which is set in ACF plugin)
if any one let me any idea any technique to filter my custom posts behalf of the my custom field value. or as in attachment if possible(if we click on meeting category it will filter the data related to that category as i want to apply on meeting year column)
kindly help me on this.
I need to change item product prices in Woocommerce Backend Order. I tried tu use the following hook, but I have a problem trying to obtain the order id. Any suggestion? Thanks in advance!
function my_custom_prices ($price, $product)
{
if ( !is_admin() || ( is_admin() && is_post_type_archive() ) ) return $price;
global $post, $woocommerce;
$order = new WC_Order( $post_id );
$user_id = $order->user_id;
$user = get_user_by('id', $user_id);
if ( in_array( 'my_role', (array) $user->roles ) ) {
return $price * 2;
}
else {
return $price;
}
}
add_filter('woocommerce_get_price', 'my_custom_prices ', 10, 2);
Complete problem:
The complete problem is as follows. I am using a plugin that adds a field to the product called wholesale price. If the customer has the wholesale customer role, the order uses those prices. The plugin works fine, but it does not take the price in the backend. I talked to the author and it's something they do not plan to change yet. My client needs to modify the orders. But when it enters the backend, it takes the common price, not the wholesaler. I need to do something in the backend that allows me to detect if the order is from a client with a wholesale customer role. If yes, take the correct price when adding products. There is more information on the discussion with the author here. https://wordpress.org/support/topic/wholesale-prices-in-backend-editing-orders/ Thank you very much for the help you can give me.
Options:
woocommerce_get_price: does not work because I cannot obtain the customer id
woocommerce_ajax_add_order_item_meta: nice option, but I could not find a sample
Button: nice option, but I do not know how can I change the price. I tryed the follow:
add_action( 'woocommerce_order_item_add_action_buttons', 'action_aplicar_mayoristas', 10, 1);
function action_aplicar_mayoristas( $order )
{
echo '<button type="button" onclick="document.post.submit();" class="button button-primary generate-items">Aplicar precios mayoristas</button>';
echo '<input type="hidden" value="1" name="aplicar_mayoristas" />';
};
add_action('save_post', 'aplicar_mayoristas', 10, 3);
function aplicar_mayoristas($post_id, $post, $update){
$slug = 'shop_order';
if(is_admin()){
if ( $slug != $post->post_type ) {
return;
}
if(isset($_POST['aplicar_mayoristas']) && $_POST['aplicar_mayoristas']){
$order = wc_get_order( $post_id);
//$order_id = $order->get_user_id();
// Iterating through each "line" items in the order
foreach ($order->get_items() as $item_id => $item_data) {
//$item_data->set_subtotal("798");
$item_data->set_price("798");
//->set_price($custom_price);
}
}
}
}
Updated
The hook that you are using is not for orders, but only for products, and is made to change the displayed prices only. So you will not get the order ID with it.
You could change the price display in many hooks, but if you want to change order item prices for real (not only the displayed formatted prices), you should trigger this prices changes when order is updated for example.
In this case you can use a custom function hooked in save_post action hook:
add_action( 'save_post', 'change_order_item_prices', 11, 1 );
function change_order_item_prices( $post_id ) {
// If this is an autosave (has not been submitted).
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user's permissions.
if ( 'shop_order' == $_POST[ 'post_type' ] ){
if ( ! current_user_can( 'edit_shop_order', $post_id ) )
return $post_id;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
## ------------------- Changing prices Start code ------------------- ##
## ===> HERE define the targeted user role
$user_role = 'my_role';
## ===> HERE define the rate multiplier (for price calculations)
$multiplier = 2;
// If this Order items prices have already been updated, we exit
$items_prices_updated = get_post_meta( $post_id, 'line_item_updated', true );
if( ! empty( $items_prices_updated ) ) return $post_id; // Exit
$order = new WC_Order( $post_id ); // Get the order object
$user_id = $order->get_user_id(); // Get the user ID
$user_data = get_userdata( $user_id ); // Get the user data
// Check the user role
if ( ! in_array( $user_role, $user_data->roles ) ) return;
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
$item_data = $item->get_data(); // The item data
$taxes = array();
foreach( $item_data['taxes'] as $key_tax => $values ){
if( ! empty( $values ) ){
foreach( $values as $key => $tax_price ){
$taxes[$key_tax][$key] = floatval($tax_price) * $multiplier;
}
}
}
$new_line_subtotal = floatval( $item_data['subtotal'] ) * $multiplier;
$new_line_subt_tax = floatval( $item_data['subtotal_tax'] ) * $multiplier;
$new_line_total = floatval( $item_data['total'] ) * $multiplier;
$new_line_total_tax = floatval( $item_data['total_tax'] ) * $multiplier;
// Update Order item prices
$item->set_subtotal($new_line_subtotal);
$item->set_subtotal_tax($new_line_subt_tax);
$item->set_total($new_line_total);
$item->set_total_tax($new_line_total_tax);
$item->set_taxes($taxes);
// Save the updated data
$item->save();
}
// Udpate order totals and cache
$order->calculate_totals();
// We mark the order as updated (to avoid repetition)
update_post_meta( $post_id, 'line_item_updated', true );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and and finally works.
I have added a security to avoid the order items to be updated twice.
The method $order->calculate_totals(); slow down the process a little bit… It's normal as it will calculate totals, update data and refresh caches.
Your code needs to be debugged.
$post_id - there is not such variable or parameter inside your function. So, use $post->ID instead.
do
var_dump( (array) $user->roles);
before the
if (in_array( 'my_role', (array) $user->roles ) )
line. And make sure that my_role exists in that array.
Temporary comment this line for debugging purpose:
// if (is_admin() && is_post_type_archive())
Then you will see the reason and able to be fix it.
I need to automatically add a product to the cart after user registration (which worked) but to decide which product to add by the user meta (which doesn't work).
The first action was just to add a product after registration and it worked perfectly:
add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 115;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
Now I need to add a specific product according to lists I have of users promoID I got, but it doesn't add anything to the cart.
example of the code:
add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$group1iid1 = array("1", "2", "3", "4");
$group1iid2 = array("5", "6", "7", "8");
if (in_array("2", $group1iid1)) {
$product_id = 115;
WC()->cart->add_to_cart( $product_id );
} elseif (in_array("0", $group1iid2)) {
$product_id = 219;
WC()->cart->add_to_cart( $product_id );
} else {
$product_id = 231;
WC()->cart->add_to_cart( $product_id );
}
}
}
If I take the code to a template file and just echo something instead of adding a product - it works ok, but when it's like this in the function.php > nothing happens.
What am I missing?
There are missing things in your code:
In your first condition you need also to add is_user_logged_in() condition, as I suppose that this code is for new registrated users only.
You need to get for the current user, HIS Promo ID value. I suppose that this value is set in user metadata, so to get this Promo ID value with get_user_meta() function, you have to define the correct meta_key.
In your code you have to replace in your conditions '2' and '0' values by the current user Promo ID… (Also elseif (in_array("0", $group1iid2)) { condition is going to be always false as "0" value doesn't exist in $group1iid2)
As I can't test for real all this, Here is some kind of work around, based on your code (without any guaranty):
add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart( $user_id ) {
if ( ! is_admin() && $user_id > 0 ) {
// DEFINE BELOW THE META KEY TO GET THE VALUE FOR YOUR GROUP OF CURRENT USER
$meta_key = 'your_group_meta_key';
// Getting the current user group ID
$user_promo_id = get_user_meta( $user_id, $meta_key, true );
$group1_id1 = array('1', '2', '3', '4');
$group1_id2 = array('5', '6', '7', '8');
if (in_array( $user_promo_id, $group1_id1 ) ) {
$product_id = 115;
} elseif (in_array( $user_promo_id, $group1_id2 ) ) {
$product_id = 219;
} else {
$product_id = 231;
}
WC()->cart->add_to_cart( $product_id );
}
}
I'm trying to disable a couple of payment gateways based on a user's role. The function & hook I found works on the Paypal method but not Amazon Payments Advanced. Here's my code:
function wk_disable_gateways( $available_gateways ) {
global $woocommerce;
$wholesale_cust = check_user_role( array( 'wholesale', 'orig-wholesale' ) );
if ( isset( $available_gateways['paypal'] ) && $wholesale_cust ) {
unset( $available_gateways['paypal'] );
}
if ( isset( $available_gateways['amazon_payments_advanced'] ) && $wholesale_cust ) {
unset( $available_gateways['amazon_payments_advanced'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'wk_disable_gateways' );
The "Pay with Amazon" code is still running on the checkout page. Any ideas?
This is not the best solution, but I have not been able to find a more viable answer.
First off I did what you did and disabled all gateways expect the one I wanted the user to use. In my case I only want someone to check out with the nmigateway.
This goes inside of your theme functions.php
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
foreach ( $gateways as $gateway_key => $gateway ) {
if ( $gateway_key !== 'nmipay' ) {
unset( $gateways[ $gateway_key ] );
}
}
}
}
return $gateways;
}
Next here is the part that makes this not the best solution editing the plugins source code.
Change the following two functions inside of the plugins/woocommerce-gateway-amazon-payments-advanced/amazon-payments-advanced.php
/**
* Checkout Button
*
* Triggered from the 'woocommerce_proceed_to_checkout' action.
*/
function checkout_button() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
?><div id="pay_with_amazon"></div><?php
}
}
/**
* Checkout Message
*/
function checkout_message() {
global $woocommerce;
// what products you wish to exculde
$nonPPproducts = array(1457, 1447, 479); // LIST YOUR PRODUCT IDS HERE
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
$disable_button = true;
}
}
if(!isset($disable_button) && $disable_button !== true ){
if ( empty( $this->reference_id ) ) {
echo '<div class="woocommerce-info info"><div id="pay_with_amazon"></div> ' . apply_filters( 'woocommerce_amazon_pa_checkout_message', __( 'Have an Amazon account?', 'woocommerce-gateway-amazon-payments-advanced' ) ) . '</div>';
}
}
}
Keep in mind that when the plugin is updated all of your changes will be lost.