Display Notice on product page after Review Submit in WooCommerce - woocommerce

I am using the code below to "re-direct" back to the product page because if not, the page stays at the "bottom" where the review form is.
My question is this:
how do I print / display a message on the product page after the review has been submitted by the customer?
Here's my code:
add_filter('comment_post_redirect', 'redirect_after_comment');
function redirect_after_comment($location) {
$location = wp_get_referer();
wc_add_notice( __( 'Thank you for writing a review. Use this coupon code <code>FIVEOFF</code> on the checkout and get $5 OFF!', 'woocommerce' ), 'success' );
return $location;
}
Problem is, the message is not being displayed and I tried adding in a do_action for wc_notice which did not help either.

You should put wc_add_notice() before return $location;. Because return stops the execution of the function and sends the value back, so the statements after it won't be executed.

Related

Woocommerce - Remove fields from woocommerce_form_field including validation

I am trying to customise the Woocommerce myaccount page, in particular the edit address page.
I want to display both the shipping + billing address forms on a single page. Ideally, in a single form with a one save button. I also need to remove a lot of the fields, so that it's a much simpler form of just an address (no name, company, etc).
I have implemented the code found on This Answer. It works nicely in that it shows both forms. However, I cannot remove the fields from the forms. If I try code like this:
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_billing_fields( $fields ) {
unset($fields['billing_country']);
unset($fields['billing_company']);
unset($fields['billing_first_name']);
unset($fields['billing_last_name']);
unset($fields['billing_phone']);
unset($fields['billing_email']);
return $fields;
}
function custom_override_shipping_fields( $fields ) {
unset($fields['shipping_country']);
unset($fields['shipping_company']);
unset($fields['shipping_first_name']);
unset($fields['shipping_last_name']);
return $fields;
}
It doesn't work, the fields are no longer shown but the form does not save on click... it just redirects to /my-account/edit-address/billing/ - and doesn't save. (the same form shown on this page doesn't save either).
I've also tried:
foreach ( $billing_fields as $key => $field ) :
if($key != 'billing_first_name' && $key != 'billing_last_name') :
woocommerce_form_field( $key, $field, $userMeta[$key][0] );
endif;
endforeach;
This removes the field from displaying, BUT the validation still exists - and any filter code I add to functions using
woocommerce_checkout_fields to remove the validation doesn't seem to affect this form at all.
Is there a way to either:
Remove fields from this form generated by woocommerce_form_field including the validation?
Create a custom form that allows me to set the input fields manually in the code, and update any fields that are there, ignoring the validation from Woocommerce completely?
This should work 100%. You need to state whether the fields you are removing is from billing or shipping and this is done by adding the ['billing'] or ['shipping'], whichever it is.
After this, adding the function directly to woocommerce_checkout_fields will apply both for billing and shipping.
For phone and company fields you can disable it in admin panel itself, do it.
Edit: And yes, all validation that was involved with the fields in the past will be removed. You can then apply any validation you need.
add_filter( 'woocommerce_checkout_fields' , 'brandimagemarketer_remove_billing_fields_checkout' );
function brandimagemarketer_remove_billing_fields_checkout( $fields ) {
unset($fields['billing']['billing_country']);
unset($fields['billing']['billing_first_name']);
unset($fields['billing']['billing_last_name']);
unset($fields['billing']['billing_email']);
unset($fields['shipping']['shipping_country']);
unset($fields['shipping']['shipping_first_name']);
unset($fields['shipping']['shipping_last_name']);
unset($fields['shipping']['shipping_email']);
return $fields;
}

Hide & refresh WooCommerce checkout after add coupon

We want to hide "Have a coupon? Add one..." on WooCommerce checkout, if a coupon is already added OR when a customer add a coupon on the checkout page.
Currently we habe this code below and it works when a customer enters a coupon on the cart page and then navigate to the checkout page. In this case, the "Have a coupon? Add one..." message is not visible. If no coupon in added on the cart page the message is visible.
This is working fine! But it does not work when a customer adds a coupon on the checkout page.
1.) We get the message "Coupon added" But the coupon message to add one is still visible AND also the coupon is not calculated in the order table. => After a page refresh is everything correctly.
2.) When a coupon is removed by the customer on checkout, then we get the message that the coupon is removed, but the discount is still visible in the order table. => After a page refresh it displays everything right again.
So now I'm trying to refresh the page after a coupon was added or removed. But I have problems to get the right event. I guess we must do this via js? Or is there a PHP approach?
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
return false;
}
return $coupons_enabled;
}
Your code should be like this
add_filter( 'woocommerce_coupons_enabled', 'woocommerce_coupons_enabled_checkout' );
function woocommerce_coupons_enabled_checkout( $coupons_enabled ) {
if(is_checkout()){
global $woocommerce;
if ( ! empty( $woocommerce->cart->get_applied_coupons() ) ) {
$coupons_enabled = false;
}
}
return $coupons_enabled;
}
Edit: okay you need to check if page is checkout or cart then run the script. I have added condition in code.

Access WooCommerce custom post field in same action as send email

Before this gets flagged as a duplicate of Save custom post fields value before Woocommerce email send I will point out that the answer given to that question is a) not accepted and b) refers to a hook that is no longer in the documentation.
I'm helping out a friend with their WordPress WooCommerce site, and they want to be able to add a tracking number to the email that gets automatically sent out to a customer once the order status is set to "Completed". I've managed to get this all working using the Advanced Custom Fields plugin and some code in functions.php. The problem I have is the user friendliness isn't quite there when it comes to processing the order, it being a double-barrelled operation:
Fill in the tracking number custom field on the Edit Order screen and then click the Update button
Set the status to Completed and the click the Update button again
Ideally, it would be nice to fill in the tracking number text field and change the status dropdown value to Completed, and then hit update. If that is done though, the email doesn't get the custom field populated, presumably because the custom fields get saved after the email gets triggered.
Is there a hook in WooCommerce that allows, on update of the order, saving of custom fields to occur before triggering the email?
For reference, this is my current code in functions.php, where (despite changing the priority of the operation to 1) it doesn't populate the tracking number on the email:
add_action( 'woocommerce_email_before_order_table', 'add_content_specific_email', 1, 4 );
function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' ) {
$ausPostTrackingNumber = get_post_meta( $order->get_id(),'australia_post_tracking_number',true);
if($ausPostTrackingNumber != '') {
echo '<p class="email-auspost-text">Your Australia Post tracking number is ' . $ausPostTrackingNumber . '</p>';
}
}
}

Backorder Notification Woocommerce

I have a little problem with the backorder notification on my woocommerce shop.
What it is now: I set in the product backorders: allowed, but notify customer. When I press a variaton on the product page it will show available for backorder. So far so good. When someone place the order it will show the text backorderer: 1 on the order confirmation, on the pdf invoice, on the e-mail.
What I want: Show the notification only on the product information page. Not on the e-mails, order confirmation, etc. But when I set up backorders only to allow. There is also no notification on the Product Page for the customer.
So do someone of you know, how I can change this? Is there a custom code or something else I can use.
This is really important for me. I tried to find a solution about 1 week without any luck. So please help me.
Can you try this?
Just copy and paste this block of code to your functions.php file in your child theme.
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2); function wcs_custom_get_availability( $availability, $_product ) { // Change In Stock Text if ( $_product->is_in_stock() && ! $_product->backorders_allowed() ) { $availability['availability'] .= __('<br />Shipped immediately', 'custom'); } if ( $_product->is_in_stock() && $_product->backorders_allowed() ) { $availability['availability'] = __('<br />We will inform you via email when the product is back in stock. Please send us your contact info via the form below.', 'custom'); } // Change Out of Stock Text if ( ! $_product->is_in_stock() ) { $availability['availability'] .= __('<br />We will inform you via email when the product is back in stock. Please send us your contact info via the form below.', 'custom'); } return $availability; }
And let me know whether it worked for you or not.
Thank you

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 );
} );

Resources