【问题标题】:if statement grouped products reference woocommerceif 语句分组产品参考 woocommerce
【发布时间】:2016-02-08 23:00:27
【问题描述】:

在我的 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() 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;

}
?>

【问题讨论】:

标签: php filter woocommerce hook


【解决方案1】:

解决问题的诀窍是将其更改为过滤器。

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

【讨论】:

    【解决方案2】:

    检查 $product->is_type('grouped') 在这里没有帮助...

    请检查代码是否对您有任何帮助..

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

    【讨论】:

    • 您好,感谢您抽出宝贵时间帮助我:) 所以我将您的代码粘贴到 wordpress 中的 functions.php 文件中,并在分组产品页面上搜索字符串“是组的一部分”但即使在页面源中也找不到它。我不明白为什么 woocommerce 让这变得如此困难,有什么想法吗?
    • 插件作者告诉我“你需要查看全局 $post 对象并从那里检查产品类型,仅在 is_singular('product') 页面上。”
    • 找到了这个wordpress.org/support/topic/…,但是当我将它复制并粘贴到我的functions.php网站并添加删除操作时,它只会破坏我的网站?
    • bryceadams.com/change-add-cart-text-woocommerce 这种事情可能有用吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-18
    • 2018-11-30
    • 2013-07-05
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多