Add custom button in myaccount users page in woocommerce plugin - woocommerce

After clicking on Users -> Edit User , I want to create a custom button in users page next to Update button.Which action should be written inside our functions.php page

By default WordPress not provided any hook to add a custom button in WordPress user edit interface but we can add it by hook using jquery. Button action we can implement by using function action admin_init.
add_action('admin_head','add_button_profile');
function add_button_profile( $checkout ) {
if(isset($_GET['user_id']) && !empty($_GET['user_id'])){
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery(".submit #submit").after(' <input
id="customButton" class="button button-primary" name="customsubmit"
value="Custom Button" type="submit">');
});
</script>
<?php }
}
add_action('init','custom_button_function');
function custom_button_function() {
if(is_admin() && isset($_POST['customsubmit']) && ($_POST['customsubmit']
== 'Custom Button')){
#####You can create button action here
}
}

Related

update checkout after adding product

am trying to do somthing like : Woocommerce update checkout ajax! am building a multistep form for my client where the customer is adding a product in the cart using ajax . I need to update the checkout when a product is added into the cart . I tried to do an onclick function so when the user add the product in the cart the checkout step update without refreshing the page :
jQuery('#test').on('click',function(){
$( document.body ).trigger( 'update_checkout' );
})
but its not working and i feel like am missing somthing obvious .... Any tips ? :)
This event fire after checkout update
jQuery( document ).on( 'updated_checkout', function() {
//Write code here to fire event
console.log('run');
});
Your code is correct you have to replace $ with jQuery
//Paste this code in theme footer.php file and check it on checkout page
<?php if (is_checkout()) { ?>
<button id="test">Click here to update checkout</button>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#test').on('click',function(){ alert("pp");
jQuery( document.body ).trigger( 'update_checkout' );
});
});
</script>
<?php } ?>
The code is tested everything is working fine.

How to clear form input on page load woocommerce checkout

I am offering customers a repurchase on the order confirmed page. If they click the link they are sended directly to the checkout page. However, the form is then prefilled with the previous data. I would like to clear it, because the idea is one product(ticket) per person.
So far i have the following (and in the page source i can see the script is loaded):
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)){
echo '<script>
window.addEventListener("load", function(){myFunction()};
function myFunction() {
document.getElementsByClassName("woocommerce-checkout").reset();
}
</script>';
}
}
add_action('wp_print_scripts', 'print_script');
I tried other triggers too, such as window.load but no result yet.
Anyone?
You should put the script in the footer using add_action( 'wp_footer', 'print_script' ); to make sure the form gets cleared after the data was loaded into it.
Wordpress has jQuery on default, so I'm using it to look for all the forms in the document and clear them. The function is automatically called on page load, therefore no need for a event listener.
<?php
function print_script() {
if (isset($_GET['extrapersoon']) && is_page(21560)) { ?>
<script type="text/javascript">
( function( $ ) {
$('form').each(function() { this.reset() });
}( jQuery ) );
</script>
<?php }
}
add_action( 'wp_footer', 'print_script' ); ?>

Send an Email and Download a PDF on click of Contact Form 7 Submit button

I have a contact form in my Wordpress website. In that I have three fields Name, Email and Mobile, and a button called Submit. When the user fills all the fields and click on Submit button an email should send which can possible with Contact From 7 plugin.
But challenge here is I need to make the user download a PDF also, when he clicks on Submit button upon filling all the fields.'
How can I achieve this in Wordpress?
You can use the wpcf7_mail_sent hook provided by Contact form 7 like this:
add_action('wpcf7_mail_sent', function ($cf) {
// Run code after the email has been sent
});
This link: https://contactform7.com/2017/06/07/on-sent-ok-is-deprecated/ also describes another way:
add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer()
{ ?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event )
{
//Write a javascript code to download the file.
} , false );
</script>
<?php
}
Since the on_sent_ok has been deprecated, here is an example you could use as inspiration.
I had the similar need to add a download CTA after the content of all case studies of a website, but "in exchange" of user's data for:
display a CF7 form on your page, I had the same one on all case studies post type single which I hooked after the content
find a way to get the wanted PDF url for people to download, as for me all case studies have a different PDF, I simply added an ACF field, filtered on PDF only, which returns the file url
based on CF7 Dom events, choose the action you prefer to make the dowload happens, as I am not sending any confirmation email, I prefer working on the wpcf7submit event. Note that wpcf7submit event is fired only if the form has been validated
So the code looks like this:
<?php
// For simplicity, using an anonymous functions
add_action( 'wp_print_footer_scripts', function () {
// Check the wanted singular post type loading
if ( is_admin() || ! is_singular( 'case-study' ) ) {
return;
}
// Check if the ACF PDF field is not empty for use
$pdf_link = get_field( 'pdf' );
if ( empty( $pdf_link ) ) {
return;
}
// Hook on the "wpcf7submit" CF7 Dom event to force the download
printf( "<script>document.addEventListener( 'wpcf7submit', function( event ) { window.open('%s'); }, false );</script>", $pdf_link );
} );

How to add a button to a custom post type in Wordpress?

I have a "Products" custom post type. Normally, this custom post type have an "Add New" button. I want to add another button call "Update from Provider".
Currently, I have modify the Wordpress code (in "wordpress\wp-admin\includes\class-wp-list-table.php") to add that button. In this case, when I update Wordpress, my modified code will be deleted. Therefore, I need to move that button to my plug-in code.
In this case, please help me how to move that button to my plug-in code.
Well, if you opened the core file you saw that there's no action in it where we can hook.
Only a couple of filters. We can use the following:
add_filter( 'views_edit-movies', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
$views['my-button'] = '<button id="update-from-provider" type="button" title="Update from Provider" style="margin:5px">Update from Provider</button>';
return $views;
}
It produces this:
To put it in an approximate position from where you'd like, use the following:
add_action( 'admin_head-edit.php', 'so_13813805_move_custom_button' );
function so_13813805_move_custom_button( )
{
global $current_screen;
// Not our post type, exit earlier
if( 'movies' != $current_screen->post_type )
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
$('#update-from-provider').prependTo('span.displaying-num');
});
</script>
<?php
}
Which results in this:

Show Metabox In Write Post/Page When Template is Changed

I can create the metaboxes just fine and if I assign a metabox to a specific template(s), they appear just fine in the Add New Page screen, but only after the new page has been saved.
Is there a way to show/hide the metabox when the template value has changed...without having to save the page first?
Add to WP admin page
jQuery(document).ready(function($) {
$("#page_template").live('change',function(){
var getCurrentTemplate = $("#page_template").val();
if( getCurrentTemplate == "template-contact.php"){
$("#pageheading-hide").attr('checked', true);
$("#pageheading").css({'display':'block'});
}
else{
$("#pageheading-hide").attr('checked', false);
$("#pageheading").css({'display':'none'});
}
});
});

Resources