frozzare / wc-export

从 WooCommerce 导出各种数据

安装: 16

依赖: 0

建议者: 0

安全性: 0

星标: 2

关注者: 2

分支: 0

开放问题: 0

类型:wordpress-plugin

v1.0.0 2016-07-20 10:17 UTC

This package is auto-updated.

Last update: 2024-08-29 04:25:26 UTC


README

Build Status No Maintenance Intended

从 WooCommerce 导出各种数据。

插件自带两种导出器,一个是客户导出,它会导出所有账单字段;另一个是电子邮件导出器,它会从订单中导出账单电子邮件字段。

内置三种编写器,CSV、XML 和 JSON。

安装

composer require frozzare/wc-export

文档

自定义导出

自定义导出类型的示例,将订单中的 billing_email 字段导出。使用 query_args 方法可以修改 WP_Query 参数。

<?php

use Frozzare\WooCommerce\Export\Exports\Export;

class Custom_Emails extends Export {

	/**
	 * Get fields that should be exported.
	 *
	 * @return array
	 */
	public function get_fields() {
		return [
			'billing_email' => __( 'Email', 'woocommerce' )
		];
	}

	/**
	 * Modify WP Query args.
	 *
	 * @param  array  $args
	 *
	 * @return array
	 */
	public function query_args( array $args ) {
		return $args;
	}
}

然后,您需要使用 wc_export_classes 过滤器将自定义导出添加到导出器列表中。

<?php

/**
 * Add export classes.
 *
 * @param  array $exports
 *
 * @return array
 */
add_filter( 'wc_export_classes', function ( array $exports ) {
	return array_merge( $exports, [
		'Custom emails' => '\\Custom_Emails'
	] );
} );

自定义编写器

自定义编写器示例,将给定数据参数作为 JSON 导出到 render 方法。

<?php

use Frozzare\WooCommerce\Export\Writers\Writer;

class Custom_JSON extends Writer {

	/**
	 * Get the content type.
	 *
	 * @var string
	 */
	protected function get_content_type() {
		return 'application/json';
	}

	/**
	 * Get the file extension.
	 *
	 * @var string
	 */
	protected function get_extension() {
		return 'json';
	}

	/**
	 * Render JSON file.
	 *
	 * @param array $data
	 */
	protected function render( array $data ) {
		foreach ( $data as $index => $row ) {
			if ( ! is_array( $row ) ) {
				unset( $data[$index] );
			}
		}

		echo json_encode( $data, JSON_UNESCAPED_UNICODE );
	}
}

然后,您需要使用 wc_export_writers 过滤器将自定义编写器添加到编写器列表中。

<?php

/**
 * Add export writer.
 *
 * @param  array $writers
 *
 * @return array
 */
add_filter( 'wc_export_writers', function ( array $writers ) {
	return array_merge( $writers, [
		'Custom JSON' => '\\Custom_JSON'
	] );
} );

许可证

MIT © Fredrik Forsmo