Change wc_empty_cart_message function in WooCommerce 3.1 - php

I've been trying to change my layout for the empty-cart message. I've removed the action, and try to replace it.
I'd like to change the htm structure output from:
<p class="empty-cart"></p>
to:
<div class="col-12 offset-md-1 col-md-10"><p class="empty-cart"></p></div>
My actual code (in functions.php file of my theme):
/** Change the output for empty-cart within a div */
remove_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );
add_action( 'wc_empty_cart_message', 'wc_empty_cart_message', 10 );
function custom_wc_empty_cart_message() {
echo '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">'
. wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) ) . '</p></div>';
}
But this code doesn't work. Does anyone has a suggestions on how to make this work?

Here below is the correct way to make it work:
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'custom_empty_cart_message', 10 );
function custom_empty_cart_message() {
$html = '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">';
$html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your cart is currently empty.', 'woocommerce' ) ) );
echo $html . '</p></div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
To remove empty cart message use just:
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );

Related

Change "[Remove]" link text to an icon for coupon in WooCommerce checkout

I would like to change the "[Remove]" link text to an icon for coupons in WooCommerce checkout page.
Instead of "[Remove]" I would like it to be a trash can icon from Font Awesome.
I have found 2 code snippets to change the text:
function filter_woocommerce_cart_totals_coupon_html( $coupon_html, $coupon, $discount_amount_html ) {
// Change text
$coupon_html = $discount_amount_html . ' ' . __( '[Remove & Re-Calculate]', 'woocommerce' ) . '';
return $coupon_html;
}
add_filter( 'woocommerce_cart_totals_coupon_html', 'filter_woocommerce_cart_totals_coupon_html', 10, 3 );
OR
function filter_woocommerce_cart_totals_coupon_html( $coupon_html, $coupon, $discount_amount_html ) {
// Change text
$coupon_html = str_replace( '[Remove]', '[Remove & Re-Calculate]', $coupon_html );
return $coupon_html;
}
add_filter( 'woocommerce_cart_totals_coupon_html', 'filter_woocommerce_cart_totals_coupon_html', 10, 3 );
These code snippets allow me to change the text but I could not figure out how to add the icon. I would appreciate if someone could help me..
For example you can add a Font Awesome Icon using the following, on your 2nd code snippet:
function filter_woocommerce_cart_totals_coupon_html( $coupon_html, $coupon, $discount_amount_html ) {
// Change returned text
return str_replace( '[Remove]', ' [<i class="fas fa-minus-circle"></i> Remove & Re-Calculate]', $coupon_html );
}
add_filter( 'woocommerce_cart_totals_coupon_html', 'filter_woocommerce_cart_totals_coupon_html', 10, 3 );
Code goes in functions.php file of the active child theme (or active theme). It should work.
You will get in checkout page something like:

How can I override a wordpress parent theme function in my child theme?

I am trying to update the content of this parent theme function in my child theme but as soon as i copy it into the child theme functions.php file, i get this error in the browser "The site is experiencing technical difficulties. Please check your site admin email inbox for instructions."
I just want to update the text in the body of the function. How can i do this please?
function oblique_footer_credits() {
echo '<a href="' . esc_url( __( 'http://wordpress.org/', 'oblique' ) ) . '" rel="nofollow">';
/* translators: WordPress */
printf( __( 'Proudly powered by %s', 'oblique' ), 'WordPress' );
echo '</a>';
echo '<span class="sep"> | </span>';
/* translators: 1 - Theme author 2 - Theme name */
printf( __( 'Theme: %2$s by %1$s.', 'oblique' ), 'Themeisle', 'Oblique' );
echo '</div>';
}
add_action( 'oblique_footer', 'oblique_footer_credits' );

Woocommerce open product link in new tab after "endless scroll" with ajax

I have managed to get all product links on my Woocommerce site to open in a new tab, however, I am using endless scroll with ajax for loading more products and the products that get loaded via endless scroll does not open in a new tab when clicking on them.
Here is my current code for opening products in a new tab;
remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
add_action ( 'woocommerce_before_shop_loop_item', 'chr_function_open_new_tab', 10 );
function chr_function_open_new_tab() {
echo '<a target="_blank" href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">';
}
Any help with this is highly appreciated.
Thanks in advance!
I think that your remove_action cannot take an action. That's why it gives a trouble. So try this code:
add_action('init',function(){ remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 ); } ,0);
function chr_function_open_new_tab() {
echo '<a target="_blank" href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">';
}
Second Other Solution, you can do it with simple jQuery
add_action('wp_footer',function(){
if ( has_term( 'stone', 'product_cat' ) ) {
echo '<script>
//for existing content
jQuery(".woocommerce-LoopProduct-link").attr("target","_blank");
//for content part which comes from AJAX
jQuery( document ).ajaxComplete(function() {
jQuery(".woocommerce-LoopProduct-link").attr("target","_blank");
});
</script>';
}
});
Instead try to use this dedicated filter hook for add to cart button:
// Change loop add to cart ajax button to a linked button to single product pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart', 20, 2 );
function replace_loop_add_to_cart( $html, $product ) {
$link = $product->get_permalink();
$text = __("Read More", "woocommerce");
return '' . $text . '';
}
And here for the product link:
add_filter( 'woocommerce_before_shop_loop_item', 'replace_template_loop_product_link_open', 1 );
function replace_loop_product_link() {
remove_action( 'woocommerce_before_shop_loop_item','woocommerce_template_loop_product_link_open', 10 );
add_action ( 'woocommerce_before_shop_loop_item', 'new_loop_product_link_open', 10 );
}
function new_loop_product_link_open() {
global $product;
echo '<a href="' . esc_url( $product->get_permalink() ) . '" target="_blank" class="woocommerce-LoopProduct-link woocommerce-loop-product__link">';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
But in your case, it could not work as expected, depending on how your custom ajax functionality is implemented. As the problem remains for products loaded via ajax, the target="_blank" need to be implemented in the corresponding script too.
At this point nobody can help you more as we can't guess how this functionality is built.

Additional button in Woocommerce shop page and archives

I'd like to add a button next to "Add to Cart" on the product page that adds "-sample" to the product URL when clicked.
Example:
You're viewing Product 1's page and the URL is "http://www.example.com/shop/product-1/"
When you click on the button, it adds "-sample" to the URL
"http://www.example.com/shop/product-1-sample/"
How can I achieve this?
Thanks
For woocommerce 3+ (only):
In woocommerce 3 you will use woocommerce_after_shop_loop_item action hook instead, as the hook woocommerce_after_add_to_cart_button will not work anymore.
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = $product->get_permalink();
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
Before woocommerce 3:
This is possible using hook woocommerce_after_add_to_cart_button to add your additional button on product pages, using this custom function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $product;
$product_link = get_permalink( get_the_id() );
$sample_link = substr($product_link, 0, -1) . '-sample/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Get a sample", "my_theme_slug" ) . '</a>';
}
This code goes on function.php file of your active child theme or theme.
This code is tested and fully functional.
Based on this: Add a button after add to cart and redirect it to some custom link in WooCommerce
And this: PHP - How to remove all specific characters at the end of a string?
It's been a long time since the original question, but here's a recent update that works for me, WooCommerce 6.3.1:
/* WooCommerce customization */
add_action( 'woocommerce_after_shop_loop_item', 'custom_select_link', 11 );
function custom_select_link() {
global $product;
// Custom "Select" button.
echo '<a class="custom-button" href="' . esc_url( get_permalink( $product->id ) ) . '"><button class="custom-button"> </button></a>';
}
This answer cites an answer in wordpress.stackexchange.

Warning: Cannot modify header information - headers already sent by in wordpress

unable to add contents in pages like about us ,courses,contact us..while adding the content error is displaying
Warning: Cannot modify header information - headers already sent by (output started at /home/phptraining/public_html/wp-content/themes/health-center-lite/functions.php:95) in /home/phptraining/public_html/wp-admin/post.php on line 235
and
Warning: Cannot modify header information - headers already sent by (output started at /home/phptraining/public_html/wp-content/themes/health-center-lite/functions.php:95) in /home/phptraining/public_html/wp-includes/pluggable.php on line 1196
functions.php
<?php /**Includes reqired resources here**/
define('WEBRITI_TEMPLATE_DIR_URI',get_template_directory_uri());
define('WEBRITI_TEMPLATE_DIR',get_template_directory());
define('WEBRITI_THEME_FUNCTIONS_PATH',WEBRITI_TEMPLATE_DIR.'/functions');
define('WEBRITI_THEME_OPTIONS_PATH',WEBRITI_TEMPLATE_DIR_URI.'/functions/theme_options');
require( WEBRITI_THEME_FUNCTIONS_PATH . '/menu/default_menu_walker.php' ); // for Default Menus
require( WEBRITI_THEME_FUNCTIONS_PATH . '/menu/webriti_nav_walker.php' ); // for Custom Menus
require( WEBRITI_THEME_FUNCTIONS_PATH . '/commentbox/comment-function.php' ); //for comments
require( WEBRITI_THEME_FUNCTIONS_PATH . '/widget/custom-sidebar.php' ); //for widget register
//content width
if ( ! isset( $content_width ) ) $content_width = 900;
//wp title tag starts here
function hc_head( $title, $sep )
{ global $paged, $page;
if ( is_feed() )
return $title;
// Add the site name.
$title .= get_bloginfo( 'name' );
// Add the site description for the home/front page.
$site_description = get_bloginfo( 'description' );
if ( $site_description && ( is_home() || is_front_page() ) )
$title = "$title $sep $site_description";
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 )
$title = "$title $sep " . sprintf( _e( 'Page', 'helth' ), max( $paged, $page ) );
return $title;
}
add_filter( 'wp_title', 'hc_head', 10,2 );
add_action( 'after_setup_theme', 'hc_setup' );
function hc_setup()
{ // Load text domain for translation-ready
load_theme_textdomain( 'health', WEBRITI_THEME_FUNCTIONS_PATH . '/lang' );
add_theme_support( 'post-thumbnails' ); //supports featured image
// This theme uses wp_nav_menu() in one location.
register_nav_menu( 'primary', __( 'Primary Menu', 'health' ) );
// theme support
$args = array('default-color' => '000000',);
add_theme_support( 'custom-background', $args );
add_theme_support( 'automatic-feed-links');
require_once('theme_setup_data.php');
require( WEBRITI_THEME_FUNCTIONS_PATH . '/theme_options/option_pannel.php' ); // for Custom Menus
// setup admin pannel defual data for index page
$health_center_lite_theme=theme_data_setup();
function hc_custom_excerpt_length( $length ) { return 50; }
add_filter( 'excerpt_length', 'hc_custom_excerpt_length', 999 );
function hc_new_excerpt_more( $more ) { return '';}
add_filter('excerpt_more', 'hc_new_excerpt_more');
$current_theme_options = get_option('hc_lite_options'); // get existing option data
if($current_theme_options)
{ $hc_lite_theme_options = array_merge($health_center_lite_theme, $current_theme_options);
update_option('hc_lite_options',$hc_lite_theme_options);
}
else
{ add_option('hc_lite_options',$health_center_lite_theme); }
}
/******** health center js and cs *********/
function hc_scripts()
{ // Theme Css
wp_enqueue_style('health-responsive', WEBRITI_TEMPLATE_DIR_URI . '/css/media-responsive.css');
wp_enqueue_style('health-font', WEBRITI_TEMPLATE_DIR_URI . '/css/font/font.css');
wp_enqueue_style('health-bootstrap', WEBRITI_TEMPLATE_DIR_URI . '/css/bootstrap.css');
wp_enqueue_style('health-font-awesome', WEBRITI_TEMPLATE_DIR_URI . '/css/font-awesome-4.0.3/css/font-awesome.min.css');
wp_enqueue_script('health-menu', WEBRITI_TEMPLATE_DIR_URI .'/js/menu/menu.js',array('jquery'));
wp_enqueue_script('health-bootstrap_min', WEBRITI_TEMPLATE_DIR_URI .'/js/bootstrap.min.js');
}
add_action('wp_enqueue_scripts', 'hc_scripts');
if ( is_singular() ){ wp_enqueue_script( "comment-reply" ); }
// Read more tag to formatting in blog page
function hc_content_more($more)
{ global $post;
return ' Read More<i class='fa fa-long-arrow-right'></i>";
}
add_filter( 'the_content_more_link', 'hc_content_more' );
?>
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>
This has nothing to do with functions.php sending headers. Rather, it has to do with output having started (in the functions file) before headers are sent (by other WordPress scripts). What you'll see, if you look on line 95, is the following:
?>
<?php
That blank line between the closing and opening PHP tags is the cause of the error. Instead, you should get rid of both of those tags, so that your code becomes:
add_filter( 'the_content_more_link', 'hc_content_more' );
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
While you're at it, also delete the last line of your functions.php file (the closing ?> PHP tag). It's unnecessary here.
This error is not specific to wordpress. This is a standard error of PHP. This problem is occurring because you are trying to modify headers after an output is generated in that page like php echo or some html is rendered.
I hope this helps you.

Categories