【问题标题】:Custom fields in WooCommerce product variationsWooCommerce 产品变体中的自定义字段
【发布时间】:2016-04-24 23:26:12
【问题描述】:

问题已更新,因为人们似乎误解了它:

使用 WordPress 的 WooCommerce 插件,我想在“附加信息”选项卡中显示产品变体名称,其方式与显示重量和尺寸的方式相同。

例如,我有一个产品有两种尺寸,2 升和 10 升,因此它是具有“2 升”和“10 升”两种产品变体的可变产品。如果我选中“在产品页面上显示”框,尺寸会显示在“附加信息”选项卡中,如下所示:“尺寸:2 升,10 升”。 I want it to work like the weight and dimensions, so when the product variation '2 litres' is selected, the Additional Information tab will display 'Size: 2 litres', and when the product variation '10 litres' is selected, the Additional信息选项卡将显示“尺寸:10 升”。

这个可以吗?

【问题讨论】:

  • 我认为您不需要自定义字段。听起来您需要为“大小”创建另一个属性并基于该新属性创建其他变体。
  • 我做到了。但附加信息选项卡显示“尺寸:2 升,10 升”,无论选择何种变体。 I need it to say 'Size: 2 litres' when the 2 litres variation is selected, and 'Size: 10 litres' when the 10 litres variation is selected.那是我的问题。
  • 这与您上面描述的问题不同。你能编辑你的问题吗?
  • 我相信这完全是同一个问题,但可以肯定的是,我会更新问题以使其更具体。
  • 问题已更新。

标签: wordpress woocommerce custom-fields


【解决方案1】:

这是为产品变体添加所有类型的自定义输入字段的完整代码:

<?php
// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes','variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
/**
 * Create new fields for variations
 *
*/
function variation_settings_fields( $loop, $variation_data, $variation ) {
    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field[' . $variation->ID . ']', 
            'label'       => __( 'My Text Field', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );
    // Number Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_number_field[' . $variation->ID . ']', 
            'label'       => __( 'My Number Field', 'woocommerce' ), 
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom number here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_number_field', true ),
            'custom_attributes' => array(
                            'step'  => 'any',
                            'min'   => '0'
                        ) 
        )
    );
    // Textarea
    woocommerce_wp_textarea_input( 
        array( 
            'id'          => '_textarea[' . $variation->ID . ']', 
            'label'       => __( 'My Textarea', 'woocommerce' ), 
            'placeholder' => '', 
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_textarea', true ),
        )
    );
    // Select
    woocommerce_wp_select( 
    array( 
        'id'          => '_select[' . $variation->ID . ']', 
        'label'       => __( 'My Select Field', 'woocommerce' ), 
        'description' => __( 'Choose a value.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, '_select', true ),
        'options' => array(
            'one'   => __( 'Option 1', 'woocommerce' ),
            'two'   => __( 'Option 2', 'woocommerce' ),
            'three' => __( 'Option 3', 'woocommerce' )
            )
        )
    );
    // Checkbox
    woocommerce_wp_checkbox( 
    array( 
        'id'            => '_checkbox[' . $variation->ID . ']', 
        'label'         => __('My Checkbox Field', 'woocommerce' ), 
        'description'   => __( 'Check me!', 'woocommerce' ),
        'value'         => get_post_meta( $variation->ID, '_checkbox', true ), 
        )
    );
    // Hidden field
    woocommerce_wp_hidden_input(
    array( 
        'id'    => '_hidden_field[' . $variation->ID . ']', 
        'value' => 'hidden_value'
        )
    );
}
/**
 * Save new fields for variations
 *
*/
function save_variation_settings_fields( $post_id ) {
    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }

    // Number Field
    $number_field = $_POST['_number_field'][ $post_id ];
    if( ! empty( $number_field ) ) {
        update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
    }
    // Textarea
    $textarea = $_POST['_textarea'][ $post_id ];
    if( ! empty( $textarea ) ) {
        update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
    }

    // Select
    $select = $_POST['_select'][ $post_id ];
    if( ! empty( $select ) ) {
        update_post_meta( $post_id, '_select', esc_attr( $select ) );
    }

    // Checkbox
    $checkbox = isset( $_POST['_checkbox'][ $post_id ] ) ? 'yes' : 'no';
    update_post_meta( $post_id, '_checkbox', $checkbox );

    // Hidden field
    $hidden = $_POST['_hidden_field'][ $post_id ];
    if( ! empty( $hidden ) ) {
        update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
    }
}
?>

要在前端获取这些值,我们只需要使用流行的 get_post_meta() 函数即可。

参考文章在这里:

http://www.remicorson.com/woocommerce-custom-fields-for-variations/

【讨论】:

  • 这只是解释如何添加可变产品。它没有说明向产品变体添加自定义字段并在“附加信息”选项卡中显示它们。
  • @Jeppe Bech,对不起,我误解了你的问题。我只是根据您的需要修改了答案。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 如果我将该代码添加到我的主题的functions.php 网站将变得不可用。代码有问题还是我做错了什么?
  • 问题已更新,因为它似乎不够清楚我想要完成什么。
【解决方案2】:

这是第一次尝试。属性表中没有太多可做的事情,但是当属性下拉列表发生更改时,它会在附加信息选项卡中找到该属性(在默认 WooCommerce 中列出)并更改文本以匹配下拉列表中的标签.当客户切换回空的“选择”选项时会出现问题,因此您需要扩展它以某种方式解决这个问题。

jQuery( document ).ready(function($) {

    $( ".variations_form" ).on( 'change', '.variations select', function() {
        id = $(this).attr('id');
        attribute = toTitleCase( $(this).attr('id').replace("pa_", "") );
        selected = $(this).find('option:selected').text();
        $('#tab-additional_information').find('.shop_attributes th:contains('+attribute+')').next('td').text(selected);
    });

    function toTitleCase(str){
        return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
    }
});

将以上内容另存为somescript.js 在您的主题文件夹中。然后将以下内容添加到functions.php 以正确地将脚本排入队列。

/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/somescript.js', array('jquery'), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

【讨论】:

  • 那段代码去哪儿了?我尝试将它放在主题的 functions.php 的末尾,但这会导致网站变为空白(服务器错误 500)。
  • 这是一个javascript,所以不,它不属于functions.php。您需要将其保存为something.js 文件并使用functions.php 中的wp_enqueue_script() 来加载它。
  • 好的,试过了,还是不行。不知何故,它搞砸了产品图像的大小。
  • 产品图像尺寸现在看起来还可以,所以我想这与该脚本无关,我想是其他一些随机缺陷。脚本仍然无法正常工作。
猜你喜欢
  • 2015-07-12
  • 1970-01-01
  • 2021-04-12
  • 1970-01-01
  • 1970-01-01
  • 2021-04-16
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
相关资源
最近更新 更多