【发布时间】:2021-07-09 10:41:32
【问题描述】:
Wordpress 网站:Woocommerce 插件
我有多个需要导出的元产品字段。
我在 WooCommerce Product CSV Importer & Exporter Documentation 中查找了参考资料,但我在理解如何扩展示例以添加多个自定义列时遇到了一些麻烦。
这部分我很好
$columns 是一个数组
所以我需要做的就是为每个自定义字段添加一个数组项。
/**
* Add the custom column to the exporter and the exporter column menu.
*
* @param array $columns
* @return array $columns
*/
function add_export_column( $columns ) {
// column slug => column name
$columns['my_custom_supplier_product_code'] = 'Supplier Product CODE';
$columns['my_custom_supplier_product_desc'] = 'Supplier Product DESC';
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );
这里我不太确定该怎么做
参考
/**
* Provide the data to be exported for one item in the column.
*
* @param mixed $value (default: '')
* @param WC_Product $product
* @return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'custom_column', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_custom_column', 'add_export_data', 10, 2 );
我的第一个自定义列的 mod
/**
* Provide the data to be exported for one item in the column.
*
* @param mixed $value (default: '')
* @param WC_Product $product
* @return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_code', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_code', 'add_export_data', 10, 2 );
现在如何添加第二列所需的内容?
我应该有
- 2 x add_export_data 函数
- 2 x 添加过滤器
/**
* Provide the data to be exported for one item in CUSTOM COLUMN 1
*
* @param mixed $value (default: '')
* @param WC_Product $product
* @return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data_my_custom_supplier_product_code( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_code', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_code', 'add_export_data_my_custom_supplier_product_code', 10, 2 );
/**
* Provide the data to be exported for one item in CUSTOM COLUMN 2
*
* @param mixed $value (default: '')
* @param WC_Product $product
* @return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data_my_custom_supplier_product_desc( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_desc', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_desc', 'add_export_data_my_custom_supplier_product_desc', 10, 2 );
请注意,我已经尝试过上面的代码,但出于某种原因
- 我的所有列都显示在电子表格中
- 有些是空的,没有从产品自定义字段中提取数据
我现在不是“到处乱搞”试图解决问题,而是退后一步,试图更好地了解它是如何工作的。
谢谢
【问题讨论】:
-
从该文档看来,您似乎确实需要为每个单独的字段添加过滤功能。
-
您对第一列的修改,是否按预期工作? “我的所有列都出现在电子表格中” - “全部”意味着什么,默认列,自定义列,两者都有?基于您期望看到的少于那些? “有些是空的,没有从产品自定义字段中提取数据” - 哪些?这些有什么特别的吗?您从中获得的返回值是否满足代码 cmets
Should be in a format that can be output into a text file (string, numeric, etc)中提到的条件? -
@CBroe 我有 11 个字段需要导出。缺失值似乎不符合任何模式。有些 TEXT 字段已导出,有些则没有。有些 NUMBER 字段已导出,有些则没有。奇怪的是,当我选择 [x] Export all Metadata ...所有正确的值出现在相应的元数据字段中。根据我的帖子评论,我正在退后一步,有条不紊地工作,逐个添加并使用导出测试每个。
-
也许首先要仔细检查所有地方的拼写。钩子的名称似乎与列 slug 直接相关,因此即使是最细微的拼写错误也可能导致它不会为相关字段调用任何函数。
-
哦。补充一点信息:我的自定义字段是使用 ACF : Advanced Custom Fields 创建的。我确实直接进入了mysql数据库
post_meta_ table' and double checked themeta_key`和meta_value。一切看起来都很好。
标签: php wordpress woocommerce