How to add more buttons into WooCommerce Add to Cart notice? - css

i want to make this change to my site.
after adding product in cart, i want to integrate more buttons in that notice showed in image. I want inside to place button:
Continue Shopping and Finish Shopping buttons .
How to do this? i searched on google and found some functions for this, but none of them was ok for my needs. This is one of them:
/**
* Custom Add To Cart Messages
* Add this to your theme functions.php file
**/
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
global $woocommerce;
// Output success messages
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
$return_to = get_permalink(woocommerce_get_page_id('shop'));
$message = sprintf('%s %s',
$return_to, __('Continue Shopping →', 'woocommerce'), __('Product
successfully added to your cart.', 'woocommerce') );
else :
$message = sprintf('%s %s',
get_permalink(woocommerce_get_page_id('cart')), __('View Cart →',
'woocommerce'), __('Product successfully added to your cart.',
'woocommerce') );
endif;
return $message;
}
/* Custom Add To Cart Messages */

Related

woocommerce add line item meta to main order meta

I have a created a simple gift wrapping plugin, which adds a gift wrap product to the user cart and when they enter a message it adds it as meta data to the line item, this is fine for the woocommerce system, but our backoffice needs this meta data to be on the order and not the line item, is there a way I can add the data to the order also?
function checkout_create_order_line_item($item, $cart_item_key, $values, $order)
{
if (isset($values['_gift_wrap_message'])) {
$item->add_meta_data(__('Your message', '_gift_wrap_message'), $values['_gift_wrap_message'], true);
}
}
add_action('woocommerce_checkout_create_order_line_item', 'checkout_create_order_line_item', 10, 4);
I have the above and and I have tried the folllowing,
function checkout_create_order_line_item($item, $cart_item_key, $values, $order)
{
if (isset($values['_gift_wrap_message'])) {
$item->add_meta_data(__('Your message', '_gift_wrap_message'), $values['_gift_wrap_message'], true);
$order->update_meta_data('_gift_wrap_message', $values['_gift_wrap_message');
$order->save();
}
}
add_action('woocommerce_checkout_create_order_line_item', 'checkout_create_order_line_item', 10, 4);
But this does not work. I would really apprieciate some guidance as to how to get $values['_gift_wrap_message'] (or at least it's contents) into the order meta data
For any dynamic order line meta, you should follow 3 steps
1) Add custom meta fields on the checkout page from where customers can pass their message like this -
/**
* Add a custom field to the checkout page
*/
add_action('woocommerce_after_order_notes', 'custom_gift_wrap_message_field');
function custom_gift_wrap_message_field($checkout)
{
echo '<div id="custom_gift_wrap_message_field"><h2>' . __('Gift Message') . '</h2>';
woocommerce_form_field('_gift_wrap_message', array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
),
'label' => __('Custom Additional Field') ,
'placeholder' => __('Write your message here...') ,
),
$checkout->get_value('_gift_wrap_message'));
echo '</div>';
}
2) Save your custom field data when placing an order from the checkout page
/**
* Save your checkout page custom field value
*/
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 20, 2);
function custom_checkout_field_update_order_meta( $order_id ) {
update_post_meta($order_id, '_gift_wrap_message', sanitize_text_field($_POST['_gift_wrap_message']) );
}
3) Fetch or display your custom field value
/**
* You can get your gift message directly anywhere from $order_id like this -
*/
<?php echo get_post_meta( $order_id, '_gift_wrap_message', true ); ?>
Example:
Show custom fields value on the welcome page (after checkout page)
/**
* Display Custom Checkout Fields Data on Thankyou page
*/
function gift_wrap_message_display_order_data( $order_id ){ ?>
<table class="shop_table shop_table_responsive additional_info">
<tbody>
<tr>
<th><?php _e( 'Your Gift Message:' ); ?></th>
<td><?php echo get_post_meta( $order_id, '_gift_wrap_message', true ); ?></td>
</tr>
</tbody>
</table>
<?php }
add_action( 'woocommerce_thankyou', 'gift_wrap_message_display_order_data', 20 );
Add the above code to your currently active theme functions.php
Complete refrence URL is - https://www.cloudways.com/blog/how-to-edit-delete-fields-and-email-in-woocommerce-custom-checkout-fields/

Add product link to out of stock email notification in WooCommerce

I need to get the product link into out of stock emails sent to admin.
This code does not have the desired result even though I am, as far as I know, using the right filter hook? Any advice?
add_filter('woocommerce_email_content_no_stock', 'add_product_url_to_out_of_stock_email', 10, 2);
function add_product_url_to_out_of_stock_email( $message, $product ) {
global $product; // with or without this is the same result
return $message ."<br>\n". get_edit_post_link( $product->get_id() );
}
You can add/use WC_Product::get_permalink() – Product permalink to customize $message to suit your needs.
So you get:
function filter_woocommerce_email_content_no_stock ( $message, $product ) {
// Edit message
$message = sprintf( __( '%s is out of stock.', 'woocommerce' ), '' . html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) . '' );
return $message;
}
add_filter( 'woocommerce_email_content_no_stock', 'filter_woocommerce_email_content_no_stock', 10, 2 );
IMPORTANT: This answer will not work by default because wp_mail() is used as mail function, where the content type is text/plain which does not allow using HTML
So to send HTML formatted emails with WordPress wp_mail(), add this extra piece of code
function filter_wp_mail_content_type() {
return "text/html";
}
add_filter( 'wp_mail_content_type', 'filter_wp_mail_content_type', 10, 0 );
Related: Adding product hyperlink to low stock notification email in WooCommerce

Woocommerce: Remove item from cart hook

I have following hook which is working fine, it does add the remove icon in checkout page to allow remove item from cart. However it does hide the product name in cart page. How to make it work only in checkout page but not the cart, or how to stop this hook from hiding product name in cart page ?
add_filter('woocommerce_cart_item_name', 'njengah_filter_wc_cart_item_remove_link', 10, 3);
function njengah_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key)
{
if (is_checkout()) {
$product_name .= apply_filters('woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url(wc_get_cart_remove_url($cart_item_key)),
__('Remove this item', 'woocommerce'),
esc_attr($cart_item['product_id']),
esc_attr($cart_item['data']->get_sku())
), $cart_item_key);
return $product_name;
}
}
When using filter you must return.
add_filter('woocommerce_cart_item_name', 'njengah_filter_wc_cart_item_remove_link', 10, 3);
function njengah_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key){
if (is_checkout()) {
$product_name .= apply_filters('woocommerce_cart_item_remove_link', sprintf(
'×',
esc_url(wc_get_cart_remove_url($cart_item_key)),
__('Remove this item', 'woocommerce'),
esc_attr($cart_item['product_id']),
esc_attr($cart_item['data']->get_sku())
), $cart_item_key);
}
return $product_name;
}

Hide/Disable WordPress Editor and Make Elementor Default Editor

I'm working with Elementor, I want to hide/disable the WordPress Editor option to edit any page/post instead I want to show only the Edit with Elementor option i.e I want to make Elementor my Default Editor.
In Image, You can see that there is an option "back to WordPress Editor" I want to show here only "Edit With Elementor" so that When I have selected a category and other stuff then I can edit it with Elementor.
How Can I do this?
Someone made a plugin for that here : Make Elementor your default editor
However, if you need more control over the code, this is how it was made :
<?php
/**
* Make Elementor the default editor, not the WordPress Editor (Gutenberg or Classic)
* Clicking the page title will take you to the Elementor editor directly
* Even non-Elementor-edited pages will become Elementor-edited pages now
* You can revert by clicking the "Back to WordPress Editor" button
*
* Author: Joe Fletcher, https://fletcherdigital.com
* URL: https://gist.github.com/heyfletch/7c59d1c0c9c56cbad51ef80290d86df7
* Credit: mjakic https://wordpress.stackexchange.com/questions/178416/how-to-change-the-title-url-on-the-edit-post-screen
* Credit: Aurovrata Venet https://developer.wordpress.org/reference/hooks/post_row_actions/
*/
/** Replace hyperlink in post titles on Page, Post, or Template lists with Elementor's editor link */
add_filter('get_edit_post_link', 'fd_make_elementor_default_edit_link', 10, 3 );
function fd_make_elementor_default_edit_link($link, $post_id, $context) {
// Get current screen parameters
$screen = get_current_screen();
//check if $screen is object otherwise we may be on an admin page where get_current_screen isn't defined
if( !is_object($screen) )
return;
// Post Types to Edit with Elementor
$post_types_for_elementor = array(
'page',
'post',
'elementor_library',
);
// When we are on a specified post type screen
if ( in_array( $screen->post_type, $post_types_for_elementor ) && $context == 'display' ) {
// Build the Elementor editor link
$elementor_editor_link = admin_url( 'post.php?post=' . $post_id . '&action=elementor' );
return $elementor_editor_link;
} else {
return $link;
}
}
/** Add back the default Edit link action in Page and Post list rows */
add_filter( 'page_row_actions', 'fd_add_back_default_edit_link', 10, 2 );
add_filter( 'post_row_actions', 'fd_add_back_default_edit_link', 10, 2 );
function fd_add_back_default_edit_link( $actions, $post ) {
// Build the Elementor edit URL
$elementor_edit_url = admin_url( 'post.php?post=' . $post->ID . '&action=edit' );
// Rewrite the normal Edit link
$actions['edit'] =
sprintf( '%2$s',
esc_url( $elementor_edit_url ),
esc_html( __( 'Default WordPress Editor', 'elementor' ) )
);
return $actions;
}
/** (optional) Remove redundant "Edit with Elementor" link added by Elementor itself */
add_filter( 'page_row_actions', 'fd_remove_default_edit_with_elementor', 99, 2 );
add_filter( 'post_row_actions', 'fd_remove_default_edit_with_elementor', 99, 2 );
function fd_remove_default_edit_with_elementor( $actions, $post ) {
// Rewrite the normal Edit link
unset( $actions['edit_with_elementor'] );
return $actions;
}
/** Alternative: Rewrite just the Edit link, and leave the page title as original */
/** Rewrite the normal Edit link on lists of Pages and replace it with Elementor's edit link */
// add_filter( 'page_row_actions', 'fd_elementor_modify_list_row_actions', 10, 2 );
// add_filter( 'post_row_actions', 'fd_elementor_modify_list_row_actions', 10, 2 );
// function fd_elementor_modify_list_row_actions( $actions, $post ) {
// // Build the Elementor edit URL
// $elementor_edit_url = admin_url( 'post.php?post=' . $post->ID . '&action=elementor' );
// // Rewrite the normal Edit link
// $actions['edit'] =
// sprintf( '%2$s',
// esc_url( $elementor_edit_url ),
// esc_html( __( 'Elementor Editor', 'elementor' ) )
// );
// return $actions;
// }

WooCommerce: Adding custom data to processing order

I need add a custom data on Processing order email, but the data always update after the email is sent, like this:
Order status change ==> Send email ==> Insert data on custom table (plugin)
What I need instead is:
Order status change ==> Insert data on custom table (plugin) ==> Send email.
I have checked and this is done with the following hooked function:
add_action('woocommerce_order_status_changed', 'fun_order_status_changed', 10, 4);
function fun_order_status_changed($order_id, $from_status, $to_status, $order){
// Some code
// Then insert to database
}
How could I do or what files can I need to modify so that first the insert is saved in the database and then the e-mail is sent?
EDIT 1
I put deliberately a var_dump and first execute the mail templeate
You can try to use woocommerce_order_status_pending_to_processing_notification action hook with a lower priority, for example 5. So that it will get processed before the mail is sent.
If you want to add a custom field to the order meta data, send the value of this field with the order confirmation mail, and additionally display it in the order detail and edit screen in the backend, you can use the follwing code. There are multiple steps to be done.
Create a new field to show up in WooCommerce Checkout. Set it as required if you want to make sure, that there is a value entered. For this we are using 'woocommerce_after_checkout_billing_form'. (just a sidenote: If you have other purposes, you can also i.e. use a hidden field and a given value)
Save the value inside order meta data using 'woocommerce_checkout_update_order_meta'
Add the value to the email being send after completing order using 'woocommerce_email_order_meta_keys'
Show the value in the order detail screen in the backend using 'woocommerce_order_details_after_order_table' and for the order edit screen 'woocommerce_admin_order_data_after_billing_address' This will place it below the billing address. Notice: The value will not show up (but still be saved in database), if the order is made within the backend, only works for orders placed in the frontend (Would go beyond the scope now).
In my code example, I did this steps to add a VAT ID field which is important in europe for business to business transactions. The VAT ID is also added to the emails and backend screens.
You can adjust the names (vat_number, or the "mrank" prefixes) to your needs, but remember to keep it consistent.
/**
* VAT Number in WooCommerce Checkout
*/
function mrank_vat_field( $checkout ) {
echo '<div id="mrank_vat_field">';
woocommerce_form_field( 'vat_number', array(
'type' => 'text',
'class' => array( 'vat-number-field form-row-wide') ,
'label' => __( 'VAT-ID' ),
'placeholder' => __( 'Enter number' ),
'description' => __( 'Please enter your VAT-ID' ),
'required' => true,
), $checkout->get_value( 'vat_number' ));
echo '</div>';
}
add_action( 'woocommerce_after_checkout_billing_form', 'mrank_vat_field' );
/**
* Save VAT Number in the order meta
*/
function mrank_checkout_vat_number_update_order_meta( $order_id ) {
if ( ! empty( $_POST['vat_number'] ) ) {
update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'mrank_checkout_vat_number_update_order_meta' );
/**
* Display VAT Number in order details screen
*/
function mrank_vat_number_display_order_details($order){
echo '<p><strong>'.__('VAT-ID').':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
}
add_action( 'woocommerce_order_details_after_order_table', 'mrank_vat_number_display_order_details', 10, 1 );
/**
* Display VAT Number in order edit screen
*/
function mrank_vat_number_display_admin_order_meta( $order ) {
echo '<p><strong>' . __( 'VAT-ID', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->get_id(), '_vat_number', true ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'mrank_vat_number_display_admin_order_meta', 10, 1 );
/**
* VAT Number in emails
*/
function mrank_vat_number_display_email( $keys ) {
$keys['VAT-ID'] = '_vat_number';
return $keys;
}
add_filter( 'woocommerce_email_order_meta_keys', 'mrank_vat_number_display_email' );

Resources