Displaying an admin message on woocommerce_order_action hook - php

I used this code :
https://gist.github.com/bekarice/5233ed58c3a836064123b290463241c0
In sv_wc_process_order_meta_box_action function, how is possible to display a message box to admin?
Currently code uses update_post_meta() function and add_order_note() method and not display any message to admin.
Thanks.

The only way I know is to use custom functions with the admin_notices action hook. So you could try to include the related add_action() inside the code you are using.
This code is untested, and I don't guarantee anything:
// The message function to be hooked in 'admin_notices' hook.
function my_custom_admin_notice() {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e('Order has been updated "printed for packaging"'); ?></p>
</div>
<?php
}
//The second function that you use (customized with an add_action()):
function sv_wc_process_order_meta_box_action( $order ) {
// add the order note
$message = sprintf( __( 'Order information printed by %s for packaging.', 'my-textdomain' ), wp_get_current_user()->display_name );
$order->add_order_note( $message );
// add the flag so this action won't be shown again
update_post_meta( $order->id, '_wc_order_marked_printed_for_packaging', 'yes' );
// Setting the admin message function in 'admin_notices' hook.
add_action('admin_notices', 'my_custom_admin_notice');
}
add_action( 'woocommerce_order_action_wc_custom_order_action', 'sv_wc_process_order_meta_box_action' );
Related documentation:
Complete Guide to WordPress Admin Notices
How to Add Admin Alerts and Error Messages to the Backend of WordPress

Related

How to Use WP_REDIRECT and Continue Parent Execution

In WordPress, how do you use the WP_REDIRECT() function from a custom plugin, and then pass back execution to the parent function?
When I use WP_REDIRECT, I am noticing that the execution is not passed back to the parent function.
Sharing Code - UPDATE:
How can I hook into the below plugins' do_action( 'otherplugin_new_user_moderation', $customer_id ); hook and use the below redirect function to force the home page to load (from my plugin); and then pass the execution flow back to the parent (other plugin) so that the wc_add_notice is added to the home page (instead of the current page).
function redirect() {
wp_safe_redirect(home_url());
exit();
}
Another Plugin - handles moderation:
do_action( 'otherplugin_created_customer', $customer_id, $new_customer_data );
if ( 'yes' === \get_option( 'otherplugin_moderate_registrations', 'yes' ) ) {
update_user_meta( $customer_id, 'otherplugin_moderation_required', true);
do_action( 'otherplugin_new_user_moderation', $customer_id );
wc_add_notice( __( 'Your registration has been submitted successfully.', 'other-plugin' ) );
} else {
do_action( 'otherplugin_new_user_auto_approved', $customer_id, $password );
wc_add_notice( __( 'Your registration was processed successfully and a password has been sent to your email address.', 'other-plugin' ) );
}
Please let me know if I can clarify anything from my end.
Thank you!

Why does the 'Added to cart' message still come up?

I have coded a custom 'Added to cart' message for WooCommerce and after that I used the conditional tag 'is_cart()' to disable the message on the cart page but it still shows up.
How can I disable the message being shown on cart page?
Here is my code example:
function iw_add_to_cart_message_function( $message, $product_id ) {
if (!is_cart()) {
$message = sprintf(esc_html__('%s has been added by to your cart pompidompidom.','iwebbers'), get_the_title( $product_id ) );
return $message;
}
}
add_filter( 'wc_add_to_cart_message', 'iw_add_to_cart_message_function', 10, 2 );
I just tried the following IF STATEMENT and it works:
global $woocommerce;
if (get_option('woocommerce_cart_redirect_after_add')=='no') {
Function wc_add_to_cart_message
it Return message rather than add it.
go refer link: docs.woocommerce

How to add new Custom Title in Woocommerce My Account sidebar?

Hi i want to add new custom title and links into My Account page into my WooCommerce site.. Searched all documentation, and Stackoverflow topics, but not found sollution for my request.
This is title for what im asking for
Text Muj Ucet is My Account in english. :)
i want to add new title like is shown in image bellow:
This is WooCommerce Template Code for that part:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<p><?php
/* translators: 1: user display name 2: logout url */
printf(
__( 'Hello %1$s (not %1$s? Log out)', 'woocommerce'
),
'<strong>' . esc_html( $current_user->display_name ) . '</strong>',
esc_url( wc_logout_url( wc_get_page_permalink( 'myaccount' ) ) )
);
?></p>
<p>Na nástěnce svého uživatelského účtu si můžete stáhnout své zakoupené
produkty a faktury, upravit své osobní informace, změnit heslo nebo
fakturační adresu.</p>
<?php
/**
* My Account dashboard.
*
* #since 2.6.0
*/
do_action( 'woocommerce_account_dashboard' );
/**
* Deprecated woocommerce_before_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_before_my_account' );
/**
* Deprecated woocommerce_after_my_account action.
*
* #deprecated 2.6.0
*/
do_action( 'woocommerce_after_my_account' );
/* Omit closing PHP tag at the end of PHP files to avoid "headers already
sent" issues. */
I want to add new one title bellow that sidebar. How to register a new title ?
Thanks
Here is the solution.
add_filter('woocommerce_account_menu_items', 'display_account_new_link');
function display_account_new_link( $items ) {
$items['new_link'] = __( 'New Link', 'text-domain' );
return $items;
}
add_action( 'woocommerce_account_new_link_endpoint', 'new_account_link_content' );
function new_account_link_content() {
//include your display template here
echo "Here goes you content";
}
After pasting this code in your plugin or in your theme function.php file this code will make a new link in the my account navigation sidebar, along with the template you want to assign this link. Here new_link is the slug for this navigation link. If you want to give some different slug you must rename new_link written everywhere in the given code. As soon as you click on this New Link it will redirect you to the Page Not Found Page. It can be solved by adding this code.
add_action( 'init', 'register_new_link_endpoint');
function register_new_link_endpoint() {
add_rewrite_endpoint( 'new_link', EP_PAGES );
}
After pasting this code you must save your permalink once, by going to WordPress Dashboard->Settings->Permalinks and hit the save changes button.

Change checkout submit button text for a specific payment method in WooCommerce

I need to change the order_button_text for a specific payment gateway (COD in this case).
I could only get it to change globally (for all payment gateways) using:
add_action( 'woocommerce_after_add_to_cart_button', 'multiple_orders_text' );
function woo_custom_order_button_text() {
return __( 'Request Shipping Quote', 'woocommerce' );
}
But have found that if I add the line
$this->order_button_text = __( 'Request a Quote', 'woocommerce' );
to the setup_properties() method in woocommerce/includes/gateways/cod/class-wc-gateway-cod.php it does work.
However this is clearly bad practice as I'm hacking a core plugin file.
How can I achieve this without hacking a woocommerce core file?
You can do it like this:
add_filter( 'woocommerce_available_payment_gateways', 'woocommerce_available_payment_gateways' );
function woocommerce_available_payment_gateways( $available_gateways ) {
if (! is_checkout() ) return $available_gateways; // stop doing anything if we're not on checkout page.
if (array_key_exists('paypal',$available_gateways)) {
// Gateway ID for Paypal is 'paypal'.
$available_gateways['paypal']->order_button_text = __( 'Request a Quote', 'woocommerce' );
}
return $available_gateways;
}
This code example is for paypal. For reference of the gateway IDs, please check WooCoomerce > Settings > Checkout > Gateway display order
Here it is the clean way to do it, using woocommerce_review_order_before_payment action hook with a custom function hooked in, using mainly jQuery (because it's a client side live event):
add_action( 'woocommerce_review_order_before_payment', 'customizing_checkout_button', 10, 0 );
function customizing_checkout_button(){
$text1 = __( 'Place order', 'woocommerce' );
$text2 = __( 'Request a Quote', 'woocommerce' );
?>
<script>
jQuery(function($){
// 1. Initialising once loaded
if($('input[name^="payment_method"]:checked').val() == 'cod' )
$('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
else
$('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');
// 2. Live event detection:When payment method is changed
$( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
var choosenPaymentMethod = $('input[name^="payment_method"]:checked').val(); // Chosen
if( choosenPaymentMethod == 'cod' )
$('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text2; ?>');
else
$('input[name^="woocommerce_checkout_place_order"]').val('<?php echo $text1; ?>');
});
});
</script>
<?php
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works with WooCommerce 3+
Easy solution, try to add the following code in your theme's function.php file.
/**
* #snippet Change checkout order button text
* #package WooCommerce
*/
function change_checkout_order_button_text() {
return __( 'Complete Order', 'woocommerce' );
}
add_filter( 'woocommerce_order_button_text', 'change_checkout_order_button_text' );

Adding content after add to cart button on woocommerce single page

I have successfully added a content after short description on single product page with
if (!function_exists('my_content')) {
function my_content( $content ) {
$content .= '<div class="custom_content">Custom content!</div>';
return $content;
}
}
add_filter('woocommerce_short_description', 'my_content', 10, 2);
I saw that in short-description.php there was apply_filters( 'woocommerce_short_description', $post->post_excerpt )
so I hooked to that.
In the same way, I'd like to add a content after the add to cart button, so I found do_action( 'woocommerce_before_add_to_cart_button' ), and now I am hooking to woocommerce_before_add_to_cart_button. I'm using
if (!function_exists('my_content_second')) {
function my_content_second( $content ) {
$content .= '<div class="second_content">Other content here!</div>';
return $content;
}
}
add_action('woocommerce_after_add_to_cart_button', 'my_content_second');
But nothing happens. Can I only hook to hooks inside apply_filters? From what I've understood so far by working with hooks is that you only need a hook name to hook to and that's it. The first one was a filter hook, so I used add_filter, and the second one is action hook so I should use add_action, and all should work. So why doesn't it?
Here, you need to echo content as it is add_action hook.
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart_button_func' );
/*
* Content below "Add to cart" Button.
*/
function add_content_after_addtocart_button_func() {
// Echo content.
echo '<div class="second_content">Other content here!</div>';
}
You need to do echo instead of return.
add_action( 'woocommerce_after_add_to_cart_button', 'ybc_after_add_to_cart_btn' );
function ybc_after_add_to_cart_btn(){
//add text OR HTML here
echo '<p>After custom text here</p>';
}
If you want the same thing on the shop archive page then you need to use the woocommerce_loop_add_to_cart_link filter to modify the add to cart button.
When using 'Action Hook' adding the content(html) out of php would be easy.
if (!function_exists('my_content_second')) {
function my_content_second( $content ) {
?>
<div class="second_content">Other content here!</div>;
<?php
}
}
add_action('woocommerce_after_add_to_cart_button', 'my_content_second');
If need to add dynamic content just echo that content using variables or add using some condition.
Filter hooks are useful to modify the existing content and needs a return statement (the modified)
Action hooks are mostly useful to add content.

Categories