When changing woocommerce title hook the first item doesn't change - php

I have a strange behaviour that I don't understand
I've changed the woocommerce_shop_loop_item_title hook to add a link to the title of the product. This is my code inside functions.php
// Add HREF TO TITLE
function abChangeProductsTitleHook(){
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
}
add_action( 'woocommerce_shop_loop_item_title', 'abChangeProductsTitleHook' );
function abChangeProductsTitle() {
echo '<h2 class="woocommerce-loop-product_title">' . get_the_title() . '</h2>';
}
It works perfectly on all the products except the first one.
I've also made a similar change to another hook to change the thumbnail image to a background image and also this one doesn't work on the first product. It's always the first product even if I change the order of the products.
Below you see a screenshot of the the first row of products on the page and that the first one is displayed differently
It would be really helpful if anyone knows that problem or can point me in the right direction .
Thank you very much
Alex

The way you are removing and adding the woocommerce_shop_loop_item_title is the problem. Try it this way.
remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );
function abChangeProductsTitle() {
echo '<h2 class="woocommerce-loop-product_title">' . get_the_title() . '</h2>';
}

Although the accepted answer works, but why do we have to remove the title and then add it back in again? We could simply filter it in the first place.
add_filter('the_title', 'your_shop_custom_title', 20, 2);
function your_shop_custom_title($the_title, $id)
{
if (is_shop() && get_post_type($id) == 'product') : // runs only on the shop page
$the_title = '' . $the_title . '';
endif;
return $the_title;
}

Related

How to change Woocommerce single product page title to real product title?

need wordpress advice. I searched for quite some time now but i can't find anything.
I am using organic 01 theme from xstore and i can't find any way to change the product page title to the real product title. Title should match the product .. now string 'Product Information' is written on all pages in that place.
I tried in child theme functions.php write php code but I didn't get it, couldn't even to remove the title for test purpose, which is very simple and i should succeed with remove_action(bla bla..) .. why is that?
In the beginning i just tried to change the title on all pages with the string in language suitable for the page but i couldn't do it either.
remove_action('woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title',5);
add_action('woocommerce_shop_loop_item_title','title',5);
function title()
{
echo 'Produkto informacija';
}
To better understand what I am trying to do I attach a photo
You need to try with high priority
<?php
// Frontend: On shop and archives pages
add_action( 'woocommerce_shop_loop_item_title', 'example_replace_title', 9 );
function example_replace_title() {
global $product;
// Remove the product title
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
$title = "My Sample Text";
// Output
echo '<h2 class="' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . $title . '</h2>';
}

How to hook an ACF field into the bottom of the "after_shop_loop" section of a Woocommerce category page after pagination

I found the correct hook of where I want the field to show which is below
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
This code works exactly how'd I'd like mine to work but with an ACF text area field. Ideally, I'd like it to show under the pagination. This hook is currently showing above the pagination.
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
function after_shop_loop() {
echo "<div class='best-seller'>
<h3>If you need help or have a question for Customer Service, please visit the <a href='#'>Help Section.</a></h3>
</div";
}
I'd like to create a function to bring in an ACF text area that I've added to the backend for all brands that are coming from WooCommerce taxonomy pages "Brand".
This is what I have so far with no luck. Is there something I'm doing wrong or something special you have to do to get ACF to show on Taxonomy pages? Any help is appreciated. Thanks in advance...
add_action( 'woocommerce_after_shop_loop', 'after_shop_loop', 1 );
function custom_after_shop_loop() {
$brand_content = get_field('brand_bottom');
if( !empty($brand_content) ) {
echo '<div class="brandbottom">' . $brand_content . '</div>';
}
}
Screenshots from my custom field.
Missing "custom_" in the name of your function and you have to get the current taxonomy term. Tested and works
add_action( 'woocommerce_after_shop_loop', 'custom_after_shop_loop', 1 );
function custom_after_shop_loop() {
$term = get_queried_object();
$brand_content = get_field('brand_bottom', $term);
if( !empty($brand_content) ) {
echo '<div class="brandbottom">' . $brand_content . '</div>';
}
}

Adding an image page banner to shop page

I am using WordPress 4.9.6.
I have set the shop page to be the home-page.
How do I add a page banner to the shop page. I would like to add it just above the breadcrumb trail.
I have tried adding this to the following page archive-product.php
if (is_shop()) {
$args = array('taxonomy' => 'product_cat');
$product_categories = get_categories( $args );
$term_id = $product_categories[0]->term_id;
$content = get_term_meta($term_id, 'cat_meta');
if(isset($content[0]['cat_header'])){
echo do_shortcode($content[0]['cat_header']);
}
}
Unfortunately, not able to add any image to the page.
You can achieve using 2 methods.
1) Add your static image directly at the beginning of archive-product.php
echo "<img src='{YOUR_IMAGE_PATH}'>";
2) Add filter in your theme's functions.php file.
add_action ('woocommerce_archive_description' , 'shop_banner',99);
function shop_banner() {
echo '<img src="{YOUR_IMAGE_PATH}" >';
}
I'm not so sure if I understand exactly what you want. But this is what I understand so far.
If you want to display an Static image banner above the breadcrumbs in your Shop Page.
You could use the woocommerce_before_main_content action.
function BannerShop(){
if(is_shop()){
echo '<img src="https://localhost/demosite/wp-content/uploads/2015/06/512x356.png" >';
}
}
add_action( 'woocommerce_before_main_content', 'BannerShop', 10 );
Here i show the before and after. BTW I don't know what theme are you using so it may be displayed different.
Before
https://i.stack.imgur.com/Mv2YK.jpg
After https://i.stack.imgur.com/nTfCa.jpg

wordpress plugin function firing twice

Followed through on this post for manipulating woocommerce menu.
This plugin code:
function product_subcategories( $args = array() ) {
$parentid = get_queried_object_id();
$args = array(
'parent' => $parentid
);
$terms = get_terms( 'product_cat', $args );
if ( $terms ) {
echo '<ul class="product-cats">';
foreach ( $terms as $term ) {
echo '<li class="category">';
woocommerce_subcategory_thumbnail( $term );
echo '<h2>';
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
echo $term->name;
echo '</a>';
echo '</h2>';
echo '</li>';
}
echo '</ul>';
}
}
add_action( 'woocommerce_before_shop_loop', 'product_subcategories', 50 );
Fires twice.
Why is that??
Thanx
EDIT
Original post link: Display WooCommerce Categories, Subcategories, and Products in Separate Lists
Woocommerce does allow you to register two events with the same tag source. Thus, if the file is loaded twice (e.g. if you use include/require instead of include_once/require_once) it can cause this problem: check it anyhow(!).
The hook itself is called twice in woocommerce, so technically it is possible that the hook is called twice. I do however not know if both templates are loaded in the same call... You might however have registered the call twice w/o knowing: thus check your code for another call to this function, perhaps with a different tag (other than woocommerce_before_shop_loop).
Update:
With your code you are just doing what WooCommerce is already doing, that's why you are seeing duplicated category/subcategory menu items and you say that it's "fired twice".
Anyway, if you keep your code, and then you want to remove that WooCommerce generated category/subcategory items, you can use this hooked function:
add_filter('woocommerce_product_subcategories_args', function( $args ){
$args['taxonomy']= '';
return $args;
});
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This will remove the WooCommerce duplicated items…
Original answer:
The woocommerce_before_shop_loop hook:
has no arguments (no need to add $args = array() )
is called many times, but only fired once. Check it replacing your code by this:
add_action( 'woocommerce_before_shop_loop', function(){
wc_print_notice('test message', 'notice');
}, 50 );
Kindly, as your question is not very explicit and detailed, we don't really know what is your problem and can't help you further than that…
While the previous answers are no doubt accurate, I don't think they are truly the reason for multiple calls. I believe the true reason is that you're simply receiving multiple HTTP requests when, to your eyes, it seems like there should only be one. Remember, your plugin is going to load upon every interaction with WP, not just the ones for your application. So /wp-cron.php links, for example, will trigger your plugin "invisibly", as will many other requests like Ajax or JSON calls. Please see https://wordpress.stackexchange.com/questions/170768/wordpress-plugin-executing-code-twice#answer-170784.

Trim Related Products Title Length - Woocommerce

I currently have an issue with trimming the length of the product title in the "Related Products" section of my single product page.
I have set up a custom hook pointing to a custom template for this. The only problem is that it isn't trimming any of it - it is still printing the whole title.
I have tried to use get_the_title() and it doesn't print anything to screen, so in the code below I have tried to pass the title as a string and not an array.
What am I doing wrong?
<?php $title = the_title('<h3 class="product_title entry-title">', '</h3>');
$text = wp_trim_words($title, 2, '...')
?>
<?php echo $text; ?>
Thanks in advance
Regards
Michael
the_title() will be auto print as you don't need to echo it.
You need to use filter for title as the following in functions.php,
function trim_title( $title ) {
$title = wp_trim_words( $title , 40, '...' );
return $title;
}
add_filter( 'the_title', 'trim_title', 10, 1 );
This solution doesn't work in up-sells.php even if it's almost the same as related.php , instead of this whole function trim_title( $title ) in both files after $post_object = get_post( $upsell->get_id() ); (or $related_product) I added $post_object->post_title = substr( $post_object->post_title, 0, 50) . '…'; and it works. It limits to 50 characters, you can change.

Categories