在Woocommerce中处理产品级别的产品尺寸单位

时间:2018-07-14 10:56:08

标签: php wordpress drop-down-menu woocommerce jquery-plugins

我想在wordpress WooCommerce中显示产品高度,宽度和长度的英寸和英尺

您可以在下面看到其标题的产品:enter image description here

网站链接:https://asiatic.endroid1.com/product/modern

我在后端wordpress仪表板woocommerce>设置>产品中有下拉选项。但是在编码中没有,现在我需要在前端使用此选项,例如,当客户购买产品时。他从下拉菜单中选择英尺或英寸

1 个答案:

答案 0 :(得分:3)

在下面的代码中,您将找到从产品自定义字段(选择字段)后端设置动态获取尺寸单位的完整方法:

// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_dimensions_unit_option');
function add_product_dimensions_unit_option(){
    woocommerce_wp_select( array(
        'id'          => '_dimensions_unit',
        'label'       => __( 'Dimensions unit', 'woocommerce' ),
        'options'       => array(
            'in' => __( 'Inch', 'woocommerce' ),
            'ft' => __( 'Foot', 'woocommerce' ),
        ),
    ) );
}

// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_dimensions_unit_option' );
function save_product_dimensions_unit_option( $post_id ){
    if( isset( $_POST['_dimensions_unit'] ) )
        update_post_meta( $post_id, '_dimensions_unit', esc_attr( $_POST['_dimensions_unit'] ) );

}

// Dynamic dimension unit from a product custom field
add_filter( 'woocommerce_format_dimensions', 'custom_dimention_unit', 20, 2 );
function custom_dimention_unit( $dimension_string, $dimensions ){
    // Get the dimension unit from product custom field value
    $dimension_unit = get_post_meta( get_the_id(), '_dimensions_unit', true );

    // HERE set the Default dimention unit  <=========  <=========  <=========
    if( empty($dimension_unit) ) $dimension_unit = 'in';

    $dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        $dimension_string .= ' ' . $dimension_unit;
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }

    return $dimension_string;
}

此代码位于您的活动子主题(或活动主题)的function.php文件中。经过测试并可以正常工作。

在后端产品编辑页面设置中:

enter image description here

在前端:

enter image description here


代码基于以下两个相关答案:


产品变体的附加代码,在后端:

// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_dimensions_unit_option', 10, 3 );
function add_variation_dimensions_unit_option( $loop, $variation_data, $variation ){

    $dimensions_unit = get_post_meta($variation->ID,"_dimensions_unit", true );
    if( ! $dimensions_unit ) $dimensions_unit = "";

    echo '<p class="form-field dimensions_field">';
    woocommerce_wp_select( array(
        'id'          => '_dimensions_unit' . '_' . $loop,
        'label'       => __( 'Dimensions unit', 'woocommerce' ),
        'options'       => array(
            'in' => __( 'Inches', 'woocommerce' ),
            'ft' => __( 'Feet', 'woocommerce' ),
        ),
        'value'       => $variation_diameter
    ) );
    echo '</p>';
}

// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_dimensions_unit_option', 10 ,2 );
function save_variation_dimensions_unit_option( $variation_id, $loop ){
    $built_lenght = $_POST["_dimensions_unit_$loop"];
    if( isset($built_lenght) )
        update_post_meta( $variation_id, '_dimensions_unit', esc_attr($built_lenght) );
}

此代码位于您的活动子主题(或活动主题)的function.php文件中。经过测试并可以正常工作。

enter image description here