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

问题描述 投票:0回答:1

我想根据自定义字段添加自定义价格后缀。我尝试在

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;
}

但是,这会导致以下通知:尝试读取 null 上的属性“ID”。

有什么建议吗?

wordpress woocommerce product hook-woocommerce suffix
1个回答
1
投票

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

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

所以你得到:

// 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 suffix', 'woocommerce' ),
        'placeholder'        => '',
        'description'        => __( 'Add your custom suffix', '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 suffix
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 );

编辑:附加问题 - “是否可以仅在单个产品页面上显示后缀以及如何向后缀添加类?”

是的,请使用此修改后的代码(将 3a 替换为 3b):

// 3b. Add suffix
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 );
© www.soinside.com 2019 - 2024. All rights reserved.