php - WooCommerce 如何显示某些作者的产品存档 IF 信息

标签 php wordpress woocommerce hook-woocommerce woocommerce-theming

我想在产品存档页面(商店列表)上的购物车前回显一些文本,但仅限于特定作者的产品。

我有这段代码,可以在我想要的地方显示我想要的信息:

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;
}

但现在我需要弄清楚如何限制它仅显示在特定作者的产品上,可以通过昵称ID .

我尝试了此代码的一些变体,但没有一个起作用。

$author_id = get_post_field('post_author', get_queried_object_id());

if (get_the_author_meta('display_name', $author_id) === 'vivian') {
    // do something
}

最佳答案

您将 $product 传递给自定义回调函数,因此您可以使用它来获取作者信息,如下所示:

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;
  }

}

关于php - WooCommerce 如何显示某些作者的产品存档 IF 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67605848/

相关文章:

php - 在 PHP 中上传一个非常大的文件时没有错误?

javascript - 上传前无法显示图片

php - Wordpress 子主题 style.css 已停止工作

php - WP - woocommerce 按订单列出产品类别

php - 将格式化的地址状态代码更改为 Woocommerce 订单中的状态名称

php - 禁用 Woocommerce 产品类别存档页面上的“添加到购物车”按钮

php - 不能在 cURL PHP 中使用 cookie

php - 在 Laravel 数据表中添加 HTML 属性

html - 用于 Wordpress 的 tinyMCE - <span> 标签而不是 <p>

php - 在 Woocommerce 中显示自定义结帐地址字段 2 标签