Brands plugin switchover issue with custom code in WooCommerce

As part of my WooCommerce store, we were using a plugin to facilitate product brands. We originally had a third-party one called YITH Brands. It worked for a while, but it doesn’t integrate well with others, so we switched over to the official WC Brands.

The problem is, our previous web developers coded some functionality from YITH into our website, and so disabling this plugin breaks everything.

It appears to be the code that displays the brand name above the product title, as below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' );
function aq_display_brand_before_title(){
global $product;
$product_id = $product->get_id();
$brands = wp_get_post_terms( $product_id, 'yith_product_brand' );
foreach( $brands as $brand ) echo '<a href="'.get_term_link($brand->term_id).'">'.$brand->name.'</a>';
</code>
<code> add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' ); function aq_display_brand_before_title(){ global $product; $product_id = $product->get_id(); $brands = wp_get_post_terms( $product_id, 'yith_product_brand' ); foreach( $brands as $brand ) echo '<a href="'.get_term_link($brand->term_id).'">'.$brand->name.'</a>'; </code>
    add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' );
    function aq_display_brand_before_title(){
    global $product;
    $product_id = $product->get_id();
    $brands = wp_get_post_terms( $product_id, 'yith_product_brand' );
    foreach( $brands as $brand ) echo '<a href="'.get_term_link($brand->term_id).'">'.$brand->name.'</a>';

I’ve found some similar code in the documentation for WC Brands, but using it doesn’t display on the website.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 1 );
function wc_brands_add_brand_name() {
global $product;
$brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
echo "<p>Brand: " . $brands . "</p>";
</code>
<code> add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 1 ); function wc_brands_add_brand_name() { global $product; $brands = implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names'])); echo "<p>Brand: " . $brands . "</p>"; </code>
    add_action( 'woocommerce_single_product_summary', 'wc_brands_add_brand_name', 1 );
    function wc_brands_add_brand_name() {
    global $product;
    $brands =  implode(', ', wp_get_post_terms($product->get_id(), 'product_brand', ['fields' => 'names']));
    echo "<p>Brand: " . $brands . "</p>";

Is there a way I can alter the first piece of code so that it pulls the variables and such from WC Brands? That way, we keep the functionality and I can safely disable YITH.

1

For official WooCommerce Brands plugin, the taxonomy is “product_brand” instead of “yith_product_brand”. With your previous developer code that you provided, there are some missing things to avoid this kind of issue.

Also, it is better to use get_the_terms() as results are cached, avoiding a database query each time the page is displayed.

Replace the code with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' );
function aq_display_brand_before_title(){
global $product;
$taxonomy = 'product_brand'; // <= Here we set the correct taxonomy
$brand_terms = get_the_terms( $product->get_id(), $taxonomy );
// Check if it's the correct taxonomy and if there are terms assigned to the product
if ( $brand_terms && ! is_wp_error( $brand_terms ) ) {
// Loop through assigned terms
foreach( $brand_terms as $term ) {
if ( is_a($term, 'WP_Term') ) {
echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
}
}
}
}
</code>
<code>add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' ); function aq_display_brand_before_title(){ global $product; $taxonomy = 'product_brand'; // <= Here we set the correct taxonomy $brand_terms = get_the_terms( $product->get_id(), $taxonomy ); // Check if it's the correct taxonomy and if there are terms assigned to the product if ( $brand_terms && ! is_wp_error( $brand_terms ) ) { // Loop through assigned terms foreach( $brand_terms as $term ) { if ( is_a($term, 'WP_Term') ) { echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>'; } } } } </code>
add_action( 'woocommerce_before_shop_loop_item_title', 'aq_display_brand_before_title' );
function aq_display_brand_before_title(){
    global $product;

    $taxonomy    = 'product_brand'; // <= Here we set the correct taxonomy
    $brand_terms = get_the_terms( $product->get_id(), $taxonomy );

    // Check if it's the correct taxonomy and if there are terms assigned to the product 
    if ( $brand_terms && ! is_wp_error( $brand_terms ) ) {
        // Loop through assigned terms
        foreach( $brand_terms as $term ) {
            if ( is_a($term, 'WP_Term') ) {
                echo '<a href="' . get_term_link( $term ) . '">' . $term->name . '</a>';
            }
        }
    }
}

It should work without issue, in a lighter way.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật