Can't set the default value of some fields in checkout page - wordpress

I want to put user's previous data at the checkout page fields by default in my website which based on woocommerce. I cant set value for first name, last name and phone fields, although I can set value for other fields. How can I solve this problem?

Looking at the WC_Checkout class (/includes/class-wc-checkout.php), it contains a get_value method that is responsible for the default methods in the form.
This method has a filter woocommerce_checkout_get_value that you can use to override the default value that is loaded for each field.
Here's an example that overrides both the first name (both billing and shipping) and the last name:
add_filter('woocommerce_checkout_get_value', function( $value, $input ) {
// first check if user is logged in
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
switch ($input) {
case 'billing_first_name':
case 'shipping_first_name':
$value = 'Jack'; // fixed default
break;
case 'billing_last_name':
case 'shipping_last_name':
$value = $current_user->user_lastname; // use lastname from user profile
break;
default:
// don't change if we don't have substituting data
break;
}
}
return $value;
},10 ,2 );
In the same method there's also a filter default_checkout_{form_field} that would let you check existing values first (the former is a short-circuit that prevents the normal default from loading), ex:
add_filter('default_checkout_billing_first_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_billing_last_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_shipping_first_name', 'woocommerce_override_default_checkout_values', 10, 2 );
add_filter('default_checkout_shipping_last_name', 'woocommerce_override_default_checkout_values', 10, 2 );
function woocommerce_override_default_checkout_values( $value, $input ) {
// first check if user is logged in
if ( is_user_logged_in() ) {
// only load if value is empty
if (empty($value)) {
// read your own
$value = '...';
}
}
return $value;
}

Related

Which hook needs to call if admin update the shipping/billing address in WooCommerce?

I'm not sure which hook/action needs to setup to know when the admin is updating shipping/billing addresses once the order has been created.
So what I'm trying to achieve here is:
In WooCommerce order section when the admin updates the shipping/billing address then it triggers an action.
this action basically makes a single curl call to my custom script and lets me know that the address of the order has been changed by the admin.
I'll do some magic in my script.
I found below but I don't think its more from admin side.
// define the woocommerce_admin_order_data_after_shipping_address callback
function action_woocommerce_admin_order_data_after_shipping_address(
$delta_wccs_custom_checkout_details_pro_shipping, $int, $int ) {
// make action magic happen here...
};
// add the action
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 3 );
Please let me know if anyone knows the right action to trigger when order shipping/billing address change.
The woocommerce_admin_order_data_after_shipping_address hook is to display extra content on the order edit page (backend)
To trigger $order_item actions before or after saving to the DB, use:
/**
* Trigger action before saving to the DB. Allows you to adjust object props before save.
*
* #param WC_Data $this The object being saved.
* #param WC_Data_Store_WP $data_store THe data store persisting the data.
*/
function action_woocommerce_before_order_item_object_save( $order_item, $data_store ) {
// Get type
$data_type = $order_item->get_type();
// Before billing changes
if ( $data_type == 'billing' ) {
// Do..
}
// Before shipping changes
if ( $data_type == 'shipping' ) {
// Do..
}
}
add_action( 'woocommerce_before_order_item_object_save', 'action_woocommerce_before_order_item_object_save', 10, 2 );
/**
* Trigger action after saving to the DB.
*
* #param WC_Data $this The object being saved.
* #param WC_Data_Store_WP $data_store THe data store persisting the data.
*/
function action_woocommerce_after_order_item_object_save( $order_item, $data_store ) {
// Get type
$data_type = $order_item->get_type();
// After billing changes
if ( $data_type == 'billing' ) {
// Do..
}
// After shipping changes
if ( $data_type == 'shipping' ) {
// Do..
}
}
add_action( 'woocommerce_after_order_item_object_save', 'action_woocommerce_after_order_item_object_save', 10, 2 );
OR
Use the almost identical woocommerce_before_order_object_save hook that may be even more suitable, because via $order->get_changes() you can trigger/log/compare which $order data has been changed
function action_woocommerce_before_order_object_save( $order, $data_store ) {
// Get changes
$changes = $order->get_changes();
// Billing OR shipping
if ( isset( $changes['billing'] ) || isset( $changes['shipping'] ) ) {
// Do..
}
// OR even more specific (e.g.: shipping first name field was changed)
if ( isset( $changes['shipping_first_name'] ) ) {
// Do..
}
}
add_action( 'woocommerce_before_order_object_save', 'action_woocommerce_before_order_object_save', 10, 2 );
EDIT: it is a known issue that these hooks are called multiple times when they are not intended to be
See: https://github.com/woocommerce/woocommerce/issues/25771
As a workaround, add:
if ( did_action( 'replace_by_the_desired_hook_name' ) >= 2 ) return;
As first line in your callback function
// Define the woocommerce_admin_order_data_after_shipping_address callback .
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
// This hook will only fire in backend when viewing the order edit screen. Not for orders placed from checkout
};
// add the action
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );

woocommerce_before_order_object_save hook not work [duplicate]

Hello I'm trying to create a function in the mu-plugins to prevent certain users to change order status from specific order statuses to specific order statuses.
I've been looking everywhere and I have tried many different ways, but nothing seems to work.
Actually the function is running using woocommerce_order_status_changed action hook. The thing is that this hook runs after the order status has already been changed, what's causing an infinite loop.
The most useful hook I found seems to be woocommerce_before_order_object_save.
I found "Add an additional argument to prevent 'woocommerce_order_status_changed' from being called" useful related thread on WooCommerce Github.
I tried using #kloon code snippet solution:
add_filter( 'woocommerce_before_order_object_save', 'prevent_order_status_change', 10, 2 );
function prevent_order_status_change( $order, $data_store ) {
$changes = $order->get_changes();
if ( isset( $changes['status'] ) ) {
$data = $order->get_data();
$from_status = $data['status'];
$to_status = $changes['status'];
// Do your logic here and update statuses with CRUD eg $order->set_status( 'completed' );
// Be sure to return the order object
}
return $order;
}
but $changes variable is always an empty array.
I tried to use wp_insert_post_data Wordpress hook, but when I set:
$data['post_status'] = "some status";
it just prevents the whole update (the whole new data) from being saved.
This is the code I would like to run is:
function($data){
if($data['order_status'] == 'comlpeted' && $data['new_order_status'] == 'proccessing'){
// prevent the order status from being changed
$data['new_order_status'] = $data['order_status'];
}
few more if conditions...
return $data;
}
Any help or advise is appreciated.
Based on #kloon code snippet, I have been able to get the old order status and the new order status. Then I can disable any status change from a specific defined order status to a specific defined order status.
With the following code, specific defined user roles can't change order status from "processing" to "on-hold":
add_filter( 'woocommerce_before_order_object_save', 'prevent_order_status_change', 10, 2 );
function prevent_order_status_change( $order, $data_store ) {
// Below define the disallowed user roles
$disallowed_user_roles = array( 'shop_manager');
$changes = $order->get_changes();
if( ! empty($changes) && isset($changes['status']) ) {
$old_status = str_replace( 'wc-', '', get_post_status($order->get_id()) );
$new_status = $changes['status'];
$user = wp_get_current_user();
$matched_roles = array_intersect($user->roles, $disallowed_user_roles);
// Avoid status change from "processing" to "on-hold"
if ( 'processing' === $old_status && 'on-hold' === $new_status && ! empty($matched_roles) ) {
throw new Exception( sprintf( __("You are not allowed to change order from %s to %s.", "woocommerce" ), $old_status, $new_status ) );
return false;
}
}
return $order;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

WordPress prevent delete taxonomy

I would like to prevent that some categories are accidentally deleted. For this I use a meta entry for the category to be protected.
I use the following code for this:
// edit: wrong hook! ** add_action( 'delete_term_taxonomy', 'taxonomy_delete_protection', 10, 1 );
add_action( 'pre_delete_term', 'taxonomy_delete_protection', 10, 1 );
function taxonomy_delete_protection ( $term_id )
{
if (get_term_meta ($term_id, 'delete-protect', true) === true)
{
wp_die('Cannot delete this category');
}
}
Unfortunately, instead of my error message, only "Something went wrong" is displayed. Why?
Edit: The `delete_term_taxonomy` is the wrong hook for my code, because it deleted the meta before i can check the meta entry. `pre_delete_term` does fire before anything happens with the category.
The "Why" is because of the following JavaScript that ships with WordPress:
$.post(ajaxurl, data, function(r){
if ( '1' == r ) {
$('#ajax-response').empty();
tr.fadeOut('normal', function(){ tr.remove(); });
/**
* Removes the term from the parent box and the tag cloud.
*
* `data.match(/tag_ID=(\d+)/)[1]` matches the term ID from the data variable.
* This term ID is then used to select the relevant HTML elements:
* The parent box and the tag cloud.
*/
$('select#parent option[value="' + data.match(/tag_ID=(\d+)/)[1] + '"]').remove();
$('a.tag-link-' + data.match(/tag_ID=(\d+)/)[1]).remove();
} else if ( '-1' == r ) {
$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Sorry, you are not allowed to do that.' ) + '</p></div>');
tr.children().css('backgroundColor', '');
} else {
$('#ajax-response').empty().append('<div class="error"><p>' + wp.i18n.__( 'Something went wrong.' ) + '</p></div>');
tr.children().css('backgroundColor', '');
}
});
The expected response to this POST request is:
'1' if the term was deleted
'-1' if your user doesn't have permission to delete the term.
For all other cases, "Something went wrong" is displayed.
You are terminating the script early with wp_die, yielding an unexpected response, which comes under "other cases".
There isn't a way to provide a custom error message in the notice box here without writing some JavaScript of your own.
This is my current solution, not perfect but it works.
The "Something went wrong" message show up if you delete the taxonomy with the row action. So i unset the "delete" action so it couldn't be triggered this way.
add_filter ('category_row_actions', 'unset_taxonomy_row_actions', 10, 2);
function unset_taxonomy_row_actions ($actions, $term)
{
$delete_protected = get_term_meta ($term->term_id, 'delete-protect', true);
if ($delete_protected)
{
unset ($actions['delete']);
}
return $actions;
}
Then i hide the "Delete" Link in the taxonomy edit form with css. It's still could be triggered if you inspect the site and it's link, but there is no hook to remove this action otherwise.
add_action( 'category_edit_form', 'remove_delete_edit_term_form', 10, 2 );
function remove_delete_edit_term_form ($term, $taxonomy)
{
$delete_protected = get_term_meta ($term->term_id, 'delete-protect', true);
if ($delete_protected)
{
// insert css
echo '<style type="text/css">#delete-link {display: none !important;}</style>';
}
}
Finally the check before deleting the taxonomy. This should catch all other ways, like the bulk action "delete". I didn't found another way yet to stop the script from deleting the taxonomy.
add_action ('pre_delete_term', 'taxonomy_delete_protection', 10, 1 );
function taxonomy_delete_protection ( $term_id )
{
$delete_protected = get_term_meta ($term_id, 'delete-protect', true);
if ($delete_protected)
{
$term = get_term ($term_id);
$error = new WP_Error ();
$error->add (1, '<h2>Delete Protection Active!</h2>You cannot delete "' . $term->name . '"!');
wp_die ($error);
}
}
This solution provides a way to disable all categories from being deleted by a non Admin. This is for anyone like myself who's been searching.
function disable_delete_cat() {
global $wp_taxonomies;
if(!current_user_can('administrator')){
$wp_taxonomies[ 'category' ]->cap->delete_terms = 'do_not_allow';
}
}
add_action('init','disable_delete_cat');
The easiest solution (that will automatically take care of all different places where you can possibly delete the category/term) and in my opinion the most flexible one is using the user_has_cap hook:
function maybeDoNotAllowDeletion($allcaps, $caps, array $args, $user)
{
if ($args[0] !== 'delete_term') return $allcaps;
// you can skip protection for any user here
// let's say that for the default admin with id === 1
if ($args[1] === 1) return $allcaps;
$termId = $args[2];
$term = get_term($termId);
// you can skip protection for all taxonomies except
// some special one - let's say it is called 'sections'
if ($term->taxonomy !== 'sections') return $allcaps;
// you can protect only selected set of terms from
// the 'sections' taxonomy here
$protectedTermIds = [23, 122, 3234];
if (in_array($termId, $protectedTermIds )) {
$allcaps['delete_categories'] = false;
// if you have some custom caps set
$allcaps['delete_sections'] = false;
}
return $allcaps;
}
add_filter('user_has_cap', 'maybeDoNotAllowDeletion', 10, 4);

FacetWP filter from custom field checkbox

I got a product custom field named organic with the values yes/no. I want to show a checkbox in our filter that says "Organic". And when checked search for yes values in db. The deafult setup of a checkbox in FacetWP shows the values from db.
How do I set that up in FacetWP?
here is your code for this task, do not forget to re-index after each name change.
/**
* reindex after adding or updating this filter
*/
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'new_facet' == $params['facet_name'] ) { // change my_facet to your facet's name/slug
if ( '1' == $params['facet_value'] ) { // be careful with 1 and 0 as values - https://www.php.net/manual/en/types.comparisons.php
$params['facet_display_value'] = 'Organic'; // text to display in the facet choices
}
if ( '0' == $params['facet_value'] ) {
$params['facet_display_value'] = 'McDonalds';
}
}
return $params;
}, 10, 2 );

Get gravity forms fields

I am using the gravity form on my site. I am working on create the custom report for this I have need gravity form fields name and id based on specific form id.Please let me know how I can do it.
I am using the following function but it is showing all forms info based on it. it is looking very hard to parse it. Please let me know any function so I can get fields name easily.
$forms = RGFormsModel::get_forms_by_id(13);
try this
function get_all_form_fields($form_id){
$form = RGFormsModel::get_form_meta($form_id);
$fields = array();
if(is_array($form["fields"])){
foreach($form["fields"] as $field){
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
}
else if(!rgar($field, 'displayOnly')){
$fields[] = array($field["id"], GFCommon::get_label($field));
}
}
}
//echo "<pre>";
//print_r($fields);
//echo "</pre>";
return $fields;
}
It's not that hard to parse:
$fields=array();
foreach ( $forms['fields'] as $field ) {
$fields[]=array($field['id'] => $field['inputName']);
}
P.S. I'm assuming you use Gravity Forms < 1.7 as RGFormsModel::get_forms_by_id is a deprecated function since 1.7
// Get the Form fields
$form = RGFormsModel::get_form_meta($form_id);
// Run through the fields to grab an object of the desired field
$field = RGFormsModel::get_field( $form, $field_id );
I use the above to get a specific field I want to filter the value of. The $field contains an object with all the properties you want.
echo $field->label // Gets the label
echo $field->inputName // Gets the name
echo $field->type // Gets the type
echo $field->cssClass // Gets the CSS Classes as a string
You are able to get the entered value/content of a field by using rgpost() and by referencing the id ($field->id).
// Check the entered value of every field
foreach( $form['fields'] as &$field ) {
// Get the content for the specific field
$fieldContent = rgpost( "input_".$field->id );
// Check the content
if ( $fieldContent == ... ){}
}

Resources