doumei8126 2018-03-13 12:17
浏览 156
已采纳

Woocommerce中产品的其他自定义尺寸

I have a products with the following size parameters:

  • Length (Woocomerce standard)
  • Width (Woocomerce standard)
  • Height (Woocomerce standard)
  • Diameter
  • Thickness
  • Circuit

In the product edit page I have only Length, Width, Height (Woocomerce standard)

I would like to add my other sizes parameters

How can I add this extra sizes properly?

Is there any filter for this?

  • 写回答

2条回答 默认 最新

  • douhao1956 2018-03-13 14:03
    关注

    There is multiple ways…

    1) Using Product attributes: (without any coding need):

    • You should create 3 new product attributes (one for each other missing dimension).

      enter image description here
    • You should set each of them in each product with the correct options (displayed on products option) and values.

      enter image description here

    Advantage: Those other dimention attributes will be displayed on product pages.

    Disadvantages:

    • Each attribute Needs to be set for each product and each value is going to be a term of this product attribute.
    • There are no specific existing WC_Product methods to get those custom product attributes like the default dimension ones. So is going to be more complicated to get them outside the product page.

    2) Using custom setting fields:

    // Add custom fields to product shipping tab
    add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
    function add_product_options_other_dimensions(){
        global $product_object;
    
        $product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
    
        echo '</div><div class="options_group">'; // New option group
    
        woocommerce_wp_text_input( array(
            'id'          => '_diameter',
            'label'       => __( 'Diameter', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Diameter description help.', 'woocommerce' )
        ) );
    
        woocommerce_wp_text_input( array(
            'id'          => '_thickness',
            'label'       => __( 'Thickness', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Thickness description help.', 'woocommerce' )
        ) );
    
        woocommerce_wp_text_input( array(
            'id'          => '_circuit',
            'label'       => __( 'Circuit', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Circuit description help.', 'woocommerce' )
        ) );
    
    }
    
    // Save the custom fields values as meta data
    add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
    function save_product_options_other_dimensions( $post_id ){
    
        if( isset( $_POST['_diameter'] ) )
            update_post_meta( $post_id, '_diameter', esc_attr( $_POST['_diameter'] ) );
    
        if( isset( $_POST['_thickness'] ) )
            update_post_meta( $post_id, '_thickness', esc_attr( $_POST['_thickness'] ) );
    
        if( isset( $_POST['_circuit'] ) )
            update_post_meta( $post_id, '_circuit', esc_attr( $_POST['_circuit'] ) );
    
    }
    
    // Add custom fields to product variation settings
    add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
    function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
    
        $variation_diameter = get_post_meta($variation->ID,"_diameter", true );
        if( ! $variation_diameter ) $variation_diameter = "";
    
        $variation_thickness = get_post_meta($variation->ID,"_thickness", true );
        if( ! $variation_thickness ) $variation_thickness = "";
    
        $variation_circuit = get_post_meta($variation->ID,"_circuit", true );
        if( ! $variation_circuit ) $variation_circuit = "";
    
        echo '<p class="form-field dimensions_field">';
    
        woocommerce_wp_text_input( array(
            'id'          => '_diameter' . '_' . $loop,
            'label'       => __( 'Diameter', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Diameter description help.', 'woocommerce' ),
            'value'       => $variation_diameter
        ) );
    
        woocommerce_wp_text_input( array(
            'id'          => '_thickness' . '_' . $loop,
            'label'       => __( 'Thickness', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Thickness description help.', 'woocommerce' ),
            'value'       => $variation_thickness
        ) );
    
        woocommerce_wp_text_input( array(
            'id'          => '_circuit' . '_' . $loop,
            'label'       => __( 'Circuit', 'woocommerce' ),
            'desc_tip'    => 'true',
            'description' => __( 'Circuit description help.', 'woocommerce' ),
            'value'       => $variation_circuit
        ) );
    
        echo '</p>';
    }
    
    
    // Save product variation custom fields values
    add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
    function save_variation_options_other_dimensions( $variation_id, $loop ){
    
        $built_lenght = $_POST["_diameter_$loop"];
        if( isset($built_lenght) )
            update_post_meta( $variation_id, '_built_lenght', esc_attr($built_lenght) );
    
        $built_width = $_POST["_thickness_$loop"];
        if( isset($built_width) )
            update_post_meta( $variation_id, '_built_width', esc_attr($built_width) );
    
        $built_height = $_POST["_circuit_$loop"];
        if( isset($built_height) )
            update_post_meta( $variation_id, '_built_height', esc_attr($built_height) );
    }
    

    This code goes on function.php file of your active child theme (or theme).

    You will get that for products:

    enter image description here

    And that for product variations (in variable products "variations settings):

    enter image description here

    Now to display and get those values, you will have to override the woocommerce template via your theme as explained in this documentation.

    The file to copy and override via your theme is: single-product/product-attributes.php template.

    You will have to add some code to it to display your custom additional dimension labels and values just after the displayed default dimentions.

    To output the correct values you will use get_post_meta() function.

    For example to display the diameter value you will use:

    <?php echo get_post_meta( $product->get_id(), '_diameter', true ); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失