Pass Woocommerce Session from admin to front-end - woocommerce

I try to pass datas from admin to front like :
WC()->frontend_includes();
WC()->session = new WC_Session_Handler();
WC()->session->set('datas', array(
'_email' => "email_here"
));
And retrieve them in front. For example here :
function action_woocommerce_before_checkout_form($wccm_autocreate_account)
{
$datas = WC()->session->get('datas');
var_dump($datas);
}
add_action('woocommerce_before_checkout_form', 'action_woocommerce_before_checkout_form', 10, 1);
add_action('woocommerce_before_cart_contents', 'action_woocommerce_before_checkout_form', 10, 1);
When I var_dump $datas in admin, I see my values but not on front

Related

How update ACF field in savepost hook and wp remote post

How I can update post meta or ACF fields by wp_remote_post or other method by using exists API ?
function publish_content($post_id, $post, $update)
{
// If this is just a revision, don't send the email.
if (wp_is_post_revision($post_id)) {
return;
}
$full_content = get_field('full_content', $post_id);
$login = 'username';
$password = 'password';
$response = wp_remote_post(
'https://example.com/wp-json/wp/v2/posts/12',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode("$login:$password")
)
)
);
//I need Update ACF Field Value in example.com site
}
add_action('save_post', 'publish_content', 11, 3);
I don't want to develop new API on the destination site and I want to use the existing (wp/v2/posts/{id}) WordPress API.

How to create a random username in Wordpress to override the original default on user registration

I'm trying to override the default Wordpress usernames on user registration with a more secure randomly generated id.
I need to update the username because I want to use this in a Zap based on another action to create a post with that username as the author, when they perform a specific action.
I have the below code, but it seems to only update the user nickname and not the username -
function generate_random_username($user_id) {
$random_username = wp_generate_password( 12, false, false );
$i = 1;
while (username_exists($random_username)) {
$random_username = wp_generate_password( 12, false, false ).$i;
$i++;
}
wp_update_user( array( 'ID' => $user_id, 'user_login' => $random_username ) );
}
add_action('user_register', 'generate_random_username');
Any help would be greatly appreciated - I am a beginner here!

How to auto select a value according to current post in wordpress?

I am trying to display auto select a value on a single-services.php page and services is my custom post type. on the single-services.php page I am having a select box that shows all the services posts title in the option created by the contact form 7 plugin. I wonder how to auto-select the option value according to the current service post visited by the user.
This is my function that adds all the service posts in the select box in functions.php
function add_posttype_to_CF7 ( $tag, $unused ) {
if ( $tag['name'] != 'services' ){
//continue only for the specific field - here we check by the name of the field
return $tag;
}
//get a list of all active services
$services = get_posts(array(
'post_type' => 'service',
'post_status' => 'publish',
'posts_per_page' => -1
));
foreach ( $services as &$services ) {
$tag['raw_values'][] = $services->post_title;
$tag['values'][] = $services->post_title;
$tag['labels'][] = $services->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'add_posttype_to_CF7', 10, 2);
I solved my issue with JQuery.
<script>
jQuery(document).ready( function($) {
var h1Text = "<?php echo $service_title ?>";
jQuery('#service_id[name="services"]').find('option[value="'+h1Text+'"]').prop("selected",true);
});</script>

How can I update Wordpress plugin with transient by add authorize_token to header

I am trying to update my WordPress plugins automatically using transient.
I have one that works but it adds authorize_token to query params like below code.
$new_files = $this->github_response['zipball_url']; // Get the ZIP
***$package = add_query_arg(array("access_token" => $this->authorize_token), $new_files);***
$slug = current( explode('/', $this->basename ) ); // Create valid slug
$plugin = array( // setup our plugin info
'url' => $this->plugin["PluginURI"],
'slug' => $slug,
'package' => $package,
'new_version' => $this->github_response['tag_name']
);
$transient->response[$this->basename] = (object) $plugin;
but i have received email from github that says i should add authorize_token to header.
I'm trying to find solution by google, but i can't find it.
How can I fix it?
I figured that you will have to add a filter to the http_request. So when wordpress will try any http request it will go through your filter. Then inside that filter you can check the url and if it is your plugin source url, you can then add your accessToken to the header.
this method was in my updater class, and I had setup the class property accessToken and username when initializing the updater.
add_filter( "http_request_args", array( $this, "addHeaders") , 10, 3);
public function addHeaders($parsed_args, $url)
{
if(empty($parsed_args['headers']))
{
$parsed_args['headers'] = [];
}
if(strpos($url, "https://api.github.com/repos/{$this->username}/{$this->repo}") !== FALSE)
{
$parsed_args['headers']['Authorization'] = "token $this->accessToken";
error_log("Adding token for : $url");
}
else
{
error_log("Not adding token for $url");
}
return $parsed_args;
}

Wordpress Create Custom Post on New User Register

In Wordpress I'm Trying to Create Custom Post on New User Register of specific user role "author"
For This I try to figure out this Code in Function.php
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles=$user_info->roles;
if ($user_roles == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
But Not Success. After I add above code no error working fine but it not triggering automatic and not creating new custom post
I simple want that whenever I add new Author / New Author Register it create one Custom Post with title as same as username. and publish it
The role your are checking with is not correct, $user_info->roles returns an array, not a string. Find the modified code below,
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$this_user_role = implode(', ', $user_roles );
if ($this_user_role == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
Hope this helps.

Resources