Rename My account tabbed menu items in Woocommerce - php

I am currently using Wordpress Version 4.9.8 and enabled Woocommerce plugin. I need to rename the tab name "Dashboard" to "My Rewards" in My account pages.
I found the code below, but it doesn't seem to be working:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'dashboard' ) && in_the_loop() ) { // add your endpoint urls
$title = "My Rewards"; // change your entry-title
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );
Any help is appreciated.

Try the following instead (Works in Woocommerce since version 2.6):
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items', 22, 1 );
function custom_my_account_menu_items( $items ) {
$items['dashboard'] = __("My Rewards", "woocommerce");
return $items;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

Change WooCommerce Order pay page title

Attempting to change the title of the "pay for order" page / "customer payment page"
https://url.com/checkout/order-pay/753/?pay_for_order=true&key=wc_order_xxxxxx
Below is not currently working, it is modified from changing the title on the thank you page.
add_filter( 'the_title', 'woo_title_pay_order', 10, 2 );
function woo_title_pay_order( $title, $id ) {
if ( function_exists( 'is_pay_for_order_page' ) &&
is_pay_for_order_page() && get_the_ID() === $id ) {
$title = "Checkout";
}
return $title;
}
Updated
You can use the following to change "Pay for order" page title:
add_filter( 'the_title', 'change_pay_for_order_title' );
function change_pay_for_order_title( $title ) {
if ( is_wc_endpoint_url( 'order-pay' ) ) {
return __('Checkout', 'woocommerce');
}
return $title;
}
Or also the following code based on 'order-pay' endpoint (but changes the breadcrumb):
add_filter( 'woocommerce_endpoint_order-pay_title', 'change_checkout_order_pay_title' );
function change_checkout_order_pay_title( $title ) {
return __( "Checkout", "woocommerce" );
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related: Set My Account custom items endpoints titles in WooCommerce

On a WordPress page template, what code can customize title and meta description?

On custom-page-template.php, what code can customize the section ( and <meta name=”description”) for all pages which are based on this template?
I tried using the following but they nothing on a page template. (They work on functions.php though).
add_filter( 'document_title_parts', 'custom_document_title_parts' );
add_action( 'wp_head', 'my_add_meta_description');
Any idea, what code can customize and in a custom-page-template.php?
Thanks.
Use this in functions.php: (change page template's name in is_page_template)
add_filter( 'document_title_parts', function( $title_parts_array ) {
if ( is_page_template('custom.php') ) {
$title_parts_array['title'] = 'New title';
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description");</script>';
}
return $title_parts_array;
} );
Or you can use this before get_header() in your custom page template
function new_page_title() {
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description2");</script>';
return 'New Title2';
}
add_action( 'pre_get_document_title', 'new_page_title' );

WooCommerce remove shopping cart by user role

In Woocommerce, I have a function that replace add to cart button by a linked button to the product in shop and archive pages:
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! current_user_can('customer') ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
I would like to remove the add to cart button on all pages if a user is not logged in as a customer.
Can anyone help please?
Instead of your actual code, try the following that will do everything everywhere and will remove add to cart button when user is not logged in:
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
if ( ! is_user_logged_in() )
$purchasable = false;
return $purchasable;
}
Code goes in function.php file of your active child theme (or active theme).

Custom templates in Woocommerce 3

I'm trying to make a separate template for only one product with ID 5555. To delete a photo from its page and change the blocks structure. Overriding this file affects all product pages.
wp-content\plugins\woocommerce\templates\content-single-product.php
The logical solution is to add a condition: if the product single prpduct page have ID 5555, then another template is used for its page.
I tried this option, but it does not work.
/**
* Custom single product template.
*/
add_filter( 'single_template', 'get_custom_post_type_template' );
function get_custom_post_type_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/single-template.php';
}
return $single_template;
}
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'slug' ) ) {
$new_template = locate_template( array( 'single-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
Or option 2: remove this hooks:
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
from only one specific product page:
Custom templates specifically since Woocommerce 3.3
Since Woocommerce 3.3 Wordpress hook template_include doesn't seem to work always, as you could see in a lot of recent related threads in StackOverFlow and somewhere else too.
The specific filter hooks related to templates for Woocommerce:
wc_get_template
wc_get_template_part
woocommerce_locate_template
woocommerce_template_path (The theme's override templates folder)
Case 1. Using wc_get_template_part (your case):
If you look at single-product.php template, the content-single-product.php template is loaded with the following at this line:
<?php wc_get_template_part( 'content', 'single-product' ); ?>
So we will use the hook wc_get_template_part.
Let say that your custom template replacement file is named content-single-product-custom.php and located in the woocommerce folder of your active child theme (or active theme). To use this custom template for a specific product ID (5555) you will need the following:
add_filter( 'wc_get_template_part', 'custom_wc_template_part', 10, 3 );
function custom_wc_template_part( $template, $slug, $name ) {
// The specific product ID
$product_id = 5555;
// The custom template file name
$custom_template_name = 'content-single-product-custom.php';
// For a specific product ID and content-single-product.php template
if( get_the_ID() == $product_id && $slug == 'content' && $name == 'single-product' ){
$template = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $template;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.
Case 2. Using wc_get_template:
If templates is loaded via wc_get_template() function, like for example single-product/meta.php and your custom replacement template is named single-product/custom-meta.php (and located in the woocommerce folder of your active child theme). To use this custom template for a specific product ID (5555) you will need the following:
add_filter( 'wc_get_template', 'custom_wc_template', 10, 5 );
function custom_wc_template( $located, $template_name, $args, $template_path, $default_path ) {
// The specific product ID
$product_id = 5555;
// The custom template file name and path
$custom_template_name = 'single-product/custom-meta.php';
if( is_product() && get_the_ID() == 37 && $template_name == 'single-product/meta.php'){
$located = trailingslashit( get_stylesheet_directory() ) . 'woocommerce/' . $custom_template_name;
}
return $located;
}
This code goes in function.php file of your active child theme (or active theme). Tested and works.
About overriding woocommerce templates:
Official documentation: Template structure & Overriding templates via a theme
WooCommerce action hooks and overriding templates
Overriding specific third party Woocommerce plugin templates
Add this to your theme's 'functions.php' where 5 is product ID and woocommerce/single-product-watermelon.php is custom product template.
add_filter( 'template_include', 'watermelon_template_include' );
function watermelon_template_include( $template ) {
if ( is_singular('product') && get_the_ID() == 5 ) { //if category, change has_term( 'watermelon', 'product_cat') instead of get_the_ID()
$template = get_stylesheet_directory() . '/woocommerce/single-product-watermelon.php';
}
return $template;
}

WooCommerce checkout page change Your order details Shipping text

woocommerce checkout page change Your order details Shipping text label.
i want attached image please check it
You can use the WordPress gettex() function that will replace the concerned text in checkout page:
add_filter( 'gettext', 'customizing_checkout_text', 10, 3 );
function customizing_checkout_text( $translated_text, $untranslated_text, $domain )
{
if ( $untranslated_text == 'Shipping' && is_checkout() ) {
$translated_text = __( 'Here goes your custom text', $domain );
}
return $translated_text;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
You can try this to resolve your issue:
Put this code in your theme's function.php file.
add_filter('ngettext', 'translate_shipping');
function translate_shipping($translated) {
$translated = str_ireplace('Shipping', 'Your Custom Text', $translated);
return $translated;
}

Categories