How to get the default user data to a bitrix form - forms-authentication

I am working on a bitrix site. A form was created linking into an information block that has a sorting of 100.
<? echo print_input($arParams, $arResult, 100) ;?>
I wanted to use the default user data like name, last name, email etc. of the current log user. How do I approach that?
Any help would much be appreciated.

On every page - $USER object is available - it contains information about current logged in user.
Contains this methods to retrieve info about current user:
GetID
GetLogin
GetEmail
GetFullName
GetFirstName
GetLastName
GetParam
GetUserGroupArray
GetUserGroupList
GetUserGroupString
Or you can use it like this
<?php
global $USER;
$rsUser = CUser::GetByID($USER->GetID());
$arUser = $rsUser->Fetch();
// print current user info
echo "<pre>"; print_r($arUser); echo "</pre>";
?>
here is more info about CUser class in official documentation (Russian)

Related

Get password value in WooCommerce checkout

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!

ACF User fileld for subscriber not return value

I'm using WordPress ACF plugin to store users' data.
The values I'm calling in template are as below:
echo $uid=get_current_user_id();
echo $lesson_order = get_field('lesson_order', 'user_'.$uid);
echo $last_lesson_time = get_field('last_lesson_time', "user_".$uid);
The values returned for admins are correct, but when logged in as a subscriber, the code returns empty values. I also checked user ids, which appear to be correct.
Can someone give me a solution to this. Thanks a lot.
I've tested both option on a local project and both were working but have you tried retrieving the user ID using this instead?
<?php
$current_user = wp_get_current_user();
echo "Current User ID " . $current_user->ID;
?>
If it still doesn't work, can you show a little more of your code?
You can also check that answer for more information : https://wordpress.stackexchange.com/questions/163407/get-current-user-id-returns-0

Get View Order URL for custom email in WooCommerce

I want to get the URL of the Order Details page from where user can see their order details, because using some 3rd party API I send mail to the customer after the product is shipped.
I tried this code
$order = wc_get_order(109);
$public_view_order_url = esc_url( $order->get_view_order_url() );
echo $public_view_order_url; //http://example.com/my-account/view-order/109/
but the URL generated by ^^ above code only works for logged in customer. Is it possible to get a Public URL so that user don't have to logged in because most of the customer don't have any account. I know for security reason email id and invoice number is needed.
One solution that I think is by creating an custom page which will accept order_id, order_key and email_id in GET parameter and query it and display the result; the whole thing I have to create but is there any WooCommerce function/hook for this?
I googled it and also spend time in Woo doc but the result was negative,
Any help or suggation will be very helpfull for me.
Thanks.
Not sure if I'm missing something, but it sounds like what you want can be achieved with the Order Received URL:
$order->get_checkout_order_received_url()
This would give you something like https://example.com/checkout/order-received/12345/?key=wc_order_1ab2cd3ef4g, which is a direct link to the order receipt shown after a successfully processed order.
This link will show billing & shipping details, order details & payment method by default, and doesn't require that the customer has an account or for them to be logged in.
I couldn't find a way to do that so I just put a condition in my email. If they have an account, link to the order, if they don't encourage them to make an account for the next order.
<p><?php
$email_order_url =$order->get_view_order_url();
if ( $order->get_user() ) {
_e( "Your message with <a href='" . $email_order_number . "'>Order
Number'</a>.", 'woocommerce' );
} else {
_e( "Message to encourage them to create an account on the regular
accounts page" , 'woocommerce' );
}
Everything you need is in post meta, this works for me when in a loop that can access get_the_ID():
<?php $the_order_id = get_the_ID();
$the_order_key = get_post_meta($the_order_id, '_order_key', true); ?>
https://yoursiteurl.com/checkout/order-pay/<?php echo the_order_id; ?>/?pay_for_order=true&key=<?php echo $the_order_key; ?>
Otherwise, if you can replaced get_the_ID() with the Order ID in some other way (however you're getting 109 in your initial question), that would work as well.

how can we get details from another database inside a plugin in wordpress

I created a plugin in wordpress to display earnings by using a shortcode.The details to display while using shortcode are stored in another database.I used direct database connection in plugin to fetch details from the that database.I used the following code
function earnings_shortcode($atts, $content, $tag)
{ //echo $atts[0];echo '<br>';
$str=base64_encode(1);
base64_decode($str);
$length = 4;
$res = trim(preg_replace("/[^0-9]/", "", $atts[0]));
$mydb1 = new wpdb('root','','db_test','localhost');
$rows = $mydb1->get_row("SELECT total,paydate FROM `tbl_shotcode` WHERE userid = $res", ARRAY_A);
echo "Payout on -" .$rows['paydate']; echo '<br/>';
echo "Total for next Pay Period:-" .$rows['total'];
}
Is there any better option to access another database inside a plugin with out hard coding the username and password.please suggest a solution.
No, you have to access the database and be authenticated to do queries. You can't get into a properly configured database without login in.
You can, however, make your $mydb1 variable global and define it at the top of your file (or in your constructor class) to be accessed by all your functions. I recommend putting it in a class to manage it more easily.

WordPress: get all meta data when user registers

I have a user registration form in the front end (in the Users admin section as well) with three extra fields (apart from default ones): birthday, country, language. their values are stored in usermeta table.
I have this action hook to retireve all meta data for the registered user:
add_action('user_register', 'new_user_func');
// user registration callback function
function new_user_func($userID) {
$newUser = get_user_meta( $userID );
$userMeta = array();
foreach ($newUser as $key => $value) {
$userMeta[$key] = $value[0];
}
//do something with $userMeta...
}
var_dump($userMeta) after submit doesn't give me the extra fields value though.. only defaults (first name, last name etc)
Anyone know what might be the case?
Did you try getting the values with:
$meta = get_the_author_meta($meta_key, $user_id);
Perhaps the meta values you add yourself isn't supported by get_user_meta() .
If this don't work either, perhaps you need to look on how you went about creating the new meta fields. Theres a pretty decent tutorial on how to do it here:
http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields
Read de Codex entry for user_register action, it says:
Not all user metadata has been stored in the database when this action is triggered.

Resources