2 回答

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
我要做的就是使用參數(shù)value中的參數(shù)woocommerce_wp_text_input。然后,您可以向其傳遞當(dāng)前值或默認(rèn)值(如果尚不存在)。
還添加了新的保存例程woocommerce_admin_process_product_object
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'so_63588126_product_custom_fields');
// Save Fields
add_action('woocommerce_admin_process_product_object', 'so_63588126_product_custom_fields_save');
function so_63588126_product_custom_fields()
{
global $product_object;
$value = $product_object->get_meta( 'weight', true );
if ( ! $value ) {
$value = 0;
}
echo '<div class="product_custom_field">';
// Custom Product Text Field
woocommerce_wp_text_input(
array(
'id' => 'weight',
'placeholder' => 'Weight of the product - values a>b',
'label' => __('Weight of the product', 'your-textdomain'),
'desc_tip' => 'true',
'value' => $value
)
);
echo '</div>';
}
function so_63588126_product_custom_fields_save( $product )
{
// Custom Product Text Field
$woocommerce_custom_product_text_field = $_POST['weight'];
if ( ! empty( $_POST['weight'] ) ) {
$product->update_meta_data( 'weight', sanitize_text_field( wp_unslash( $_POST['weight'] ) ) );
} else {
$product->update_meta_data( 'weight', 0 );
}
}
補(bǔ)充說明:
我使用$product_object->update_meta_data()WooCommerce(自 3.0 起)更喜歡在對象上使用 CRUD(創(chuàng)建、讀取、更新、刪除)方法。update_post_meta()仍然有效,但如果/當(dāng) Woo 決定對產(chǎn)品和產(chǎn)品元使用自定義表時(shí),CRUD 將是面向未來的。
了解有關(guān) CRUD 的更多信息:https ://docs.woocommerce.com/document/developing-using-woocommerce-crud-objects/
關(guān)于文本域...這是自定義代碼,因此為了正確翻譯它,您需要使用您自己的獨(dú)特文本域,而不是“woocommerce”。
閱讀有關(guān)文本域的更多信息:https ://developer.wordpress.org/themes/functionality/internationalization/#text-domain
最后,sanitize_text_field()可能不是清理的正確選擇,但我不知道您在那里存儲了什么樣的數(shù)據(jù)。intval()對于數(shù)字會更好...因此可以通過將參數(shù)設(shè)置為 args將type輸入設(shè)置為數(shù)字輸入類型。'number'woocommerce_wp_text_input()

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是對我有用的解決方案。
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
? ? function woocommerce_product_custom_fields()
? ? {
? ? ? ? global $woocommerce, $post, $product_object;
? ? ? ? $value = $product_object->get_meta( 'priority', true );
? ? ? ? if ( ! $value ) {
? ? ? ? ? ? $value = 0;
? ? ? ? }
? ? ? ??
? ? ? ? echo '<div class="product_custom_field">';
? ? ? ? // Custom Product Text Field
? ? ? ? woocommerce_wp_text_input(
? ? ? ? ? ? array(
? ? ? ? ? ? ? ? 'id' => 'priority',
? ? ? ? ? ? ? ? 'placeholder' => 'Priority - number 1>0',
? ? ? ? ? ? ? ? 'label' => __('Priority', 'woocommerce'),
? ? ? ? ? ? ? ? 'type' => 'number',
? ? ? ? ? ? ? ? 'desc_tip' => 'true',
? ? ? ? ? ? ? ? 'value' => $value
? ? ? ? ? ? )
? ? ? ? );
? ? ? ? echo '</div>';
? ? }
? ? function woocommerce_product_custom_fields_save($post_id)
? ? {
? ? ? ? // Custom Product Text Field
? ? ? ? if (array_key_exists('priority', $_POST)) {
? ? ? ? ? ? update_post_meta($post_id, 'priority', intval($_POST['priority']));
? ? ? ? }
? ? }
? ? function cw_add_postmeta_ordering_args( $args_sort_cw ) {
? ? ? $cw_orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) :
? ? ? ? ? ? apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
? ? ? switch( $cw_orderby_value ) {
? ? ? ? ? ?case 'priority':
? ? ? ? ? ? ? ? $args_sort_cw['orderby'] = 'meta_value';
? ? ? ? ? ? ? ? $args_sort_cw['order'] = 'desc';
? ? ? ? ? ? ? ? $args_sort_cw['meta_key'] = 'priority';
? ? ? ? ? ? ? ? break;
? ? ? }
? ? ? return $args_sort_cw;
? ? }
? ? add_filter( 'woocommerce_get_catalog_ordering_args', 'cw_add_postmeta_ordering_args' );
? ? function cw_add_new_postmeta_orderby( $sortby ) {
? ? ? ?$sortby['priority'] = __( 'Recommended', 'woocommerce' );
? ? ? ?return $sortby;
? ? }
? ? add_filter( 'woocommerce_default_catalog_orderby_options', 'cw_add_new_postmeta_orderby' );
? ? add_filter( 'woocommerce_catalog_orderby', 'cw_add_new_postmeta_orderby' );
調(diào)整了該woocommerce_product_custom_fields_save()功能array_key_exists(),現(xiàn)在工作正常。
- 2 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)