Replace Woocommerce taxonomy archive pages title with ACF custom field - php

I am trying to figure out how to replace "woocommerce_page_title" with an ACF field? If there isn't one then it should fall back to "woocommerce_page_title();". I can't seem to find anything super helpful. Your help would be greatly appreciated.
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $title ) {
$title = get_field("custom_tag_h1");
return $title;
}

For Archives pages taxonomy titles and ACF:
The woocommerce_page_title works for shop page and taxonomy archive pages, so in ACF:
Then in a product category for example (here "Clothing"), you set your custom title:
In the code, you need to get the queried object (the WP_Term object) for the taxonomy archive pages and to set it as following:
add_filter( 'woocommerce_page_title', 'custom_title' );
function custom_title( $page_title ) {
if ( ! function_exists('get_field') || is_search() )
return $page_title;
if ( is_tax() ) {
$term = get_queried_object();
$the_id = $term->taxonomy . '_' . $term->term_id;
} elseif ( is_shop() ) {
$the_id = wc_get_page_id( 'shop' );
}
return get_field( "custom_tag_h1", $the_id ) ? get_field( "custom_tag_h1", $the_id ) : $page_title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.

Related

Hook into a Posts Widget in Elementor

I'm looking for a way to hook into an Elementor Posts Widget to display an extra H2 tag under the posts title for each posts.
I would then get this H2 value from from the single posts ACF field.
From what I am reading else where there are ways to get the whole HTML of the output as string, but that requires a lot of string replace and so not very future proof. Eg:
Hook into elementor widget?
https://developers.elementor.com/docs/hooks/render-widget-content/
If I am using a code like this is there a way to hook this after the Post title? or is string replace the best way to approach this?
function change_heading_widget_content( $widget_content, $widget ) {
if ( 'posts' === $widget->get_name() ) {
$settings = $widget->get_settings();
$post_id = "Somehow get the post id (maybe look for in the $widget_content string per post?)";
if ( ! empty( $settings['link']['is_external'] ) ) {
$widget_content .= '<h2>'. get_field("extra_heading", $post_id) .'<h2>';
}
}
return $widget_content;
}
add_filter( 'elementor/widget/render_content', 'change_heading_widget_content', 10, 2 );
I appreciate all and any help.
Thanks
If you dig into Elementor Pro source code you'll find a good hint:
Dynamic Tags -> ACF Module
get_queried_object()
Or try this:
Dynamic Tags -> ACF Module Rendering
function get_queried_object_meta( $meta_key ) {
$value = '';
if ( is_singular() ) {
$value = get_post_meta( get_the_ID(), $meta_key, true );
} elseif ( is_tax() || is_category() || is_tag() ) {
$value = get_term_meta( get_queried_object_id(), $meta_key, true );
}
return $value;
}
Or just use get_field('my-field') without $post_id

Add CSS to WooCommerce product pages only if product is not on sale

I need to add some CSS to hide some elements/classes on the page if the product is NOT on sale.
From another function I have, I am assuming I can check if $args['meta_key'] = '_sale_price'; is blank and if it then adds the CSS.
How can I use this in my functions.php?
add_action( 'wp', 'add_cssif_product_not_onsale' );
function add_cssif_product_not_onsale() {
if ( is_product() ) {
global $post;
$product = wc_get_product( $post );
if ( !$product->is_on_sale() )
echo "<style>body{display:none}</style>";
}
}

Changing the titles on My Account pages in Woocommerce

I've seen loads of example of how to re-order / change the navigation and page with the WooCommerce my account dashboard. But i can't for the life of me work out how to change the main titles for each section (My Account, Orders, Downloads, Addresses etc).
I've searched through the templates but no joy. I've tried using conditional php comments to echo the titles for the correct page. But it doesn't work because my account section uses endpoints. I've tried adding a filter, but no joy. Anyone got any idea how i change these titles?
Thanks
It can be done using the composite filter hook woocommerce_endpoint_{$endpoint}_title.
For example if you need to change the My Account "** Account details**" title you will use (where the endpoint is edit-account):
add_filter( 'woocommerce_endpoint_edit-account_title', 'change_my_account_edit_account_title', 10, 2 );
function change_my_account_edit_account_title( $title, $endpoint ) {
$title = __( "Edit your account details", "woocommerce" );
return $title;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Found I can do using woo_endpoint_title filter.
This worked for me to rename the titles of all my account pages:
function wpb_woo_endpoint_title( $title, $id ) {
if ( is_wc_endpoint_url( 'orders' ) && in_the_loop() ) {
$title = "Deine Buchungen";
}
elseif ( is_wc_endpoint_url( 'edit-address' ) && in_the_loop() ) {
$title = "Deine Rechnungs- & Behandlungsadresse";
}
elseif ( is_wc_endpoint_url( 'payment-methods' ) && in_the_loop() ) {
$title = "Deine Bezahlmethoden";
}
elseif ( is_wc_endpoint_url( 'edit-account' ) && in_the_loop() ) {
$title = "Dein Nutzerkonto";
}
return $title;
}
add_filter( 'the_title', 'wpb_woo_endpoint_title', 10, 2 );

Add a body class for product category archive page in Woocommerce

I want to add product category slug in the body class on product category archive pages.
Here is an example of what I would like (url of the product category page example):
https://example.com/product-category/canon/… So I would like "canon" in the body class.
Your example link doesn't seem to work; however, WooCommerce should already be adding a term-specific class to your category pages: something along the lines of archive tax-product_cat term-{slug} term-{id} to a product category page. Taking your example link, and assuming the term ID is 7 (for example), the body class would include:
tax-product_cat term-7 term-canon
So if you need to access it via CSS/jQuery/whatever, you can use the selector body.term-canon.
You can use page slug in body class, See this code
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
add_filter( 'body_class', 'add_slug_body_class' );
for this you need to add the below code in your functions.php file in your activated theme.
add_filter( 'body_class', 'product_cats_css_body_class' );
function product_cats_css_body_class( $classes ){
if( is_archive( 'product-cat' ) )
{
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = $custom_term->slug;
}
}
}
return $classes;
}
this will add the category slug at the last of body class.
Even if Woocommerce embed as body class the product category term slug as term-canon, you can easily add just canon using the dedicated WordPress body_class action hook, with Woocommerce is_product_category() conditional function and WordPress get_queried_object() this way:
add_filter( 'body_class', 'product_category_slug_to_body_class', 99, 1 );
function product_category_slug_to_body_class( $classes ){
if( is_product_category() ){
$classes[] = get_queried_object()->slug;
}
return $classes;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

woocommerce add to cart button on single product page

On WooCommerce, I would like a custom add to cart button redirection for specific categories to contact us page (when customer click on the add to cart button in single product pages).
This is my code:
add_filter( 'woocommerce_add_to_cart_redirect', 'rv_redirect_to_url' );
function rv_redirect_to_url() {
global $woocommerce, $post;
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
$rv_woo_redirect_url = get_post_meta( $product_id, '_rv_woo_product_custom_redirect_url', true );
if ( ! empty( $rv_woo_redirect_url ) ) {
wp_redirect( esc_url( $rv_woo_redirect_url ) ); exit;
}
}
How can I change my code to get it working only for defined product categories?
Updated (the right custom url when it's a defined product category)
Using a custom function hooked in woocommerce_add_to_cart_redirect filter hook, that will redirect customer when a product is added to cart (for defined product category(ies)):
add_filter( 'woocommerce_add_to_cart_redirect', 'conditional_add_to_cart_redirection', 99, 1 );
function conditional_add_to_cart_redirection( $url ) {
// ==> HERE define your product category or categories in the array
$category = array( 'clothing', 'music' );
if ( ! isset( $_REQUEST['add-to-cart'] ) ) return $url; // Very important!
// When it's available (on add to cart click), get the product ID
$product_id = absint( $_REQUEST['add-to-cart'] );
// Get the custom url from product post meta data
$custom_url = get_post_meta( $product_id, '_rv_woo_product_custom_redirect_url', true );
// Exit to normal, If the custom URL redirection is not set in the product
if( empty( $custom_url ) ) return $url;
// Custom redirection only for your defined product category(ies)
if( has_term( $category, 'product_cat', $product_id ) ){
// Clear add to cart notice (with the cart link).
wc_clear_notices();
$url = $custom_url; // Updated here
}
return $url;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested on Woocommerce 3+ and works
Add to cart redirection will not work with ajax add to cart on shop and archives pages. So you will have to choose between that 2 options for shop and archives pages:
Disable ajax add-to-cart on shop and archives pages ( WC settings > Products > Display ).
Add the following code to replace the add to cart button, by a button linked to the product:
This 2nd option seems to be the best (as this is conditional on some products only):
// Conditionally changing add to cart button link and text on shop and archives
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( $product->is_type( 'variable-subscription' ) || $product->is_type( 'variable' ) ) return $button;
// ==> HERE define your product category or categories in the array
$category = array( 'clothing', 'music' );
// Check that the custom url from product post meta data is not empty
$custom_url = get_post_meta( $post->ID, '_rv_woo_product_custom_redirect_url', true );
if( empty( $custom_url ) ) return $button;
// Check if the current product has a defined product category(ies)
if( ! has_term( $category, 'product_cat', $post->ID ) ) return $button;
$button_text = __( 'View product', 'woocommerce' );
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
return $button;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested on Woocommerce 3+ and works
If the product is not meant to be purchased outright, then I would filter woocommerce_is_purchasable and replace the add to cart button with a button-styled link to your contact form.
function so_46395830_not_purchasable( $is_purchasable, $product ) {
$rv_woo_redirect_url = $product->get_meta( '_rv_woo_product_custom_redirect_url', true );
if ( ! empty( $rv_woo_redirect_url ) ) {
add_action( 'woocommerce_single_product_summary', 'so_46395830_redirect_to_contact' );
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'so_46395830_not_purchasable', 10, 2 );
function so_46395830_redirect_to_contact( $url ) {
global $product;
$rv_woo_redirect_url = $product->get_meta( '_rv_woo_product_custom_redirect_url', true );
echo sprintf( '<a class="button" href="%s">%s</a>', esc_url( $rv_woo_redirect_url ), __( 'Contact Us for Info', 'your-plugin-textdomain' ) );
}

Categories