Scott February 2016

if statement grouped products reference woocommerce

In my functions.php file I have some remove_actions and add_filters that run for woocommerce but the problem is these functions run for all woocommerce product types.

I'd like to wrap a simple if statement around the hooks/filters I have to only run for grouped product pages the problem is I dont know how woocommerce refers to these pages heres what I have at the moment.

 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

Heres what i'd like to do but I dont know the correct reference for $grouped_product.

 if ($grouped_product) {
 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
 }

I have two add_filter and one remove action i'd like to append in my functions.php to only execute on grouped product pages I just need the correct reference in the first set of brackets in the second code block above.

Tried this code but it doesn't work..

if (is_product() and $product->is_type('grouped')) {
 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}

functions php file

 <?php
function theme_enqueue_styles() {

$parent_style = 'parent-style';

wp_enqueue_style( $parent_style, get_template_directory_uri() .       '/style.css' );
wp_enqueue_style( 'child-style',
    get_stylesheet_directory_uri() . '/style.css',
    array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

//Remove cart options from under short description.

    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );

//Filter below adds new tab and returns cart options within tab.

add_filter( 'woocommerce_product_tabs', 'woo_paym_product_tab' );

function woo_paym_product_tab( $tabs ) {

    // Adds the new tab

        $tabs['paym-plans'] = a        

Answers


PHPExpert February 2016

Checking $product->is_type('grouped') will not help here...

Please check if the code provides any help to you..

global $post; 
if( $post->post_parent != 0 ){ 
    echo 'is part of a group'; 
}


Scott February 2016

Solved the problem trick was to change it to a filter.

add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart');  

function filter_grouped_cart(){
   global $post;
   if( function_exists('get_product') ){
   $product = get_product( $post->ID );
   if( $product->is_type( 'grouped' ) ){
      remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
}
}

Post Status

Asked in February 2016
Viewed 2,268 times
Voted 8
Answered 2 times

Search




Leave an answer