当前位置: 首页 > 知识库问答 >
问题:

在选定的WooCommerce产品变化价格后添加自定义域值

仉姚石
2023-03-14
<?php

function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {

    echo '<div class="options_group form-row form-row-full">';

    $value = get_post_meta( $post->ID, '_variable_select_field', true );
    if( empty( $value ) ) $value = '';
    // Select
    woocommerce_wp_select(
        array(
            'id'      => '_variable_select_field[' . $variation->ID . ']',
            'label'   => __( 'Mengeneinheit', 'woocommerce' ),
            'value' => get_post_meta( $variation->ID, '_variable_select_field', true ),
            'options' => array(
                ''          => __( 'ohne', 'woocommerce' ),
                'Stück'     => __( 'Stück', 'woocommerce' ),
                'Paar'      => __( 'Paar', 'woocommerce' ),
                'Karton'   => __( 'Karton', 'woocommerce' ),
                'Packung'   => __( 'Packung', 'woocommerce' ), 
                'Set'   => __( 'Set', 'woocommerce' ),              
                'lfm'       => __( 'Laufmeter', 'woocommerce' ),
                'm²'        => __( 'm2', 'woocommerce' ),
                )
        )
    );
    echo '</div>';
}

// Variations tab
add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 );


// Add custom field for variations
function load_variation_settings_fields( $variations ) {
$variations['_variable_select_field'] = get_post_meta( $variations[ 'variation_id' ], '_variable_select_field', true );
  return $variations;
}

add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );

//Save variable product field
function mytheme_woo_add_custom_variation_fields_save( $post_id ){

    $woocommerce_select_field = $_POST['_variable_select_field'][ $post_id ];
    update_post_meta( $post_id, '_variable_select_field', esc_attr( $woocommerce_select_field ) );
}
add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );


/* Display the custom field after the price */
function mytheme_display_woo_custom_fields( $price ){
    global $post, $product;

    if ( $product->is_type( 'variable' ) ){
        $variation_price_unit = get_post_meta( $post->ID, '_variable_select_field', true );
        $price .= $variation_price_unit;
    }
        return $price;  
}

add_action( 'woocommerce_get_price_html', 'mytheme_display_woo_custom_fields', 15 );



谢谢!

共有1个答案

方波娃
2023-03-14

这个简单的代码片段将在选择的变化价格之后显示相关的价格单位,如下所示:

add_filter( 'woocommerce_available_variation', 'display_price_unit_after_variations_price_html', 10, 3) ;
function display_price_unit_after_variations_price_html( $data, $product, $variation ) {
    if ( $mengeneinheit = $variation->get_meta('_variable_select_field') )
        $data['price_html'] .= ' ' . $mengeneinheit;

    return $data;
}

代码放在活动子主题(或活动主题)的functions.php文件中。测试和工作。

您应该在任何地方用_variable_select_field重命名键_mengeneinheit,因为它会更加明确(或者用英语_price_unit)。

 类似资料:
  • 我正在使用名为高级定制字段(ACF)和WooCommerce的插件。我想为WooCommerce产品变体创建一个文本和图像字段。我在ACF中创建了字段,并将它们分配给。 但是在下没有看到这些字段。我搜索了,看起来我必须写一个自定义代码来适应这些字段。我试着搜索这个问题,我创建的大多数建议和教程都是关于通过代码创建自定义字段,而不是使用ACF,这对我没有帮助,因为字段必须使用ACF,这是因为我使用V

  • 我试图显示从x-x的变化自定义价格跨度。 null

  • 我已经添加了一个自定义字段,其中有图片帧的额外价格,现在如果图片价格是10$和用户选择一个帧,它将加起来5$让说,总数将是15$。 现在,如果我添加另一个产品,它的选择定制框架价格应该得到添加。e、 g产品1的价格是:10美元,上面选择的框架是:frame1谁的价格是5美元,那么该产品的总价格将是15美元,如果产品2加上价格10美元,选择frame2的价格是6美元,那么该产品的总价格将是16美元,

  • 我试图在变体下拉列表中显示产品变体价格。我试图改变默认的行为,当你在下拉列表中选择一个变量时,价格显示在div中。 问题是我找不到那个部门的变动价格。我搜索了所有的javascript,但找不到它 如果我使用: 我只得到所有选项的最小变化价格。我想知道每种变体的价格。有线索吗?

  • WooCommerce 如何增加自定义产品选项?产品编辑页面( Product Metabox )是WordPress Metabox。本文先介绍一个增加自定义产品字段的代码示例,再给出各个位置的钩子函数,最后介绍如何从前台获取自定义产品选项。 自定义产品选项的代码 目标是在上图所示的常规选项卡下添加一个自定义字段,代码如下,放在子主题的functions.php里。 /** * 增加自定义字段

  • 我正在使用这个我在网上找到的脚本,在Woocommerce/WordPress中删除价格范围,只显示可变产品的最低价格,语法如下:From us$xxxx 当变量产品只有一个变体时,我希望脚本做同样的事情。有一个自动的cron(bash+SQL脚本)可以删除不可用的产品。有时,它只留下一个变化的产品,并列出一个价格说明“从XXX美元”的变化看起来是不恰当的,因为只有一个单一的变化)。 如何添加一个