WooCommerce: custom error message when cancelling bank payment - woocommerce

How can I display some custom error message when user clicks "place order" with card payment method selected, is redirected to the bank website, but then hits "cancel" and gets redirected back to WooCommerce thank you page?
The WooCommerce thank you page template code is summarized here (just in case I know how to add custom WooCommerce templates in my child theme):
if ( $order ) : ?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p><?php _e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction.', 'woocommerce' ); ?></p>
<?php else : ?>
<p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->payment_method, $order->id ); ?>
<?php do_action( 'woocommerce_thankyou', $order->id ); ?>
<?php else : ?>
<p><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>
<?php endif; ?>
Where does the bank payment cancelling case is located in this code? And how to display a custom message like "you cancelled bank payment, your order has been cancelled"?

function wdm_my_custom_message( $order_id ){
global $woocommerce;
$order=new WC_Order($order_id);
if ( $order->has_status( 'failed' ) ) {
$woocommerce->add_error( __( 'you cancelled bank payment, your order has been cancelled.', 'woocommerce' ) );
}
}
add_action( 'woocommerce_thankyou','wdm_my_custom_message',10,1);
this will help you to achieve your purpose.

Related

The order number belongs to the user or not

I wanted to make a series of changes on the page checkout/thankyou.php woocommerce
if ( is_user_logged_in() ) {
if ( order number Belongs to user ???? ) {
show order details
} else { echo 'not show order details'; }
}
But I do not know how to check whether the order number belongs to the user or not
If the custom number entered belongs to him, show the order details
If the order number does not belong to the user, do not display the order details
I searched the site for about 3 hours but did not find anything, then I decided to ask a question
You can check with user id from order object $order->user_id; and check against the current logged-in user id for that you can use get_current_user_id(). try the below code.
thankyou.php
<?php
/**
* Thankyou page
*
* This template can be overridden by copying it to yourtheme/woocommerce/checkout/thankyou.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files, and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #package WooCommerce\Templates
* #version 3.7.0
*/
defined( 'ABSPATH' ) || exit;
$user_id = $order->user_id;
if( is_user_logged_in() && $user_id == get_current_user_id() ){ ?>
<div class="woocommerce-order">
<?php
if ( $order ) :
do_action( 'woocommerce_before_thankyou', $order->get_id() );
?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed"><?php esc_html_e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed-actions">
<?php esc_html_e( 'Pay', 'woocommerce' ); ?>
<?php if ( is_user_logged_in() ) : ?>
<?php esc_html_e( 'My account', 'woocommerce' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', esc_html__( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Order number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<li class="woocommerce-order-overview__date date">
<?php esc_html_e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo wc_format_datetime( $order->get_date_created() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
<li class="woocommerce-order-overview__email email">
<?php esc_html_e( 'Email:', 'woocommerce' ); ?>
<strong><?php echo $order->get_billing_email(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php endif; ?>
<li class="woocommerce-order-overview__total total">
<?php esc_html_e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></strong>
</li>
<?php if ( $order->get_payment_method_title() ) : ?>
<li class="woocommerce-order-overview__payment-method method">
<?php esc_html_e( 'Payment method:', 'woocommerce' ); ?>
<strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->get_payment_method(), $order->get_id() ); ?>
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', esc_html__( 'Thank you. Your order has been received.', 'woocommerce' ), null ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></p>
<?php endif; ?>
</div>
<?php }else{
echo 'Your custom message here.';
} ?>

Add editable address fields to WooCommerce invoice page

Please forgive such a basic question. I'm relatively new to WooCommerce theme development and genuinely trying to understand how wordpress handles customer data so I can learn how to manipulate it. If this question is still too broad, (My previous question was closed for that reason) I'd even welcome a few links which help point me in the correct direction and explain the area I'm looking at.
What I am trying to do is add editable address fields to the page customers see when we email them an invoice. (form-pay.php)
Initially, I tried adding fields manually using variation on the following code for each field in functions.php and calling it in from order-pay.php:
<p class="form-row form-row-first">
<label for="billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
However, when I do it this way, it adds the address details associated with my own WordPress login, rather than the customer's address associated with the invoice.
I have worked through the woocommerce codex on hooks and filters and also found the answer to this question which allowed the correct address fields to be added.
This is where my question seems to differ from a lot of the solutions I've found, in that most solutions are for updating the billing and shipping address of the current cart or logged in user, rather than that associated with a specific invoice.
Here's the fields I've added to form-pay.php.
<h2 class="woocommerce-column__title"><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>
<address>
<?php echo wp_kses_post( $order->get_formatted_billing_address( __( 'N/A', 'woocommerce' ) ) ); ?>
<?php if ( $order->get_billing_phone() ) : ?>
<p class="woocommerce-customer-details--phone"><?php echo esc_html( $order->get_billing_phone() ); ?></p>
<?php endif; ?>
<?php if ( $order->get_billing_email() ) : ?>
<p class="woocommerce-customer-details--email"><?php echo esc_html( $order->get_billing_email() ); ?></p>
<?php endif; ?>
</address>
<h2 class="woocommerce-column__title"><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>
<address>
<?php echo wp_kses_post( $order->get_formatted_shipping_address( __( 'N/A', 'woocommerce' ) ) ); ?>
</address>
<!-- Form -->
<h3><?php _e( 'Billing details', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_before_checkout_billing_form', $order ); ?>
<div class="woocommerce-billing-fields__field-wrapper">
<?php
$fields = WC()->checkout->get_checkout_fields( 'billing' );
foreach ( $fields as $key => $field ) {
$field_name = $key;
if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
$field['value'] = $order->{"get_$field_name"}( 'edit' );
} else {
$field['value'] = $order->get_meta( '_' . $field_name );
}
woocommerce_form_field( $key, $field, $field['value'] );
}
?>
</div>
<?php do_action( 'woocommerce_after_checkout_billing_form', $order ); ?>
<h3><?php _e( 'Shipping details', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_before_checkout_shipping_form', $order ); ?>
<div class="woocommerce-shipping-fields__field-wrapper">
<?php
$fields = WC()->checkout->get_checkout_fields( 'shipping' );
foreach ( $fields as $key => $field ) {
$field_name = $key;
if ( is_callable( array( $order, 'get_' . $field_name ) ) ) {
$field['value'] = $order->{"get_$field_name"}( 'edit' );
} else {
$field['value'] = $order->get_meta( '_' . $field_name );
}
woocommerce_form_field( $key, $field, $field['value'] );
}
?>
</div>
<?php do_action( 'woocommerce_after_checkout_shipping_form', $order ); ?>
The fields correctly appear on the invoice page now which display all address details I have already added to the invoice. I just need to understand what logic I need to attach to a button to tell WooCommerce to update the record.
As I said, I recognise this is a fundamentally basic question, but so far I've failed to find an explanation that works specifically in the context of updating the invoice rather than the cart.

Woocommerce - Print Product's Custom Field In Email

In Woocommerce, I need to print a custom field on the Completed Order email.
The template is completely unique in my themes woocommerce/emails folder because the default template doesn't work for my needs.
As such I'm trying to display the following:
Title, Quantity and two custom fields
This needs to be included in the customer-completed-order.php email template but I am not certain what syntax is necessary to print it.
I found something by Googling but it doesn't work:
<?php foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
?>
<p>Class: <?php echo get_post_meta($_product->id,'name',true); ?></p>
<p>Date: <?php echo get_post_meta($_product->id,'date_text_field',true); ?></p>
<p>Time: <?php echo get_post_meta($_product->id,'time_text_field',true); ?></p>
<p>Number of Spaces: <?php echo get_post_meta($_product->id,'qty',true); ?></p>
<?php endforeach; ?>
Try adding to action hook,
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
OR
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
This could be used in following way:
add_action( 'woocommerce_order_item_name', 'action_custom_order_meta', 10, 3 );
OR
add_action( 'woocommerce_order_item_name', 'action_custon_order_meta', 10, 3 );
Following is the callback handler function.
function action_custom_order_meta($item_name, $item, $false){
$product_id = $item['product_id'];
$product_custom_date = get_post_meta($product_id, 'product_custom_date', true);
$product_custom_time = get_post_meta($product_id, 'product_custom_time', true);
$product_custom_meta = 'Date: ' . $product_custom_date . ' <br> Time: ' . $product_custom_time;
echo $product_custom_meta;
}
Change the custom field names to appropriate. You might also need to wrap the output according to the required markup for your custom email.
Hope this helps.

Changing a heading in Woo Commerce file

The site is located at http://nancyhamiltonart.com/product/yellow-river/
Near the bottom, there's a "Related Products" text. I'm trying to change it to "Related Paintings".
I've narrowed down where I believe the string is, but when I updated and reload, it's not working.
Here's what I've changed (from wp-content/plugins/woocommerce/templates/single-product/related.php)
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Paintings', 'woocommerce' ); ?></h2>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
Line 3 -
<h2><?php _e( 'Related Paintings', 'woocommerce' ); ?></h2>
is where I made the change, but nothing changed. Is there a different string that creates the output I'm referring to?
Thoughts one what I'm doing wrong?
UPDATE
I added the following in the functions.php file...
//Changes text "Related Products" to "Related Paintings
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related Products' :
$translated_text = __( 'Related Paintings', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Everything seems to work now...

Woocommerce send email on order

I need anything that, when a order is created, it sends in addition to the email that goes to the costumer, an email to one of my email accouts with only some information the customer inserted, such as:
Name
Surname
Street
Country etc.
How could I archive this?
Go to yoursite.com/wp-admin/admin.php?page=wc-settings&tab=email&section=wc_email_new_order or from the dashboard:
Go to the WooCommerce sections and in that go to the Settings
In settings click on the Email tab and in that click on the New Order tab.
At the bottom of that page there is a button called: "Copy file to theme", click on that and then click on the "View Template" button. Replace the contents of the email from <?php do_action( 'woocommerce_email_header', $email_heading ); ?> down with:
<h2><?php _e( 'Customer details', 'woocommerce' ); ?></h2>
<?php echo $order->get_formatted_billing_address(); ?>
<?php if ( $order->billing_email ) : ?>
<p><strong><?php _e( 'Email:', 'woocommerce' ); ?></strong> <?php echo $order->billing_email; ?></p>
<?php endif; ?>
<?php if ( $order->billing_phone ) : ?>
<p><strong><?php _e( 'Tel:', 'woocommerce' ); ?></strong> <?php echo $order->billing_phone; ?></p>
<?php endif; ?>
The above removes the order details and keeps the customers details.

Resources