根据WooCommerce产品数据"general"设置选项卡中的自定义字段显示价格后缀



我想添加基于自定义字段的自定义价格后缀。我尝试在functions.php上组合一些现成的代码。

add_action('woocommerce_product_options_general_product_data', 'woocommerce_product_custom_fields');
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields()
{
global $woocommerce, $post;
echo '<div class="product_custom_field">';
woocommerce_wp_text_input(
array(
'id' => '_custom_product_text_field',
'label' => __('Taksit:', 'woocommerce'),
)
);
echo '</div>';
}
function woocommerce_product_custom_fields_save($post_id)
{
$woocommerce_custom_product_text_field = $_POST['_custom_product_text_field'];
if (!empty($woocommerce_custom_product_text_field))
update_post_meta($post_id, '_custom_product_text_field', esc_attr($woocommerce_custom_product_text_field));
}
add_filter( 'woocommerce_get_price_suffix', 'price_taksit_ekle', 99, 4 );

function price_taksit_ekle( $html, $product, $price, $qty ){
$html = get_post_meta($post->ID, '_custom_product_text_field', true);
return $html;
}

然而,这导致以下注意:尝试读取属性";ID";在null上。

有什么建议吗?

您的代码包含一些小错误:

  • 要保存字段,可以使用woocommerce_admin_process_product_object挂钩,与过时的woocommerce_process_product_meta挂钩相反
  • 使用woocommerce_get_price_suffix过滤器挂钩时未定义$post

所以你得到了:

// 1. Add to general_product_data tab
function action_woocommerce_product_options_general_product_data() {
// Text field
woocommerce_wp_text_input( array(
'id'                 => '_custom_product_text_field',
'label'              => __( 'My sufix', 'woocommerce' ),
'placeholder'        => '',
'description'        => __( 'Add your custom sufix', 'woocommerce' ),
'desc_tip'           => true,
));
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
// 2. Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
// Isset
if ( isset( $_POST['_custom_product_text_field'] ) ) {        
// Update
$product->update_meta_data( '_custom_product_text_field', sanitize_text_field( $_POST['_custom_product_text_field'] ) );
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
// 3a. Add sufix
function filter_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
// Get meta
$suffix = $product->get_meta( '_custom_product_text_field' );
// NOT empty
if ( ! empty ( $suffix ) ) {
$html .= $suffix;
// Else condition is OPTIONAL and can be REMOVED if desired
} else {
$html .= __( 'Default', 'woocommerce' ); 
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'filter_woocommerce_get_price_suffix', 10, 4 );

编辑:附加问题-"是否可以只在单个产品页面上显示后缀,以及如何向后缀添加类">

是的,使用这个修改后的(用3b代替3a(代码:

// 3b. Add sufix
function filter_woocommerce_get_price_suffix( $html, $product, $price, $qty ) {
// Only on single product page
if ( ! is_product() ) return $html;
// Get meta
$suffix = $product->get_meta( '_custom_product_text_field' );
// NOT empty
if ( ! empty ( $suffix ) ) {
$html .= '<span class="my-class">' . $suffix . '</span>';
// Else condition is OPTIONAL and can be REMOVED if desired
} else {
$html .= '<span class="my-class">' . __( 'Default', 'woocommerce' ) . '</span>'; 
}
return $html;
}
add_filter( 'woocommerce_get_price_suffix', 'filter_woocommerce_get_price_suffix', 10, 4 );

最新更新