【发布时间】:2021-10-19 15:34:43
【问题描述】:
基于对以下问题的回答:
- Add custom field to product settings shipping tab and display value on additional information tab in WooCommerce
- Add custom fields to WooComerce product setting pages in the shipping tab
我设法在 WooCommerce 运输选项中创建了两个额外的字段:
- 添加装载米的字段
- 添加运输重量的字段
这些字段将通过管理员用户编辑字段获得值,或者通过帮助我们用值预填充字段的计算来获得值。
下面的 sn-p 显示了通过将 sn-ps 添加到 functions.php 文件中来创建字段。
add_action('woocommerce_product_options_shipping', function() {
woocommerce_wp_text_input(
array(
'id' => '_loading_meters',
'label' => __('Loading meters', 'woocommerce'),
'desc_tip' => 'true',
'description' => __( 'Vul hier de laadmeters in', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_loading_weight',
'label' => __('Loading weight', 'woocommerce'),
'desc_tip' => 'true',
'description' => __( 'Vul hier het laadgewicht in', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
});
第二个 sn-p 保存管理员用户手动添加的值。这个 sn-p 也被添加到functions.php 文件中。
add_action('woocommerce_process_product_meta', function($post_id) {
$product = wc_get_product($post_id);
$num_loading_meters = isset($_POST['_loading_meters']) ? $_POST['_loading_meters'] : '';
$num_loading_weight = isset($_POST['_loading_weight']) ? $_POST['_loading_weight'] : '';
$product->update_meta_data('_loading_meters', sanitize_text_field($num_loading_meters));
$product->update_meta_data('_loading_weight', sanitize_text_field($num_loading_weight)); $product->save();
});
作为最终结果,我希望实现使用计算自动填写这两个字段,但仍然可以由管理员用户编辑。
装载米的计算:
- (长(米))x(高(米))/2.4
装载重量的计算:
- 装载米* 1750
有没有人可以为我指明正确的方向?
【问题讨论】:
-
@7uc1f3r 感谢您的修订和有用的反馈。我今天将对此进行处理,并就您的提案提供一些额外的反馈。
-
@7uc1f3r 它按预期完成工作 - 感谢您分享您的经验。
标签: wordpress woocommerce backend product shipping