I have a custom field in my product's page that is saved to database and it is also visible in my editor
I am trying to access it through API as a meta data and I am successful in updating that field.
The problem is that it doesn't automatically gets updated on my frontend and I have to click update (product) to get updated value in woocommerce.
I am using this code to save it to my wp database
function save_woocommerce_product_custom_fields($post_id)
{
$product = wc_get_product($post_id);
$custom_fields_woocommerce_title = isset($_POST['alt_points']) ? $_POST['alt_points'] : '';
$product->update_meta_data('alt_points', sanitize_text_field($custom_fields_woocommerce_title));
$product->save();
}
add_action('woocommerce_process_product_meta', 'save_woocommerce_product_custom_fields');
now I need this code as an api to automatically show it on frontend
Related
Is there any way to retrieve the clean unhashed password value in the WooCommerce checkout page with any hook?
What I need to do: I need to create a Firebase Auth user when a new WordPress user is creating. If this is not possible, what would be the best practice to achieve this?
What I tried
First I tried to create a new custom field on checkout and retrieve it with:
function wh_CustomReadOrder($order_id)
{
$order = wc_get_order($order_id);
WC()->session = new WC_Session_Handler;
/*
* Next lets create a customer so we can access checkout fields
* If you will check a constructor for WC_Customer class you will see
* that if you will not provide user to create customer it will use some
* default one. Magic.
*/
WC()->customer = new WC_Customer;
/*
* Done. You can browse all chceckout fields (including custom ones)
*/
?>
<script type="text/javascript">
var order = <?php echo $order ?>;
var checkout_fields = <?php echo json_encode(WC()->checkout->checkout_fields) ?>
var email = order;
console.log(checkout_fields);
</script>
<?php
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder');
I get an array with all fields, but my custom field is not showing. But even if so, the WordPress password will still be different. The best way would be to simply get the WordPress password and then create the user in Firebase.
Do you have any idea?
As you will see in the wc-user-functions.php file, the function wc_create_new_customer is used when creating a new account.
For checking the checkout page you can use Conditional Tags
is_checkout() Returns true on the checkout page.
So to intercept the unhashed password you could use the woocommerce_created_customer hook. The $unhashed_password variable will contain the unhashed password.
function action_woocommerce_created_customer ( $customer_id, $new_customer_data, $password_generated ) {
// Returns true on the checkout page.
if ( is_checkout() ) {
$unhashed_password = $new_customer_data['user_pass'];
}
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
One approach I would consider is to build a custom webform for creating the customer's account. That way you can manipulate the data however you want.
For example, when the user submits the form, take the data, register the new user in WC/WP, send the data to firebase, then redirect.
The downside is that you'll have to manage the process a 100% and deal with any possible errors.
Another way:
Use the default WC or WP account creation form, but on submit -> prevent Default with Javascript, take the data (yes you can access the password before it's hashed), send it to Firebase, THEN, submit the form and let WC/WP save it in the database in a normal fashion.
I did it like this when I needed to send that data to an Email Management software. The user enters the values, hits submit: my code blocks the submit event, sends the data where I want it to, then submits the form.
Hope it helps!
I have added several custom fields to my checkout that only appear when a user is checking out as a guest for the first time. My checkout process requires that the user create an account to complete checkout.
I have added four fields using the woocommerce_form_field method and then I have tried modifying the code provided in the answer here to achieve what I want. However, I have tried several checkouts, creating new accounts to check if the custom field values save into the new users' profile, but it doesn't seem to be working.
Here is one example of a solution I tried which did not work:
function reigel_woocommerce_checkout_update_user_meta( $customer_id) {
if ( ! empty( $_POST['practitioner_license_number'] ) ) {
$pln = sanitize_text_field( $_POST['practitioner-license-number'] );
update_user_meta($customer_id, 'practitioner_license_number', $pln);
}
}
add_action('woocommerce_created_customer', 'reigel_woocommerce_checkout_update_user_meta', 10, 2);
practitioner-license-number is my custom field added using the woocommerce_form_field method. I modified the code at the link above, so that the function runs on woocommerce_created_customer and then in update_user_meta I'm trying to pass the customer ID so that it saves the meta to the user profile that is created.
Would appreciate help trying to solve this. Most of the questions/answers on this subject assume that the user is logged in and already has an account, which is not the case here.
From the code you share, I think the problem lies in incorrectly passing to the $_POST,
perhaps practitioner_license_number vs practitioner-license-number.
You can use the code below to see if your hook works, if the value is written in the database you know that the problem is indeed elsewhere.
It might help to share the code you used for the custom fields too?
function action_woocommerce_created_customer( $customer_id, $new_customer_data, $password_generated ) {
$pln = 'test';
update_user_meta( $customer_id, 'practitioner_license_number', $pln);
}
add_action( 'woocommerce_created_customer', 'action_woocommerce_created_customer', 10, 3 );
I am doing an IONIC app and the project requires to create a post through the Wordpress API with custom fields (I am using ACF Pro and Aires ACF to REST API), I can create a normal post with the REST API, even tho, the ACF fields are not created or asiggned, they are created for the post only if I create the post in Wordpress admin interface.
I was reading the documentation for the ACF to REST API , but I see only edit and show capabilities. Does that mean the post meta have to exist? So how to "create" them for the post
Thanks in advance for the help
Best regards
phpadmin post creating:
absence of post meta:
$json = file_get_contents('PATH/TO/FILE.json');
function the_json_contents($json_to_get){
$json_in_array = json_decode($json_to_get, true); //json string to array
return $json_in_array;
}
function do_stuff_do_json() {
$json_array = the_json_contents($json);
foreach($json_array['id'] as $js) { //assuming we have an 'id' key
$somevariable = $js['someKey'];
$somevariable2 = $js['someOtherKey'];
}
}
Refer this solution
I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callbackĀ
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filterĀ
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();
I am developing my site and require to change post author id on the fly while publishing.
All post will be write and publish by admin but as content provided by different author and need to change post author id from admin to other author ( author id will get from custom field )
So how to do this on the fly while publishing.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.
$post_id = $post->ID; // change this to whathever
$user_id = '4'; // change this too
$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_author'] = $user_id;
wp_insert_post( $the_post );
The code above will update the currently looped through post (via $post->ID) with an author with ID of 4.
Read how wp_insert_post works here. Basically, if you pass it an ID of a post that already exists it will update that post with the new information you pass to it. (the author ID in this case)
If you want to grab things from the custom fields you can use:
$author = get_post_meta($this->ID, 'author_id', TRUE);
More info on custom fields.
Does the author info have to come from the custom field? Because you could do this straight in the UI manually.
Enable the "author" block in the display options when creating a post
Change the author in the drop-down menu
Here is my theme metabox.php code I am using wpalchemy metabox script and below code is in my cpt loop php file
// getting custom field value from image_artist
// this is giving value like artist_login_name / id
$png_gallery_meta->the_field('image_artist');
$artist_info = $png_gallery_meta->get_the_value();
// to separate artist_login_name and id
$string = $artist_info;
$artist = substr($string, 0, stripos($string, "/") );
// getting first name and last name from user id
$author_first_name = get_userdata(basename($string))->first_name;
$author_last_name = get_userdata(basename($string))->last_name;
So I am using above code to get user info from custom field dropdown selection box. Now how can I use above value to change id on the fly while admin publish the post the post author id should change with above id value get from selection box. I hope now it is more clear.