【问题标题】:How to use the use WooCommerce functions in a WordPress plugin?如何在 WordPress 插件中使用使用 WooCommerce 功能?
【发布时间】:2013-10-07 06:00:48
【问题描述】:

如何在我的插件中使用 WooCommerce 挂钩?这是我想要做的:

add_filter('woocommerce_edit_product_columns', 'pA_manage_posts_columns');
function pA_manage_posts_columns($columns, $post_type = 'product') {
global $woocommerce;
if ( in_array( $post_type, array( 'product') ) ) {
    $columns['offering_price'] = __( 'offering price', 'your_text_domain' ); // this offering price title 
    $columns['offering_qty'] = __( 'Qty', 'your_text_domain' ); // add the quantity title
    }
unset($columns['name']);
return $columns;

这是我在插件中包含 WooCommerce 的方式:

$ds = DIRECTORY_SEPARATOR;
$base_dir = realpath(dirname(__FILE__)  . $ds . '..') . $ds;
$file = "{$base_dir}woocommerce{$ds}woocommerce.php"; 
include_once($file);

仍然无法得到输出

print_r($woocommerce);

【问题讨论】:

  • 你不应该包含插件,而是激活它...
  • 谢谢,这是我激活插件的方法:'code' include_once(ABSPATH .'wp-admin/includes/plugin.php'); activate_plugin(ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php');仍然无法获得添加新列或未设置列的钩子。
  • 为什么不正常激活插件`??为什么需要包含它?
  • 如果你的意思是从管理面板,它已经被激活,但我无法让 woocommerce 过滤器工作。
  • 我可以从 print_r($woocommerce); 得到结果

标签: wordpress plugins woocommerce


【解决方案1】:

你把钩子和回调混在一起了。原话在this file

add_filter( 'manage_edit-product_columns', 'woocommerce_edit_product_columns' );

你的代码应该是:

add_filter( 'manage_edit-product_columns', 'pA_manage_posts_columns', 15 );

function pA_manage_posts_columns( $columns ) 
{
    global $woocommerce;
    $columns['offering_price'] = __( 'offering price', 'your_text_domain' ); // this offering price title 
    $columns['offering_qty'] = __( 'Qty', 'your_text_domain' ); // add the quantity title
    unset($columns['name']);
    return $columns;
}

请注意,回调中没有post_type 参数。过滤器钩子已经告诉你帖子类型是什么:manage_edit-<b>product</b>_columns

由于 Obmerk Kronen 拥有 pointed out,因此无需包含任何 WooCommerce 文件,您已经可以使用它的所有功能。

【讨论】:

  • 谢谢,它现在可用于添加新列,但我无法取消设置列。
  • @user2833755,我们需要设置一个较低的优先级(15,默认是10),所以我们的过滤器在WC添加了它的列之后执行。答案已更新并正在运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 1970-01-01
  • 2015-04-12
  • 2018-12-06
  • 2011-02-20
  • 1970-01-01
相关资源
最近更新 更多