Issue with output of WooCommerce hooks on cart and checkout page - wordpress

I am trying to add some text below the applied coupon on my cart page, but for some reason, I can only get it to display above the table, as per the screenshot.
I even created a fresh install with the twentytwentyone theme, and no other plugins installed besides woocommerce.
These are the code that I am using:
add_action('woocommerce_cart_totals_before_shipping', 'bb_before_shipping');
function bb_before_shipping() {
echo 'woocommerce_cart_totals_before_shipping';
}
And
add_action('woocommerce_before_cart_totals', 'apply_product_on_coupon');
function apply_product_on_coupon() {
global $woocommerce;
if ( ! empty( $woocommerce->cart->applied_coupons ) ) {
echo 'woocommerce_before_cart_totals';
}
}
Any help will be appreciated!

You are using the correct hook for the cart page, but the output is part of a HTML table.
So you get:
// Cart
function action_woocommerce_cart_totals_before_shipping() {
echo '<tr><td>woocommerce_cart_totals_before_shipping</td></tr>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'action_woocommerce_cart_totals_before_shipping' );
// Checout
function action_woocommerce_review_order_before_shipping() {
echo '<tr><td>woocommerce_review_order_before_shipping</td></tr>';
}
add_action( 'woocommerce_review_order_before_shipping', 'action_woocommerce_review_order_before_shipping' );

Related

Remove 'Move to Trash' under woocommerce order action for shop manager

According to this post, custom post type, hide or disable the trash button in publish meta box, this is specifically removing for post. I couldn't find any post solutions relating to remove 'Move to trash' under woocommerce order action. Does this code apply same to removing this button on woocommerce?
function my_custom_admin_styles() {
?>
<style type="text/css">
.post-type-inhoud form #delete-action{
display:none;
}
</style>
<?php
}
add_action('admin_head', 'my_custom_admin_styles');
How do I apply this to woocommerce removing 'move to trash' button under order action and only specifically for shop manager.
To prevent deleting post/order/custom post is capability per user role. To prevent shop_manager role from deleting orders you must remove that capability. All other solutions will visualy work but someone who knows his stuff still can delete order if he wants. So place the following function in your functions.php
function wp23958_remove_shop_manager_capabilities() {
$shop_manager = get_role( 'shop_manager' ); // Target user role
//List of capabilities which we want to edit
$caps = array(
'delete_shop_orders',
'delete_private_shop_orders',
'delete_published_shop_orders',
'delete_others_shop_orders',
);
// Remove capabilities from our list
foreach ( $caps as $cap ) {
$shop_manager->remove_cap( $cap );
}
}
add_action( 'init', 'wp23958_remove_shop_manager_capabilities' );
Bonus How to know what capabilities current user have
function wp32985_check_user_capabilities() {
$data = get_userdata( get_current_user_id() );
if ( is_object( $data) ) {
$current_user_caps = $data->allcaps;
// print it to the screen
echo '<pre>' . print_r( $current_user_caps, true ) . '</pre>';
}
}
add_action( 'init', 'wp32985_check_user_capabilities' );
The correct CSS class to target is: .order_actions li #delete-action
The following will hide it for all backend users (admins, shop managers, etc.):
add_action( 'admin_head', 'bbloomer_css_wp_admin' );
function bbloomer_css_wp_admin() {
echo '<style>.order_actions li #delete-action { display: none; }</style>';
}
If you only wanted to apply this CSS for shop managers (and therefore not for admins for example), you need conditional logic, and specifically you need to use the inbuilt WooCommerce function wc_current_user_has_role():
add_action( 'admin_head', 'bbloomer_css_wp_admin' );
function bbloomer_css_wp_admin() {
if ( ! wc_current_user_has_role( 'shop_manager' ) ) return;
echo '<style>.order_actions li #delete-action { display: none; }</style>';
}

Woocommerce hook: How to add content inside the feature products on home page?

I'm trying to add the custom message inside the woocommerce website "Featured Product" section.
Like content goes inside the red box:
I've found the hook like below but it does not have any effect:
add_action('storefront_homepage_after_featured_products_title','homepage',15);
function homepage() {
echo "test";
}
I also try below code but that only display message under catalogue page.
add_action( 'woocommerce_after_shop_loop_item', 'catalogue_message', 15);
function catalogue_message() {
global $product;
$item_cost = $product->get_price();
if($item_cost != '') {
echo "TEST";
}
}
So not sure what's the name of woocommerce hook that can add the custom message on home page - Featured Product section.
Can anyone please let me know the name of the hook.
Thanks:)
Try this code to add html after price in featured product.
add_filter( 'woocommerce_get_price_html', 'theme_add_description_after_or_after_price', 2, 10 );
function theme_add_description_after_or_after_price($price, $product){
$description = ($product->is_featured()) ? 'Your HTML or text' : '';
return $price . $description ;
}

Remove JS file from Woocommerce Product edit pages

I am getting this error "Uncaught Error: Option 'ajax' is not allowed for Select2 when attached to a element." while updating Product Variation.
Actually there are 2 select2.js files, one from Woocommerce and other from 'WR PageBuilder' plugin. While I am renaming 'WR PageBuilder' select2.js file then its working fine. But that file is required for Editor.
I want to remove that js file only from Product pages.
I did 'wp_deregister_script()' and 'wp_dequeue_script()' but nothing happened.
Here is my code:
add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
}
if( 'product' == $typenow ){
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
}
}
function deregister_my_script() {
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
can anyone give me a solution?
This won't work because you are using the actions wrong.
Look here for the correct usage of action hooks:
Hooks in Wordpress
You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook.
Try taking admin_enqueue_scripts outside of the admin_init hook like this:
global $typenow;
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
function deregister_my_script() {
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if( 'product' == $typenow ){
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
}

WP woocommerce change "Proceed to checkout" buttons URL

I want to change to URL of the "Proceed to checkout" button. I wanna do this to to check if there's a need for my product, so my customers shouldn't be able to really buy it. Therefore I want to direct them not to the checkout but to a customized page of mine.
Thanks a lot.
Josh
It should be available at below path
wp-content/plugins/woocommerce/templates/cart/proceed-to-checkout-button.php
the best way is the woocommerce hooks
Try to use this code (put it in your functions.php theme file).
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$url = "your checkout url ";
return $url;
}
You can customize it by adding some conditions to change the checkout url
for example:
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$allowed_countries = array('FR');
$customer_country = WC()->customer->get_default_country();
if( !in_array( $customer_country , $allowed_countries ) ) {
$url = wc_get_page_permalink( 'other-checkout' );
}
return $url;
}
Change Proceed To Checkout Text In WooCommerce
* Change Proceed To Checkout Text in WooCommerce
* Place this in your Functions.php file
This is the new method, for version 2.3.8 of WooCommerce and forward.
<?php
function woocommerce_button_proceed_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}

Disable WooCommerce SKU on Product Page

I have a WooCommerce store and I don't want to display the SKU on any single product page. Looking at their code, I found this filter:
/**
* Returns whether or not SKUS are enabled.
* #return bool
*/
function wc_product_sku_enabled() {
return apply_filters( 'wc_product_sku_enabled', true );
}
and I attempted to override it with this line of code I placed in a custom plugin:
apply_filters( 'wc_product_sku_enabled', false );
I also tried placing the apply_filter inside an action function for woocommerce_product_meta_start which fires right before but it still renders the SKU on the product page. Any ideas?
I think you shoul try with this:
add_filter( 'wc_product_sku_enabled', '__return_false' );
That will remove sku from all woo, back and front end. You can always hide it just by CSS if need it on admin.
The easiest way is with CSS:
.sku_wrapper {
display:none;
}
A more robust approach is to recreate the woocommerce template woocommerce/templates/single-product/meta.php in your own theme and simply comment out the line:
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span>
To recreate a woocommerce template in your own theme, see:
http://docs.woothemes.com/document/template-structure/
Hiding the SKU/UGS by using cSS is not an efficient solution because it will be still part of the HTML code.
In order to hide it from the product single page and keep it in the admin page, you have to add this code in the child (or parent if you don’t have the child) functions.php :
// Remove the Product SKU from Product Single Page
add_filter( 'wc_product_sku_enabled', 'woocustomizer_remove_product_sku' );
function woocustomizer_remove_product_sku( $sku ) {
// Remove only if NOT admin and is product single page
if ( ! is_admin() && is_product() ) {
return false;
}
return $sku;
}
Make sure also in the product php page (it can have a different name depending on the theme you use) to have this condition to show the SKU in the product single page:
if (wc_product_sku_enabled() && $product->get_sku()) { // HTML code that shows the SKU in the product single page}
Make Sure to remove it from the frontend only by using this code on function.php usually you can edit the function file on theme editor
add_filter( 'wc_product_sku_enabled', 'my_remove_sku', 10 );
function my_remove_sku( $return, $product ) {
if ( !is_admin() && is_product() ) {
return false;
} else {
return true;
}
}
If you don’t need to use SKUs at all in your shop, you can disable them completely by using this plugin. simply install this plugin. https://wordpress.org/plugins/woocommerce-remove-sku/

Resources