if语句分组产品参考woocommerce [英] if statement grouped products reference woocommerce

查看:39
本文介绍了if语句分组产品参考woocommerce的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的functions.php文件中,我有一些针对woocommerce运行的remove_actions和add_filters,但问题是这些函数适用于所有woocommerce产品类型.

我想在只为分组产品页面运行的挂钩/过滤器周围包装一个简单的if语句,问题是我不知道woocommerce在这里指的是这些页面.

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

这里是我想做的事,但我不知道$ grouped_product的正确参考.

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

我有两个add_filter和一个删除动作,我想在我的functions.php中添加它们以仅在分组的产品页面上执行,我只需要上面第二个代码块的第一组括号中的正确引用即可.

尝试过此代码,但不起作用.

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

功能php文件

 <?php函数theme_enqueue_styles(){$ parent_style ='父母式';wp_enqueue_style($ parent_style,get_template_directory_uri().'/style.css');wp_enqueue_style('child-style',get_stylesheet_directory_uri().'/style.css',数组($ parent_style));}add_action('wp_enqueue_scripts','theme_enqueue_styles');//从简短说明下删除购物车选项.remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);//下面的过滤器会添加新标签,并在标签内返回购物车选项.add_filter('woocommerce_product_tabs','woo_paym_product_tab');函数woo_paym_product_tab($ tabs){//添加新标签$ tabs ['paym-plans'] = array('title'=> __('Contract Deals','woocommerce'),'priority'=> 10,'callback'=>'woo_paym_product_tab_content');返回$ tabs;}函数woo_paym_product_tab_content(){//新标签页内容woocommerce_template_single_add_to_cart();}//下面的过滤器更改选项卡优先级以首先显示价格计划.add_filter('woocommerce_product_tabs','sb_woo_move_description_tab',98);函数sb_woo_move_description_tab($ tabs){$ tabs ['paym-plans'] ['priority'] = 10;$ tabs ['description'] ['priority'] = 20;$ tabs ['additional_information'] ['priority'] = 30;返回$ tabs;}?> 

解决方案

解决了问题的技巧是将其更改为过滤器.

  add_filter('woocommerce_single_product_summary','filter_grouped_cart');函数filter_grouped_cart(){全球$ 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);}}} 

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'] = array( 'title' => __( 'Contract Deals', 'woocommerce' ), 'priority' => 10, 'callback' => 'woo_paym_product_tab_content' );

    return $tabs;

}

function woo_paym_product_tab_content() {

    // The new tab content

        woocommerce_template_single_add_to_cart();

} 

//Filter below changes tab priority to display price plans first.

add_filter( 'woocommerce_product_tabs', 'sb_woo_move_description_tab', 98);

function sb_woo_move_description_tab($tabs) {



    $tabs['paym-plans']['priority'] = 10;

    $tabs['description']['priority'] = 20;

    $tabs['additional_information']['priority'] = 30;

    return $tabs;

}
?>

解决方案

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

这篇关于if语句分组产品参考woocommerce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆