Php if语句分组产品引用
在我的functions.php文件中,我有一些针对woocommerce运行的remove_操作和add_过滤器,但问题是这些函数适用于所有woocommerce产品类型 我想把一个简单的if语句包装在钩子/过滤器周围,我只需要为分组的产品页面运行。问题是我不知道woocommerce是如何引用这些页面的,这是我目前所拥有的Php if语句分组产品引用,php,filter,woocommerce,hook,Php,Filter,Woocommerce,Hook,在我的functions.php文件中,我有一些针对woocommerce运行的remove_操作和add_过滤器,但问题是这些函数适用于所有woocommerce产品类型 我想把一个简单的if语句包装在钩子/过滤器周围,我只需要为分组的产品页面运行。问题是我不知道woocommerce是如何引用这些页面的,这是我目前所拥有的 remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_ad
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
以下是我想做的,但我不知道$U产品的正确参考
if ($grouped_product) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
我想在functions.php中添加两个add_filter和一个remove操作,以便只在分组的产品页面上执行,我只需要在上面第二个代码块的第一组括号中正确引用
尝试了此代码,但无效
if (is_product() and $product->is_type('grouped')) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
函数php文件
<?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;
}
?>
检查$product->is_type('grouped')在这里没有帮助
请检查代码是否对您有帮助
global $post;
if( $post->post_parent != 0 ){
echo 'is part of a group';
}
解决问题的诀窍是把它改成过滤器
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 );
}
}
}
您好,感谢您抽出时间帮助我:)因此我将您的代码粘贴到wordpress中的functions.php文件中,并在分组产品页面上搜索字符串“是组的一部分”,但即使在页面源代码中也找不到。我不明白为什么woocommerce会让这变得如此困难,有什么想法吗?插件作者告诉我“你需要查看全局$post对象并从那里检查产品类型,仅在is_singular('product')页面上。”发现了这个问题,但当我复制并粘贴到functions.php网站并添加删除操作时,它会破坏我的网站吗?也许这类事情行得通?这家伙做了一些类似的事情,但添加到购物车文本我怎么能复制他的技术,但删除行动只为分组产品?