对于每个循环(WooCommerce阵列)隐藏自定义选项卡

时间:2016-02-21 11:46:00

标签: php wordpress woocommerce

大家好,我一直在努力隐藏我的woocommerce产品页面中的自定义标签,具体取决于天气,分组产品子产品包含某个属性。我的代码看起来不错,但问题是当我在我的functions.php文件中使用它时,我的产品页面上的所有选项卡都会消失。这是我的代码。

add_filter( 'woocommerce_product_tabs', 'woo_simfree_product_tab' );

function woo_simfree_product_tab( $tabs ) {

global $post;
if  (function_exists( 'get_product' )) {
    $product = get_product( $post->ID );

        if ($product->is_type( 'grouped' )) {
                $PAYG = false;

                foreach ($product->get_children() as $child_id) {
                    $child = get_product($child_id);
                    $attr = $child->get_attribute('contract-type');
                        if ($attr == 'PAYG') {
                            $PAYG = true;
                        }
                }

                if ($PAYG = true) {
                    $tabs['simfree-plans'] = array( 'title' => __( 'SIM Free', 'woocommerce' ), 'priority' => 20, 'callback' => 'woo_simfree_product_tab_content' );
                } else {
                    return $tabs;
                }

        } else {
            return $tabs;
        }

}
}

任何人都可以看到任何问题或指出我正确的方向,谢谢。

更新:现在产品选项卡正在显示,因为如果$ PAYG = true,我忘记返回$ tabs,但由于某种原因,该功能无效。该选项卡仍显示在不应显示的产品页面上。谁知道为什么?

add_filter( 'woocommerce_product_tabs', 'woo_simfree_product_tab' );

function woo_simfree_product_tab( $tabs ) {

global $post;
if  (function_exists( 'get_product' )) {
    $product = get_product( $post->ID );

        if ($product->is_type( 'grouped' )) {
                $PAYG = false;

                foreach ($product->get_children() as $child_id) {
                    $child = get_product($child_id);
                    $attr = $child->get_attribute('contract-type');
                        if ($attr == 'PAYG') {
                            $PAYG = true;
                        }
                }

                if ($PAYG = true) {
                    $tabs['simfree-plans'] = array( 'title' => __( 'SIM Free', 'woocommerce' ), 'priority' => 20, 'callback' => 'woo_simfree_product_tab_content' );
                    return $tabs;
                } else {
                    return $tabs;
                }

        } else {
            return $tabs;
        }

}
}

更新:如果没有任何产品,我现在已经能够将标签完全清空,如果空的话可以隐藏woocommerce自定义标签。

add_filter( 'woocommerce_product_tabs', 'woo_simfree_product_tab' );

function woo_simfree_product_tab( $tabs ) {

global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
        $tabs['simfree-plans'] = array( 'title' => __( 'SIM Free', 'woocommerce' ), 'priority' => 20, 'callback' => 'woo_simfree_product_tab_content' );
    return $tabs;
} else {
    return $tabs;
}
}

}  

0 个答案:

没有答案