Monday, May 9, 2022

[FIXED] How to check if a product is not a bundled product in WooCommerce

Issue

I want to use this snippet to replace the titles of products. That works.

  /** Remove the shop loop title link and replace with brand **/ 
  remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
  add_action('woocommerce_shop_loop_item_title', 'abChangeProductsTitle', 10 );

  function abChangeProductsTitle() { ?>
      <p class="name product-title custom-brand" style="height: 23px;">
      <?php echo strip_tags ( get_the_term_list( $post->ID, 'product_brand', '', ', ', ''  ) ); ?> 
  </p><?php 
  }

But I am trying to figure out how to only run this snippet (replace titles on all other products) if the product type is not a bundled product (from https://woocommerce.com/products/product-bundles/ ).

Can someone help me along a little?


Solution

Updated

Try to use something like (to restrict your code to other products than "bundle" product type):

add_action('woocommerce_shop_loop_item_title', 'change_product_title_except_bundle_products', 1 );
function change_product_title_except_bundle_products(){
    global $product;

    // Except for bundle products type
    if( ! $product->is_type('bundle') ) {
        remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
        add_action('woocommerce_shop_loop_item_title', 'customize_product_title', 10 );
    }
}

function customize_product_title() {
    $brands = get_the_term_list( get_the_id(), 'product_brand', '', ', ', ''  );
    ?>
    <p class="name product-title custom-brand" style="height: 23px;"><?php echo strip_tags ( $brands ); ?> </p><?php
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.



Answered By - LoicTheAztec
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.