简体   繁体   English

自定义插件的 Woocommerce 管理工具提示

[英]Woocommerce admin tooltip for a custom plugin

I build my Woocommerce plugin and I need to put some content next to my inputs with tooltip.我构建了我的 Woocommerce 插件,我需要使用工具提示在我的输入旁边放置一些内容。 I found this function: wc_help_tip() from Woocommerce Docs but I don't understand, and doesn't work.我从Woocommerce Docs中找到了这个函数: wc_help_tip()但我不明白,也不起作用。

Here's my code:这是我的代码:

<?php 
    $tip = "test";
    echo wc_help_tip($tip, false);
?>

When I debug with F12, I saw a span content:当我用 F12 调试时,我看到了一个 span 内容:

<span class="woocommerce-help-tip" data-tip="test"></span> 

But nothing appears in frontend.但是前端什么都没有出现。

Any ideas to this?对此有什么想法吗? Or something else to put native tooltip of WordPress?或者其他的东西来放置 WordPress 的原生工具提示?

EDIT : I need to use it in a custom admin backend page hook not in front end nor woocommerce admin backend page编辑:我需要在自定义管理后端页面挂钩中使用它,而不是在前端也不是 woocommerce 管理后端页面

You should add your screen id to woocommerce.您应该将您的屏幕 ID 添加到 woocommerce。 Use woocommerce_screen_ids filter使用 woocommerce_screen_ids 过滤器

Example:例子:

add_filter('woocommerce_screen_ids', [ $this, 'set_wc_screen_ids' ] );

public function set_wc_screen_ids( $screen ){
      $screen[] = 'your_screen_id';
      return $screen;
}

Make sure you had enqueued the TipTip JS for this, Here is the code which may help you, Copy the below code and paste where your all javascript files are enqueuing确保您已为此排队 TipTip JS,这是可能对您有所帮助的代码,复制下面的代码并粘贴所有 javascript 文件排队的位置

<?php
wp_register_script( 'woocommerce_admin', WC()->plugin_url() . '/assets/js/admin/woocommerce_admin.js', array( 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'jquery-tiptip' ), WC_VERSION );
$locale = localeconv();
$decimal = isset( $locale['decimal_point'] ) ? $locale['decimal_point'] : '.';

$params = array(
/* translators: %s: decimal */
'i18n_decimal_error' => sprintf( __( 'Please enter in decimal (%s) format without thousand separators.', 'woocommerce' ), $decimal ),
/* translators: %s: price decimal separator */
'i18n_mon_decimal_error' => sprintf( __( 'Please enter in monetary decimal (%s) format without thousand separators and currency symbols.', 'woocommerce' ), wc_get_price_decimal_separator() ),
'i18n_country_iso_error' => __( 'Please enter in country code with two capital letters.', 'woocommerce' ),
'i18_sale_less_than_regular_error' => __( 'Please enter in a value less than the regular price.', 'woocommerce' ),
'decimal_point' => $decimal,
'mon_decimal_point' => wc_get_price_decimal_separator(),
'strings' => array(
'import_products' => __( 'Import', 'woocommerce' ),
'export_products' => __( 'Export', 'woocommerce' ),
),
'urls' => array(
'import_products' => esc_url_raw( admin_url( 'edit.php?post_type=product&page=product_importer' ) ),
'export_products' => esc_url_raw( admin_url( 'edit.php?post_type=product&page=product_exporter' ) ),
),
);

wp_localize_script( 'woocommerce_admin', 'woocommerce_admin', $params );
wp_enqueue_script( 'woocommerce_admin' );

You don't need to load the entire WooCommerce admin scripts for this.您无需为此加载整个 WooCommerce 管理脚本。 In fact, this is part of a jQuery plugin that WooCommerce uses, and not part of the WooCommerce code itself.事实上,这是 WooCommerce 使用的 jQuery 插件的一部分,而不是 WooCommerce 代码本身的一部分。

So, first of all, you need to make sure that jquery-tiptip is loaded on your page:因此,首先,您需要确保jquery-tiptip已加载到您的页面上:

wp_enqueue_script( 'jquery-tiptip' );

Then, you initialize the tipTip() function on your woocommerce-help-tip elements, using JS:然后,使用 JS 在woocommerce-help-tip元素上初始化tipTip()函数:

jQuery( function( $ ) {
    $('.woocommerce-help-tip').tipTip({
        'attribute': 'data-tip',
        'fadeIn':    50,
        'fadeOut':   50,
        'delay':     200
    });
});

make sure that when loading this script, you include jquery-tiptip as a dependency (3rd argument to wp_enqueue_script() )确保在加载此脚本时,将jquery-tiptip作为依赖项包含( wp_enqueue_script()的第三个参数)

wp_enqueue_script(
    'your-script',
    plugin_dir_url( __FILE__ ) . '/js/example.js',
    array( 'jquery', 'jquery-tiptip' ),
    '1.0.0'
);

This function is made for backend…此功能是为后端...

Below you have an example that will output the tooltip in order edit pages:下面有一个示例,它将在订单编辑页面中输出工具提示

// Displayed in Order edit pages below order details on top first column
add_action( 'woocommerce_admin_order_data_after_order_details', 'displaying_tooltip_in_order_edit_pages', 10, 1 );
function displaying_tooltip_in_order_edit_pages( $order ){
    ?>
        <p class="form-field form-field-wide wc-customer-custom">Some text with a tooltip <?php echo wc_help_tip("hello world"); ?></p>
    <?php
}

Code goes in function.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。

Tested and works.测试和工作。

在此处输入图像描述

So if you add some code to display some custom fields or content in woocommerce admin pages through hooks, you can add those tooltips with wc_help_tip() function因此,如果您添加一些代码以通过钩子在 woocommerce 管理页面中显示一些自定义字段或内容,您可以使用wc_help_tip()函数添加这些工具提示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM