【问题标题】:WooCommerce showing product variation data in tab disables other tab contentWooCommerce 在选项卡中显示产品变体数据会禁用其他选项卡内容
【发布时间】:2018-05-30 14:35:50
【问题描述】:

所以我试图在选项卡中显示 WooCommerce 变体的长度、宽度和高度。一切顺利,但它禁用了其他选项卡。本质上,它们返回时没有内容。

// Update descriptions tab
add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
function woo_custom_description_tab( $tabs ) {
$tabs['description']['callback'] = 'woo_custom_description_tab_content';    // Custom description callback
return $tabs;
}
function woo_custom_description_tab_content() {

global $product;

$available_variations = $product->get_available_variations();

$variation_id=$available_variations[0]['variation_id']; // Variation ID for first variation

$variable_product1 = new WC_Product_Variation( $variation_id );

echo '<strong>Product Box Dimensions</strong>';
if($variable_product1 ->get_length() != ''){
   echo '<div class="dimensions">Length: ' . $variable_product1 ->get_length() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
}

if($variable_product1 ->get_width() != ''){
   echo '<div class="dimensions">Width: ' . $variable_product1 ->get_width() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
}

if($variable_product1 ->get_height() != ''){
echo '<div class="dimensions">Height: ' . $variable_product1 ->get_height() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
}


}

【问题讨论】:

    标签: php wordpress object woocommerce


    【解决方案1】:

    您的代码中有一些错误。

    1. 在获取变体之前,请确认产品是 可变产品。
    2. 您的回调函数用于产品描述 标签。所以它会破坏描述选项卡中的默认内容。

    这是一个更新的代码。

    add_filter( 'woocommerce_product_tabs', 'woo_custom_description_tab', 98 );
    
    
    function woo_custom_description_tab( $tabs ) {
    
        $tabs['description']['callback'] = 'woo_custom_description_tab_content';    // Custom description callback
        return $tabs;
    
    }
    
    function woo_custom_description_tab_content() {
    
        global $product;
    
        if ( $product->is_type( 'variable' ) ) {  // Before fetching variables, confirm that it is a variable product.
    
            $available_variations = $product->get_available_variations();
    
            $variation_id=$available_variations[0]['variation_id']; // Variation ID for first variation
    
            $variable_product1 = new WC_Product_Variation( $variation_id );
    
            echo '<strong>Product Box Dimensions</strong>';
    
            if($variable_product1 ->get_length() != ''){
               echo '<div class="dimensions">Length: ' . $variable_product1 ->get_length() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
            }
    
            if($variable_product1 ->get_width() != ''){
               echo '<div class="dimensions">Width: ' . $variable_product1 ->get_width() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
            }
    
            if($variable_product1 ->get_height() != ''){
                echo '<div class="dimensions">Height: ' . $variable_product1 ->get_height() . ' ' . get_option( 'woocommerce_dimension_unit' ); 
            }
    
        }
    
    }
    

    要添加新的自定义选项卡,请使用下面的 sn-ps 并更新代码。

    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    
        // Adds the new tab
    
        $tabs['test_tab'] = array(
            'title'     => __( 'New Product Tab', 'woocommerce' ),
            'priority'  => 50,
            'callback'  => 'woo_new_product_tab_content'
        );
    
        return $tabs;
    
    }
    function woo_new_product_tab_content() {
    
        // The new tab content
    
        echo '<h2>New Product Tab</h2>';
        echo '<p>Here\'s your new product tab.</p>';
    
    }
    

    WooCommerce 文档页面中提供了对产品选项卡的更多调整。

    https://docs.woocommerce.com/document/editing-product-data-tabs/

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多