【问题标题】:How to get the brand name of product in WooCommerce如何在 WooCommerce 中获取产品的品牌名称
【发布时间】:2019-10-10 02:44:05
【问题描述】:

我需要得到产品的品牌名称,我有这个代码

$product = wc_get_product();

  $type = $product->get_type();      
  $name = (string)$product->get_name();
  $id = (int)$product->get_id(); 
  $sku  = (int)$product->get_sku(); 
  $precio = (int)$product->get_price();

$brand_name = $product->get_brand(); ---> ???

我得到了这个属性,但我不知道如何捕捉品牌名称,还有其他方法吗?

谢谢!

【问题讨论】:

  • 您似乎正在使用该品牌的插件,您能说出那个插件的名字吗?我认为品牌将保留为该特定分类的条款...

标签: php wordpress woocommerce custom-taxonomy taxonomy-terms


【解决方案1】:

最好使用产品 ID 中的wc_get_post_terms()(允许获取术语名称而不是 WP_Term 对象),并且根据您使用的插件,分类会有所不同:

  • product_brand 用于 Woocommerce 品牌插件
  • yith_product_brand 用于 YITH WooCommerce 品牌插件
  • pa_brand 用于自定义产品属性

例如,您将使用 Woocommerce Brands 插件:

$product_id  = get_the_id();
$product     = wc_get_product( $product_id );

$taxonomy    = `product_brand`;
$brand_names = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'names' ) );

// Get the brand name
$brand_name = reset( $brand_names );

相关:

【讨论】:

    【解决方案2】:

    使用get_the_terms

    get_the_terms($product->get_id(),'pa_brand') 
    

    【讨论】:

      【解决方案3】:

      感谢您的帮助,我使用了此代码并且它有效。

      $terms = get_the_terms( get_the_ID(), 'product_brand' );
      
      foreach ( $terms as $term ){
          if ( $term->parent == 0 ) {
              $brand_name=  $term->slug;
          }
      }  
      echo $brand_name;
      

      【讨论】:

      • 是的,这取决于@LoicTheAztec 解释的插件
      【解决方案4】:

      如果您使用 Woocommerce 品牌插件:https://docs.woocommerce.com/document/woocommerce-brands/

      你可以使用下一个函数:

      echo get_brands();
      

      插件包含一个获取品牌的函数,下一行在下一个插件文件和目录 woocommerce-brands/includes/wc-brands-functions/line-64 到 81 中声明

      function get_brands( $post_id = 0, $sep = ', ', $before = '', $after = '' ) {
          global $post;
      
          if ( ! $post_id ) {
              $post_id = $post->ID;
          }
      
          return get_the_term_list( $post_id, 'product_brand', $before, $sep, $after );
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-25
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        • 2018-03-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多