我们使用wordpress,woocommerce,woocommerce智能优惠券(大量功能,包括批量导出优惠券功能)和woocommerce自动添加的优惠券pro(添加了bogo功能)
我需要使用复选框从中导入10,000张bogo优惠券 woocommerce自动添加了优惠券专业插件。在我看来,我需要获取id ='_ wjecf_multiply_free_products'以导出到CSV,并在该列中填充“是”
I'm pretty sure I can use this file as a template for integrating a custom field to be exported to CSV, but I'm struggling:
<?php
/**
* Class to handle feature Coupons By User Role
*
* @author StoreApps
* @category Admin
* @package wocommerce-smart-coupons/includes
* @version 1.2.3
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'WC_SC_Coupons_By_User_Role' ) ) {
/**
* Class WC_SC_Coupons_By_User_Role
*/
class WC_SC_Coupons_By_User_Role {
/**
* Variable to hold instance of this class
*
* @var $instance
*/
private static $instance = null;
/**
* Constructor
*/
public function __construct() {
add_action( 'woocommerce_coupon_options_usage_restriction', array( $this, 'usage_restriction' ), 10, 2 );
add_action( 'save_post', array( $this, 'process_meta' ), 10, 2 );
add_filter( 'woocommerce_coupon_is_valid', array( $this, 'validate' ), 11, 2 );
add_filter( 'wc_smart_coupons_export_headers', array( $this, 'export_headers' ) );
add_filter( 'wc_sc_export_coupon_meta', array( $this, 'export_coupon_meta_data' ), 10, 2 );
add_filter( 'smart_coupons_parser_postmeta_defaults', array( $this, 'postmeta_defaults' ) );
add_filter( 'sc_generate_coupon_meta', array( $this, 'generate_coupon_meta' ), 10, 2 );
add_filter( 'wc_sc_process_coupon_meta_value_for_import', array( $this, 'process_coupon_meta_value_for_import' ), 10, 2 );
add_filter( 'is_protected_meta', array( $this, 'make_action_meta_protected' ), 10, 3 );
}
/**
* Get single instance of this class
*
* @return this class Singleton object of this class
*/
public static function get_instance() {
// Check if instance is already exists.
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Handle call to functions which is not available in this class
*
* @param string $function_name The function name.
* @param array $arguments Array of arguments passed while calling $function_name.
* @return result of function call
*/
public function __call( $function_name = '', $arguments = array() ) {
global $woocommerce_smart_coupon;
if ( ! is_callable( array( $woocommerce_smart_coupon, $function_name ) ) ) {
return;
}
if ( ! empty( $arguments ) ) {
return call_user_func_array( array( $woocommerce_smart_coupon, $function_name ), $arguments );
} else {
return call_user_func( array( $woocommerce_smart_coupon, $function_name ) );
}
}
/**
* Display field for coupon by user role
*
* @param integer $coupon_id The coupon id.
* @param WC_Coupon $coupon The coupon object.
*/
public function usage_restriction( $coupon_id = 0, $coupon = null ) {
$user_role_ids = array();
if ( ! empty( $coupon_id ) ) {
$user_role_ids = get_post_meta( $coupon_id, 'wc_sc_user_role_ids', true );
if ( empty( $user_role_ids ) || ! is_array( $user_role_ids ) ) {
$user_role_ids = array();
}
}
$available_user_roles = $this->get_available_user_roles();
?>
<div class="options_group smart-coupons-field">
<p class="form-field">
<label for="wc_sc_user_role_ids"><?php echo esc_html__( 'Allowed user roles', 'woocommerce-smart-coupons' ); ?></label>
<select id="wc_sc_user_role_ids" name="wc_sc_user_role_ids[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php esc_attr_e( 'No user roles', 'woocommerce-smart-coupons' ); ?>">
<?php
if ( ! empty( $available_user_roles ) && is_array( $available_user_roles ) ) {
foreach ( $available_user_roles as $role_id => $role ) {
$role_name = translate_user_role( $role['name'] );
echo '<option value="' . esc_attr( $role_id ) . '"' . esc_attr( selected( in_array( $role_id, $user_role_ids, true ), true, false ) ) . '>' . esc_html( $role_name ) . '</option>';
}
}
?>
</select>
<?php
$tooltip_text = esc_html__( 'User Role that must be selected during checkout for this coupon to be valid.', 'woocommerce-smart-coupons' );
echo wc_help_tip( $tooltip_text ); // phpcs:ignore
?>
</p>
</div>
<?php
}
/**
* Save coupon by user role data in meta
*
* @param Integer $post_id The coupon post ID.
* @param WP_Post $post The coupon post.
*/
public function process_meta( $post_id = 0, $post = null ) {
if ( empty( $post_id ) || empty( $post ) || empty( $_POST ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( is_int( wp_is_post_revision( $post ) ) ) {
return;
}
if ( is_int( wp_is_post_autosave( $post ) ) ) {
return;
}
if ( empty( $_POST['woocommerce_meta_nonce'] ) || ! wp_verify_nonce( wc_clean( wp_unslash( $_POST['woocommerce_meta_nonce'] ) ), 'woocommerce_save_data' ) ) { // phpcs:ignore
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( 'shop_coupon' !== $post->post_type ) {
return;
}
$user_role_ids = ( isset( $_POST['wc_sc_user_role_ids'] ) ) ? wc_clean( wp_unslash( $_POST['wc_sc_user_role_ids'] ) ) : array(); // phpcs:ignore
update_post_meta( $post_id, 'wc_sc_user_role_ids', $user_role_ids );
}
/**
* Validate the coupon based on user role
*
* @param boolean $valid Is valid or not.
* @param WC_Coupon $coupon The coupon object.
*
* @throws Exception If the coupon is invalid.
* @return boolean Is valid or not
*/
public function validate( $valid = false, $coupon = object ) {
// If coupon is invalid already, no need for further checks.
if ( false === $valid ) {
return $valid;
}
$coupon_id = ( $this->is_wc_gte_30() ) ? $coupon->get_id() : $coupon->id;
$user_role_ids = get_post_meta( $coupon_id, 'wc_sc_user_role_ids', true );
if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) {
$current_user = wp_get_current_user();
// Check if current user's role is allowed.
if ( ! array_intersect( $current_user->roles, $user_role_ids ) ) {
throw new Exception( __( 'This coupon is not valid for you.', 'woocommerce-smart-coupons' ) );
}
}
return $valid;
}
/**
* Add meta in export headers
*
* @param array $headers Existing headers.
* @return array
*/
public function export_headers( $headers = array() ) {
$headers['wc_sc_user_role_ids'] = __( 'User Role', 'woocommerce-smart-coupons' );
return $headers;
}
/**
* Function to handle coupon meta data during export of existing coupons
*
* @param mixed $meta_value The meta value.
* @param array $args Additional arguments.
* @return string Processed meta value
*/
public function export_coupon_meta_data( $meta_value = '', $args = array() ) {
if ( ! empty( $args['meta_key'] ) && 'wc_sc_user_role_ids' === $args['meta_key'] ) {
if ( isset( $args['meta_value'] ) && ! empty( $args['meta_value'] ) ) {
$user_role_ids = maybe_unserialize( stripslashes( $args['meta_value'] ) );
if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) {
$user_role_names = $this->get_user_role_names_by_ids( $user_role_ids );
if ( is_array( $user_role_names ) && ! empty( $user_role_names ) ) {
$meta_value = implode( '|', wc_clean( wp_unslash( $user_role_names ) ) ); // Replace user role ids with their respective role name.
}
}
}
}
return $meta_value;
}
/**
* Post meta defaults for user role ids meta
*
* @param array $defaults Existing postmeta defaults.
* @return array
*/
public function postmeta_defaults( $defaults = array() ) {
$defaults['wc_sc_user_role_ids'] = '';
return $defaults;
}
/**
* Add user role's meta with value in coupon meta
*
* @param array $data The row data.
* @param array $post The POST values.
* @return array Modified data
*/
public function generate_coupon_meta( $data = array(), $post = array() ) {
$user_role_names = '';
if ( ! empty( $post['wc_sc_user_role_ids'] ) && is_array( $post['wc_sc_user_role_ids'] ) ) {
$user_role_names = $this->get_user_role_names_by_ids( $post['wc_sc_user_role_ids'] );
if ( is_array( $user_role_names ) && ! empty( $user_role_names ) ) {
$user_role_names = implode( '|', wc_clean( wp_unslash( $user_role_names ) ) );
}
}
$data['wc_sc_user_role_ids'] = $user_role_names; // Replace user role ids with their respective role name.
return $data;
}
/**
* Function to get user role titles for given user role ids
*
* @param array $user_role_ids ids of user roles.
* @return array $user_role_names titles of user roles
*/
public function get_user_role_names_by_ids( $user_role_ids = array() ) {
$user_role_names = array();
if ( is_array( $user_role_ids ) && ! empty( $user_role_ids ) ) {
$available_user_roles = $this->get_available_user_roles();
foreach ( $user_role_ids as $index => $user_role_id ) {
$user_role = ( isset( $available_user_roles[ $user_role_id ] ) && ! empty( $available_user_roles[ $user_role_id ] ) ) ? $available_user_roles[ $user_role_id ] : '';
if ( is_array( $user_role ) && ! empty( $user_role ) ) {
$user_role_name = ! empty( $user_role['name'] ) ? $user_role['name'] : '';
if ( ! empty( $user_role_name ) ) {
$user_role_names[ $index ] = $user_role_name; // Replace user role id with it's repective name.
} else {
$user_role_names[ $index ] = $user_role_id; // In case of empty user role name replace it with role id.
}
}
}
}
return $user_role_names;
}
/**
* Process coupon meta value for import
*
* @param mixed $meta_value The meta value.
* @param array $args Additional Arguments.
* @return mixed $meta_value
*/
public function process_coupon_meta_value_for_import( $meta_value = null, $args = array() ) {
if ( ! empty( $args['meta_key'] ) && 'wc_sc_user_role_ids' === $args['meta_key'] ) {
$meta_value = ( ! empty( $args['postmeta']['wc_sc_user_role_ids'] ) ) ? explode( '|', wc_clean( wp_unslash( $args['postmeta']['wc_sc_user_role_ids'] ) ) ) : array();
if ( is_array( $meta_value ) && ! empty( $meta_value ) ) {
$available_user_roles = $this->get_available_user_roles();
if ( is_array( $available_user_roles ) && ! empty( $available_user_roles ) ) {
foreach ( $meta_value as $index => $user_role_name ) {
foreach ( $available_user_roles as $role_id => $user_role ) {
$role_name = isset( $user_role['name'] ) ? $user_role['name'] : '';
if ( $role_name === $user_role_name ) {
$meta_value[ $index ] = $role_id; // Replace user role title with it's repective id.
}
}
}
}
}
}
return $meta_value;
}
/**
* Make meta data of user role ids protected
*
* @param bool $protected Is protected.
* @param string $meta_key The meta key.
* @param string $meta_type The meta type.
* @return bool $protected
*/
public function make_action_meta_protected( $protected = false, $meta_key = '', $meta_type = '' ) {
if ( 'wc_sc_user_role_ids' === $meta_key ) {
return true;
}
return $protected;
}
/**
* Function to get available user roles which current user use.
*
* @return array $available_user_roles Available user roles
*/
public function get_available_user_roles() {
$available_user_roles = array();
if ( ! function_exists( 'get_editable_roles' ) ) {
require_once ABSPATH . 'wp-admin/includes/user.php';
}
if ( function_exists( 'get_editable_roles' ) ) {
$available_user_roles = get_editable_roles();
}
return $available_user_roles;
}
}
}
WC_SC_Coupons_By_User_Role::get_instance();
这是我在woocommerce自动添加优惠券专业版中看到的内容:
<?php
defined( 'ABSPATH' ) or die();
class WJECF_Pro_Free_Products_Admin {
private $plugin = null;
public function __construct( $plugin ) {
$this->plugin = $plugin;
}
/* ADMIN HOOKS */
public function init_admin_hook() {
//Inject columns
if ( WJECF()->is_pro() ) {
WJECF()->inject_coupon_column(
'_wjecf_free_products',
__( 'Free products', 'woocommerce-jos-autocoupon' ),
array( $this, 'admin_render_shop_coupon_columns' ),
'products'
);
}
//Add the tab to the coupon edit page
add_filter( 'woocommerce_coupon_data_tabs', array( $this, 'filter_woocommerce_coupon_data_tabs' ), 10, 1 );
add_action( 'woocommerce_coupon_data_panels', array( $this, 'action_woocommerce_coupon_data_panels' ), 10, 0 );
//Metabox
add_action( 'wjecf_coupon_metabox_products', array( $this, 'admin_coupon_metabox_products' ), 15, 2 );
}
//Add tabs to the coupon option page
public function filter_woocommerce_coupon_data_tabs( $tabs ) {
$tabs['extended_features_products'] = array(
'label' => __( 'Free products', 'woocommerce-jos-autocoupon' ),
'target' => 'wjecf_coupondata_free_products',
'class' => 'wjecf_coupondata_free_products',
);
return $tabs;
}
//Add panel to the coupon option page
public function action_woocommerce_coupon_data_panels() {
global $thepostid, $post;
$thepostid = empty( $thepostid ) ? $post->ID : $thepostid;
?>
<div id="wjecf_coupondata_free_products" class="panel woocommerce_options_panel">
<?php
//Feed the panel with options
/**
* @since 2.5.0
* Deprecated wjecf_coupon_metabox_products
* Replaced by wjecf_coupon_metabox_free_products
* All other items from this tab are in "Usage restriction" now
*/
do_action( 'wjecf_coupon_metabox_products', $thepostid, $post );
do_action( 'wjecf_coupon_metabox_free_products', $thepostid, $post );
WJECF_Admin()->admin_coupon_data_footer();
?>
</div>
<?php
}
public function admin_render_shop_coupon_columns( $column, $post ) {
switch ( $column ) {
case '_wjecf_free_products':
$free_product_ids = $this->plugin->get_coupon_free_product_ids( WJECF_WC()->get_coupon( (int) $post->ID ) );
echo esc_html( implode( ', ', $free_product_ids ) );
break;
}
}
public function admin_coupon_metabox_products( $thepostid, $post ) {
//=============================
//Title
echo '<h3>' . esc_html( __( 'Free products', 'woocommerce-jos-autocoupon' ) ) . "</h3>\n";
//=============================
//Free product ids
$free_product_ids = $this->plugin->get_coupon_free_product_ids( WJECF_WC()->get_coupon( (int) $thepostid ) );
echo '<p class="form-field"><label>' . __( 'Free products', 'woocommerce' ) . '</label>';
WJECF_Admin_Html::render_admin_product_selector( 'wjecf_free_product_ids', '_wjecf_free_product_ids', $free_product_ids, null );
echo WJECF_Admin_Html::wc_help_tip( __( 'Free products that will be added to the cart when this coupon is applied.', 'woocommerce-jos-autocoupon' ) );
echo '</p>';
//=============================
//2.3.0 Select free product
woocommerce_wp_checkbox(
array(
'id' => '_wjecf_must_select_free_product',
'label' => __( 'Customer must select', 'woocommerce-jos-autocoupon' ),
'description' => __( 'Check this box if the customer must choose from the free products.', 'woocommerce-jos-autocoupon' ),
)
);
//=============================
//2.3.0 Select free product
$message = $this->plugin->get_select_free_product_message( $thepostid, 'raw' );
woocommerce_wp_text_input(
array(
'id' => '_wjecf_select_free_product_message',
'label' => __( '\'Select your gift\'-message', 'woocommerce-jos-autocoupon' ),
'placeholder' => __( 'Please choose your free gift:', 'woocommerce-jos-autocoupon' ),
'description' => __( 'This message is displayed when the customer must choose a free product.', 'woocommerce-jos-autocoupon' ),
'desc_tip' => true,
'value' => $message,
)
);
//=============================
//2.2.2 Allow multiplying the free products
woocommerce_wp_checkbox(
array(
'id' => '_wjecf_multiply_free_products',
'label' => __( 'Allow multiplication of the free products', 'woocommerce-jos-autocoupon' ),
'description' => // '<b>' . __( 'EXPERIMENTAL: ', 'woocommerce-jos-autocoupon' ) . '</b>' .
__( 'The amount of free products is multiplied every time the minimum spend, subtotal or quantity is reached.', 'woocommerce-jos-autocoupon' ),
)
);
//=============================
//2.2.5 BOGO All matching products
woocommerce_wp_checkbox(
array(
'id' => '_wjecf_bogo_matching_products',
'label' => __( 'BOGO matching products', 'woocommerce-jos-autocoupon' ),
'description' => // '<b>' . __( 'EXPERIMENTAL: ', 'woocommerce-jos-autocoupon' ) . '</b>' .
__( 'Buy one or more of any of the matching products (see \'Usage Restriction\'-tab) and get one free. Check \'Allow multiplication\' to get one free item for every matching item in the cart.', 'woocommerce-jos-autocoupon' ),
)
);
}
public function admin_coupon_meta_fields( $coupon ) {
return array(
'_wjecf_free_product_ids' => 'int,',
'_wjecf_multiply_free_products' => 'yesno',
'_wjecf_bogo_matching_products' => 'yesno',
'_wjecf_must_select_free_product' => 'yesno',
'_wjecf_select_free_product_message' => 'clean',
);
}
}