首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向WooCommerce管理产品数据添加一个自定义文本字段"Variations“

向WooCommerce管理产品数据添加一个自定义文本字段"Variations“
EN

WordPress Development用户
提问于 2019-05-02 12:40:15
回答 2查看 5.4K关注 0票数 2

在wooCommerce管理产品页面中,在可变产品上可以有产品变体。见下面的“变化”设置部分:

我想要实现的What:

我已经能够添加一个文本字段的常规价格和简单产品的销售价格管理“产品数据”metabox下的“一般”部分。现在,我需要在“变化”部分下为可变产品添加一个新的文本字段“成本价格”,但是我不知道如何添加这个字段。

因此,我开始搜索脚本/阅读WooCommerce文档,并通过编辑includes/admin/metabox/html-variation-admin.php来添加新的文本字段,并将下面的代码添加到其中:

代码语言:javascript
复制
woocommerce_wp_text_input(
    array(
        'id'            => "variable_cost_price{$loop}",
        'name'          => "variable_cost_price[{$loop}]",
        'value'         => wc_format_localized_price( $variation_object->get_cost_price( 'edit' ) ),
        'data_type'     => 'price',
        'label'         => $label ,
        'wrapper_class' => 'form-row form-row-last',
    )
);

我知道这是可行的,因为我从“常规价格”表单字段克隆了这个。

My问题:当然,如果文本字段不保存到数据库,并将数据带回页面加载时显示,那么文本字段就没有意义了。再说一次,我不是百分之百确定,但我认为我必须作出更多的补充。

<#>What我已经尝试过了:我已经将get_cost_price方法添加到class-wc-ajax.php脚本中,因为我已经看到了一个用于get_regular_price的脚本。我还在class-wc-meta-box-product-data.php中看到了引用regular_price的行,因此我为我的新cost_price添加了一个条目,请参见下面添加的内容(添加了cost_price行):

代码语言:javascript
复制
$errors = $variation->set_props(
    array(
        'status'            => isset( $_POST['variable_enabled'][ $i ] ) ? 'publish' : 'private',
        'menu_order'        => wc_clean( $_POST['variation_menu_order'][ $i ] ),
        'regular_price'     => wc_clean( $_POST['variable_regular_price'][ $i ] ),
        'sale_price'        => wc_clean( $_POST['variable_sale_price'][ $i ] ),
        'cost_price'        => wc_clean( $_POST['variable_code_price'][ $i ] ),
        // .. more code here..

我错过了什么,我需要修改另一个脚本吗?

同样,文本字段正在显示,但输入数据并单击“保存更改”实际上似乎并没有向postmeta表添加任何内容。

编辑:我不需要在前端网站上显示,这纯粹是为了存储后端数据

EN

回答 2

WordPress Development用户

回答已采纳

发布于 2019-05-03 00:36:53

由于许多原因,重写核心文件是绝对要避免的事情。

若要将自定义字段添加到“产品变体选项价格”中并保存“保存”中的值,请使用以下命令:

代码语言:javascript
复制
// Admin: Add custom field in product variations options pricing
add_action( 'woocommerce_variation_options_pricing', 'add_variation_custom_option_pricing', 10, 3 );
function add_variation_custom_option_pricing( $loop, $variation_data, $variation ){

   woocommerce_wp_text_input( array(
        'id'            => '_cost_price['.$loop.']',
        'label'         => __("Cost Price", "woocommerce") . ' (' . get_woocommerce_currency_symbol() . ')',
        'class' => 'short wc_input_price',
        'data_type'     => 'price',
        'wrapper_class' => 'form-row form-row-first',
        'value'         => wc_format_localized_price( get_post_meta( $variation->ID, '_cost_price', true ) )
    ) );
}

// Admin: Save custom field value from product variations options pricing
add_action( 'woocommerce_save_product_variation', 'save_variation_custom_option_pricing', 10, 2 );
function save_variation_custom_option_pricing( $variation_id, $i ){
    if( isset($_POST['_cost_price'][$i]) ){
        update_post_meta( $variation_id, '_cost_price', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_cost_price'][$i]) ) ) );
    }
}

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

使用-如果您需要使用它来获得值:

( 1)来自更改ID

代码语言:javascript
复制
$cost_price = update_post_meta( $variation_id, '_cost_price', true );

( 2)来自产品变体对象

代码语言:javascript
复制
$cost_price = $variation->get_meta('_cost_price');
票数 2
EN

WordPress Development用户

发布于 2019-05-02 14:40:45

您需要使用系统为您提供的WordPress挂钩和过滤器。

将此代码放入当前主题的functions.php中。确保您的主题已经通过“woocommerce_support (‘woocommerce’)”启用了;

代码语言:javascript
复制
 'cost-price',
            'class'         => 'short wc_input_price cost-price',
            'label'         => __('Cost-Price (€)', 'yourtextdomain'),
           'description'    => 'Cost-price description...',
           'placeholder'    => '50 Cent'
       ));
   }
}
add_action('woocommerce_product_options_pricing','se_add_extra_price_field');

// Enable to save the extra field
function se_save_extra_price_field($post_id) {
    $product = wc_get_product($post_id);
    $title = isset( $_POST['cost-price'] ) ? $_POST['cost-price'] : '';
    $product->update_meta_data( 'cost-price', sanitize_text_field( $title ) );
    $product->save();
    if(isset($_POST['cost-price'])) {
            if(is_numeric($_POST['cost-price'])){
                update_post_meta($product_id, 'cost-price', $_POST['cost-price']);
            }
        }
    }
add_action('save_post', 'se_save_extra_price_field');

我已经测试过这段代码,它应该可以工作--只需复制和粘贴(或多或少.)。

如果这个答案对你有用,请接受它的正确答案。谢谢。

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/336891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档