I want to echo some text before the cart on a product archive page (shop listings) but ONLY on the products that are by a certain author.
I have this code which shows the information I want where I want it:
add_filter( 'woocommerce_loop_add_to_cart_link', 'tomich_add_melody_vendor', 10, 3 );
function tomich_add_melody_vendor ( $add_to_cart_html, $product, $args )
{
$before = 'Shop: <img class="vendor-image-tomich" src="https://melodypayne.com/wp-content/uploads/2019/06/cropped-Melody-Payne-Store-Logo-Pic-2019.png" alt="Melody Logo"><span class="tomich-shop-title"><a href="/shop/melodypayne">Melody Payne</a></span></br></br>'; // Some text or HTML here
$after = ''; // Add some text or HTML here as well
return $before . $add_to_cart_html . $after;
}
But now I need to figure out how to restrict it to just show on products by a certainauthor
, either bynickname
orID
.
I tried some variations of this code but none of it worked.
$author_id = get_post_field('post_author', get_queried_object_id());
if (get_the_author_meta('display_name', $author_id) === 'vivian') {
// do something
}
You're passing$product
to your custom callback function, so you could use it to get the author info like so:
add_filter('woocommerce_loop_add_to_cart_link', 'tomich_add_melody_vendor', 10, 3);
function tomich_add_melody_vendor($add_to_cart_html, $product, $args)
{
$author_nickname = get_the_author_meta('nickname', $product->post->post_author);
// OR
// $author_id = get_the_author_meta('ID', $product->post->post_author);
// OR
// $author_name = get_the_author_meta('display_name', $product->post->post_author);
// You could also get the author with other paramaeters like:
// display_name
// first_name
// ID
// last_name
// nickname
if ($author_nickname == "vivian") { // pass the name you want
$before = 'my custom element before'; // Some text or HTML here
$after = 'my custom element after'; // Add some text or HTML here as well
return $before . $add_to_cart_html . $after;
} else {
return $add_to_cart_html;
}
}
After retrieving the post author with the Wordpress function you can use another Wordpress function
to get the user's nickname and other data.
add_filter( 'woocommerce_loop_add_to_cart_link', 'tomich_add_melody_vendor', 10, 3 );
function tomich_add_melody_vendor ( $add_to_cart_html, $product, $args ) {
// Gets the "WP_Post" object of the product
$post = get_post( $product->get_id() );
// Gets the user ID
$user_id = $post->post_author;
// Gets "WP_User" object
$user = get_userdata( $user_id );
// Gets the user's data
$user_login = $user->user_login;
$user_nicename = $user->user_nicename;
$user_email = $user->user_email;
$display_name = $user->display_name;
// run your code here
}
The code has been tested and works. Add it to your active theme's functions.php.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.