post_title ) ) {
return;
}
$shop_page_id = get_option( 'woocommerce_shop_page_id', 0 );
if ( ! empty( $shop_page_id ) ) {
$shop_page_id = 'shop';
} else {
$home_url = home_url();
$shop_page_id = ( function_exists( 'wpcom_vip_url_to_postid' ) ) ? wpcom_vip_url_to_postid( $home_url ) : url_to_postid( $home_url ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.url_to_postid_url_to_postid
}
if ( empty( $shop_page_id ) ) {
$shop_page_id = 'cart';
}
$coupon_share_url = add_query_arg(
array(
'coupon-code' => $post->post_title,
'sc-page' => $shop_page_id,
),
home_url( '/' )
);
?>
is_wc_gte_32() ) {
woocommerce_wp_hidden_input(
array(
'id' => 'wc_sc_expiry_time',
)
);
woocommerce_wp_text_input(
array(
'id' => 'wc_sc_expiry_time_picker',
'label' => __( 'Coupon expiry time', 'woocommerce-smart-coupons' ),
'placeholder' => esc_attr__( 'HH:MM', 'woocommerce-smart-coupons' ),
'description' => __( 'Time after which coupon will be expired. This will work in conjunction with Coupon expiry date.', 'woocommerce-smart-coupons' ),
'type' => 'text',
'desc_tip' => true,
'custom_attributes' => array(
'autocomplete' => 'off',
),
)
);
}
// Max discount field for percentage type coupon.
woocommerce_wp_text_input(
array(
'id' => 'wc_sc_max_discount',
'label' => __( 'Max discount', 'woocommerce-smart-coupons' ),
'placeholder' => esc_attr__( 'Unlimited discount', 'woocommerce-smart-coupons' ),
'description' => __( 'The maximum discount this coupon can give on a cart.', 'woocommerce-smart-coupons' ),
'type' => 'number',
'desc_tip' => true,
'custom_attributes' => array(
'step' => 'any',
'min' => 0,
),
)
);
woocommerce_wp_checkbox(
array(
'id' => 'sc_restrict_to_new_user',
'label' => __( 'For new user only?', 'woocommerce-smart-coupons' ),
'description' => __( 'When checked, this coupon will be valid for the user\'s first order on the store.', 'woocommerce-smart-coupons' ),
)
);
if ( true === $is_callable_coupon_get_meta ) {
$sc_coupon_validity = $coupon->get_meta( 'sc_coupon_validity' );
$validity_suffix = $coupon->get_meta( 'validity_suffix' );
} else {
$sc_coupon_validity = get_post_meta( $post->ID, 'sc_coupon_validity', true );
$validity_suffix = get_post_meta( $post->ID, 'validity_suffix', true );
}
?>
>
>
>
>
'is_pick_price_of_product',
'label' => __( 'Coupon value same as product\'s price?', 'woocommerce-smart-coupons' ),
'description' => __( 'When checked, generated coupon\'s value will be same as product\'s price', 'woocommerce-smart-coupons' ),
)
);
woocommerce_wp_checkbox(
array(
'id' => 'auto_generate_coupon',
'label' => __( 'Auto generate new coupons with each item', 'woocommerce-smart-coupons' ),
'description' => __( 'Generate exact copy of this coupon with unique coupon code for each purchased product (needs this coupon to be linked with that product)', 'woocommerce-smart-coupons' ),
)
);
}
echo '
';
?>
get_meta( 'coupon_title_prefix' );
$coupon_title_suffix = $coupon->get_meta( 'coupon_title_suffix' );
} elseif ( ! empty( $post->ID ) ) {
$coupon_title_prefix = get_post_meta( $post->ID, 'coupon_title_prefix', true );
$coupon_title_suffix = get_post_meta( $post->ID, 'coupon_title_suffix', true );
}
?>
coupon_code
';
if ( ! $is_page_bulk_generate ) {
echo '
';
// for disabling e-mail restriction.
woocommerce_wp_checkbox(
array(
'id' => 'sc_is_visible_storewide',
'label' => __( 'Show on cart, checkout', 'woocommerce-smart-coupons' ) . ' ' . __( 'and my account?', 'woocommerce-smart-coupons' ),
'description' => __( 'When checked, this coupon will be visible on cart/checkout page for everyone', 'woocommerce-smart-coupons' ),
)
);
echo '
';
}
?>
$this,
'coupon_id' => $coupon_id,
'coupon_obj' => $coupon,
)
);
$generated_from_order_id = ( is_object( $coupon ) && $this->is_callable( $coupon, 'get_meta' ) ) ? $coupon->get_meta( 'generated_from_order_id' ) : $this->get_post_meta( $coupon_id, 'generated_from_order_id', true );
if ( empty( $generated_from_order_id ) ) {
woocommerce_wp_checkbox(
array(
'id' => 'sc_disable_email_restriction',
'label' => __( 'Disable email restriction?', 'woocommerce-smart-coupons' ),
'description' => __( 'Do not restrict auto-generated coupons to buyer/receiver email, anyone with coupon code can use it', 'woocommerce-smart-coupons' ),
)
);
}
?>
is_callable( $coupon, 'get_meta' ) ) ? $coupon->get_discount_type() : get_post_meta( $post_id, 'discount_type', true );
if ( true === $this->is_callable( $coupon, 'update_meta_data' ) ) {
if ( isset( $_POST['sc_restrict_to_new_user'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_restrict_to_new_user', $post_sc_restrict_to_new_user );
} else {
$coupon->update_meta_data( 'sc_restrict_to_new_user', 'no' );
}
if ( isset( $_POST['auto_generate_coupon'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'auto_generate_coupon', $post_auto_generate_coupon );
} else {
if ( 'smart_coupon' === $type ) {
$coupon->update_meta_data( 'auto_generate_coupon', 'yes' );
} else {
$coupon->update_meta_data( 'auto_generate_coupon', 'no' );
}
}
if ( isset( $_POST['usage_limit_per_user'] ) ) { // phpcs:ignore
$coupon->set_usage_limit_per_user( $post_usage_limit_per_user );
}
if ( isset( $_POST['limit_usage_to_x_items'] ) ) { // phpcs:ignore
$coupon->set_limit_usage_to_x_items( $post_limit_usage_to_x_items );
}
if ( 'smart_coupon' === $type ) {
$coupon->update_meta_data( 'apply_before_tax', 'no' );
}
if ( isset( $_POST['coupon_title_prefix'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'coupon_title_prefix', $post_coupon_title_prefix );
}
if ( isset( $_POST['coupon_title_suffix'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'coupon_title_suffix', $post_coupon_title_suffix );
}
if ( isset( $_POST['sc_coupon_validity'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_coupon_validity', $post_sc_coupon_validity );
$coupon->update_meta_data( 'validity_suffix', $post_validity_suffix );
}
if ( isset( $_POST['sc_is_visible_storewide'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_is_visible_storewide', $post_sc_is_visible_storewide );
} else {
$coupon->update_meta_data( 'sc_is_visible_storewide', 'no' );
}
if ( isset( $_POST['sc_disable_email_restriction'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_disable_email_restriction', $post_sc_disable_email_restriction );
} else {
$coupon->update_meta_data( 'sc_disable_email_restriction', 'no' );
}
if ( isset( $_POST['is_pick_price_of_product'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'is_pick_price_of_product', $post_is_pick_price_of_product );
} else {
$coupon->update_meta_data( 'is_pick_price_of_product', 'no' );
}
if ( isset( $_POST['wc_sc_add_product_ids'] ) ) { // phpcs:ignore
if ( $this->is_wc_gte_30() ) {
$product_ids = $post_wc_sc_add_product_ids;
} else {
$product_ids = array_filter( array_map( 'trim', explode( ',', $post_wc_sc_add_product_ids ) ) );
}
$add_product_details = array();
if ( ! empty( $product_ids ) ) {
$quantity = $post_wc_sc_add_product_qty;
$discount_amount = $post_wc_sc_product_discount_amount;
$discount_type = $post_wc_sc_product_discount_type;
foreach ( $product_ids as $id ) {
$data = array();
$data['product_id'] = $id;
$data['quantity'] = $quantity;
$data['discount_amount'] = $discount_amount;
$data['discount_type'] = $discount_type;
$add_product_details[] = $data;
}
}
$coupon->update_meta_data( 'wc_sc_add_product_details', $add_product_details );
} else {
$coupon->update_meta_data( 'wc_sc_add_product_details', array() );
}
if ( isset( $_POST['wc_sc_max_discount'] ) ) { // phpcs:ignore
$max_discount = wc_clean( wp_unslash( $_POST['wc_sc_max_discount'] ) ); // phpcs:ignore
$coupon->update_meta_data( 'wc_sc_max_discount', $max_discount );
}
if ( isset( $_POST['wc_sc_expiry_time'] ) ) { // phpcs:ignore
$expiry_time = wc_clean( wp_unslash( $_POST['wc_sc_expiry_time'] ) ); // phpcs:ignore
$coupon->update_meta_data( 'wc_sc_expiry_time', $expiry_time );
}
if ( ! empty( $post_discount_type ) && 'smart_coupon' === $post_discount_type && ! empty( $post_original_post_status ) && 'auto-draft' === $post_original_post_status && ! empty( $post_coupon_amount ) ) {
$coupon->update_meta_data( 'wc_sc_original_amount', $post_coupon_amount );
}
} else {
if ( isset( $_POST['sc_restrict_to_new_user'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'sc_restrict_to_new_user', $post_sc_restrict_to_new_user );
} else {
update_post_meta( $post_id, 'sc_restrict_to_new_user', 'no' );
}
if ( isset( $_POST['auto_generate_coupon'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'auto_generate_coupon', $post_auto_generate_coupon );
} else {
if ( 'smart_coupon' === $type ) {
update_post_meta( $post_id, 'auto_generate_coupon', 'yes' );
} else {
update_post_meta( $post_id, 'auto_generate_coupon', 'no' );
}
}
if ( isset( $_POST['usage_limit_per_user'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'usage_limit_per_user', $post_usage_limit_per_user );
}
if ( isset( $_POST['limit_usage_to_x_items'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'limit_usage_to_x_items', $post_limit_usage_to_x_items );
}
if ( 'smart_coupon' === $type ) {
update_post_meta( $post_id, 'apply_before_tax', 'no' );
}
if ( isset( $_POST['coupon_title_prefix'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'coupon_title_prefix', $post_coupon_title_prefix );
}
if ( isset( $_POST['coupon_title_suffix'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'coupon_title_suffix', $post_coupon_title_suffix );
}
if ( isset( $_POST['sc_coupon_validity'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'sc_coupon_validity', $post_sc_coupon_validity );
update_post_meta( $post_id, 'validity_suffix', $post_validity_suffix );
}
if ( isset( $_POST['sc_is_visible_storewide'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'sc_is_visible_storewide', $post_sc_is_visible_storewide );
} else {
update_post_meta( $post_id, 'sc_is_visible_storewide', 'no' );
}
if ( isset( $_POST['sc_disable_email_restriction'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'sc_disable_email_restriction', $post_sc_disable_email_restriction );
} else {
update_post_meta( $post_id, 'sc_disable_email_restriction', 'no' );
}
if ( isset( $_POST['is_pick_price_of_product'] ) ) { // phpcs:ignore
update_post_meta( $post_id, 'is_pick_price_of_product', $post_is_pick_price_of_product );
} else {
update_post_meta( $post_id, 'is_pick_price_of_product', 'no' );
}
if ( isset( $_POST['wc_sc_add_product_ids'] ) ) { // phpcs:ignore
if ( $this->is_wc_gte_30() ) {
$product_ids = $post_wc_sc_add_product_ids;
} else {
$product_ids = array_filter( array_map( 'trim', explode( ',', $post_wc_sc_add_product_ids ) ) );
}
$add_product_details = array();
if ( ! empty( $product_ids ) ) {
$quantity = $post_wc_sc_add_product_qty;
$discount_amount = $post_wc_sc_product_discount_amount;
$discount_type = $post_wc_sc_product_discount_type;
foreach ( $product_ids as $id ) {
$data = array();
$data['product_id'] = $id;
$data['quantity'] = $quantity;
$data['discount_amount'] = $discount_amount;
$data['discount_type'] = $discount_type;
$add_product_details[] = $data;
}
}
update_post_meta( $post_id, 'wc_sc_add_product_details', $add_product_details );
} else {
update_post_meta( $post_id, 'wc_sc_add_product_details', array() );
}
if ( isset( $_POST['wc_sc_max_discount'] ) ) { // phpcs:ignore
$max_discount = wc_clean( wp_unslash( $_POST['wc_sc_max_discount'] ) ); // phpcs:ignore
update_post_meta( $post_id, 'wc_sc_max_discount', $max_discount );
}
if ( isset( $_POST['wc_sc_expiry_time'] ) ) { // phpcs:ignore
$expiry_time = wc_clean( wp_unslash( $_POST['wc_sc_expiry_time'] ) ); // phpcs:ignore
update_post_meta( $post_id, 'wc_sc_expiry_time', $expiry_time );
}
if ( ! empty( $post_discount_type ) && 'smart_coupon' === $post_discount_type && ! empty( $post_original_post_status ) && 'auto-draft' === $post_original_post_status && ! empty( $post_coupon_amount ) ) {
update_post_meta( $post_id, 'wc_sc_original_amount', $post_coupon_amount );
}
}
if ( $this->is_callable( $coupon, 'save' ) ) {
$coupon->save();
}
}
/**
* Function to process smart coupon meta for legacy api
*
* @param int $coupon_id The coupon id.
* @param array $data request body.
*/
public function woocommerce_legacy_api_process_smart_coupon_meta( $coupon_id = 0, $data = null ) {
if ( empty( $coupon_id ) ) {
return;
}
$coupon = new WC_Coupon( $coupon_id );
if ( ! $coupon instanceof WC_Coupon ) {
return;
}
if ( ! empty( $data ) && ! is_array( $data ) ) {
return;
}
$post_sc_restrict_to_new_user = ( isset( $data['sc_restrict_to_new_user'] ) ) ? wc_clean( wp_unslash( $data['sc_restrict_to_new_user'] ) ) : 'no'; // phpcs:ignore
$post_auto_generate_coupon = ( isset( $data['auto_generate_coupon'] ) ) ? wc_clean( wp_unslash( $data['auto_generate_coupon'] ) ) : 'no'; // phpcs:ignore
$post_usage_limit_per_user = ( isset( $data['usage_limit_per_user'] ) ) ? wc_clean( wp_unslash( $data['usage_limit_per_user'] ) ) : ''; // phpcs:ignore
$post_limit_usage_to_x_items = ( isset( $data['limit_usage_to_x_items'] ) ) ? wc_clean( wp_unslash( $data['limit_usage_to_x_items'] ) ) : ''; // phpcs:ignore
$post_coupon_title_prefix = ( isset( $data['coupon_title_prefix'] ) ) ? wc_clean( wp_unslash( $data['coupon_title_prefix'] ) ) : ''; // phpcs:ignore
$post_coupon_title_suffix = ( isset( $data['coupon_title_suffix'] ) ) ? wc_clean( wp_unslash( $data['coupon_title_suffix'] ) ) : ''; // phpcs:ignore
$post_sc_coupon_validity = ( isset( $data['sc_coupon_validity'] ) ) ? wc_clean( wp_unslash( $data['sc_coupon_validity'] ) ) : ''; // phpcs:ignore
$post_validity_suffix = ( isset( $data['validity_suffix'] ) ) ? wc_clean( wp_unslash( $data['validity_suffix'] ) ) : 'days'; // phpcs:ignore
$post_sc_is_visible_storewide = ( isset( $data['sc_is_visible_storewide'] ) ) ? wc_clean( wp_unslash( $data['sc_is_visible_storewide'] ) ) : 'no'; // phpcs:ignore
$post_sc_disable_email_restriction = ( isset( $data['sc_disable_email_restriction'] ) ) ? wc_clean( wp_unslash( $data['sc_disable_email_restriction'] ) ) : 'no'; // phpcs:ignore
$post_is_pick_price_of_product = ( isset( $data['is_pick_price_of_product'] ) ) ? wc_clean( wp_unslash( $data['is_pick_price_of_product'] ) ) : 'no'; // phpcs:ignore
$post_wc_sc_add_product_ids = ( isset( $data['wc_sc_add_product_ids'] ) ) ? wc_clean( wp_unslash( $data['wc_sc_add_product_ids'] ) ) : array(); // phpcs:ignore
$post_wc_sc_add_product_qty = ( isset( $data['wc_sc_add_product_qty'] ) ) ? wc_clean( wp_unslash( $data['wc_sc_add_product_qty'] ) ) : 1; // phpcs:ignore
$post_wc_sc_product_discount_amount = ( isset( $data['wc_sc_product_discount_amount'] ) ) ? wc_clean( wp_unslash( $data['wc_sc_product_discount_amount'] ) ) : ''; // phpcs:ignore
$post_wc_sc_product_discount_type = ( isset( $data['wc_sc_product_discount_type'] ) ) ? wc_clean( wp_unslash( $data['wc_sc_product_discount_type'] ) ) : 'percent'; // phpcs:ignore
$post_original_post_status = ( isset( $data['original_post_status'] ) ) ? wc_clean( wp_unslash( $data['original_post_status'] ) ) : ''; // phpcs:ignore
$post_coupon_amount = ( isset( $data['amount'] ) ) ? wc_clean( wp_unslash( $data['amount'] ) ) : 0;
$post_discount_type = ( $this->is_callable( $coupon, 'get_discount_type' ) ) ? $coupon->get_discount_type() : get_post_meta( $coupon_id, 'discount_type', true );
if ( true === $this->is_callable( $coupon, 'update_meta_data' ) ) {
if ( isset( $data['sc_restrict_to_new_user'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_restrict_to_new_user', $post_sc_restrict_to_new_user );
} else {
$coupon->update_meta_data( 'sc_restrict_to_new_user', 'no' );
}
if ( isset( $data['auto_generate_coupon'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'auto_generate_coupon', $post_auto_generate_coupon );
}
if ( isset( $data['usage_limit_per_user'] ) ) { // phpcs:ignore
$coupon->set_usage_limit_per_user( $post_usage_limit_per_user );
}
if ( isset( $data['limit_usage_to_x_items'] ) ) { // phpcs:ignore
$coupon->set_limit_usage_to_x_items( $post_limit_usage_to_x_items );
}
if ( 'smart_coupon' === $post_discount_type ) {
$coupon->update_meta_data( 'apply_before_tax', 'no' );
}
if ( isset( $data['coupon_title_prefix'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'coupon_title_prefix', $post_coupon_title_prefix );
}
if ( isset( $data['coupon_title_suffix'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'coupon_title_suffix', $post_coupon_title_suffix );
}
if ( isset( $data['sc_coupon_validity'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_coupon_validity', $post_sc_coupon_validity );
$coupon->update_meta_data( 'validity_suffix', $post_validity_suffix );
}
if ( isset( $data['sc_is_visible_storewide'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_is_visible_storewide', $post_sc_is_visible_storewide );
} else {
$coupon->update_meta_data( 'sc_is_visible_storewide', 'no' );
}
if ( isset( $data['sc_disable_email_restriction'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'sc_disable_email_restriction', $post_sc_disable_email_restriction );
} else {
$coupon->update_meta_data( 'sc_disable_email_restriction', 'no' );
}
if ( isset( $data['is_pick_price_of_product'] ) ) { // phpcs:ignore
$coupon->update_meta_data( 'is_pick_price_of_product', $post_is_pick_price_of_product );
} else {
$coupon->update_meta_data( 'is_pick_price_of_product', 'no' );
}
if ( isset( $data['wc_sc_add_product_ids'] ) ) { // phpcs:ignore
if ( $this->is_wc_gte_30() ) {
$product_ids = $post_wc_sc_add_product_ids;
} else {
$product_ids = array_filter( array_map( 'trim', explode( ',', $post_wc_sc_add_product_ids ) ) );
}
$add_product_details = array();
if ( ! empty( $product_ids ) ) {
$quantity = $post_wc_sc_add_product_qty;
$discount_amount = $post_wc_sc_product_discount_amount;
$discount_type = $post_wc_sc_product_discount_type;
foreach ( $product_ids as $id ) {
$data = array();
$data['product_id'] = $id;
$data['quantity'] = $quantity;
$data['discount_amount'] = $discount_amount;
$data['discount_type'] = $discount_type;
$add_product_details[] = $data;
}
}
$coupon->update_meta_data( 'wc_sc_add_product_details', $add_product_details );
} else {
$coupon->update_meta_data( 'wc_sc_add_product_details', array() );
}
if ( isset( $data['wc_sc_max_discount'] ) ) { // phpcs:ignore
$max_discount = wc_clean( wp_unslash( $data['wc_sc_max_discount'] ) ); // phpcs:ignore
$coupon->update_meta_data( 'wc_sc_max_discount', $max_discount );
}
if ( isset( $data['wc_sc_expiry_time'] ) ) { // phpcs:ignore
$expiry_time = wc_clean( wp_unslash( $data['wc_sc_expiry_time'] ) ); // phpcs:ignore
$coupon->update_meta_data( 'wc_sc_expiry_time', $expiry_time );
}
if ( ! empty( $post_discount_type ) && 'smart_coupon' === $post_discount_type && ! empty( $post_coupon_amount ) ) {
$coupon->update_meta_data( 'wc_sc_original_amount', $post_coupon_amount );
}
if ( $this->is_callable( $coupon, 'save' ) ) {
$coupon->save();
}
}
}
/**
* Add a tab in coupon data metabox
*
* @param array $tabs Existing tabs.
* @return array $tabs With additional tab
*/
public function smart_coupons_data_tabs( $tabs = array() ) {
$tabs['wc_sc_actions'] = array(
'label' => __( 'Actions', 'woocommerce-smart-coupons' ),
'target' => 'wc_smart_coupons_actions',
'class' => '',
);
return $tabs;
}
/**
* Panel for Smart Coupons additional data fields
*
* @param integer $coupon_id The coupon id.
* @param WC_Coupon $coupon The coupon object.
*/
public function smart_coupons_data_panels( $coupon_id = 0, $coupon = null ) {
$add_product_details = ( ! empty( $coupon_id ) ) ? $this->get_post_meta( $coupon_id, 'wc_sc_add_product_details', true ) : array();
$add_product_qty = ( isset( $add_product_details[0]['quantity'] ) ) ? $add_product_details[0]['quantity'] : 1;
$discount_amount = ( isset( $add_product_details[0]['discount_amount'] ) && '' !== $add_product_details[0]['discount_amount'] ) ? $add_product_details[0]['discount_amount'] : '';
$discount_type = ( ! empty( $add_product_details[0]['discount_type'] ) ) ? $add_product_details[0]['discount_type'] : 'percent';
$is_js_started = did_action( 'wc_sc_enhanced_select_script_start' );
if ( 0 === $is_js_started ) {
do_action( 'wc_sc_enhanced_select_script_start' );
}
?>
plugin_url() ) . '/assets/';
// Register scripts.
if ( ! wp_script_is( 'woocommerce_admin', 'registered' ) ) {
wp_register_script( 'woocommerce_admin', WC()->plugin_url() . '/assets/js/admin/woocommerce_admin' . $suffix . '.js', array( 'jquery', 'jquery-blockui', 'jquery-ui-sortable', 'jquery-ui-widget', 'jquery-ui-core', 'jquery-tiptip' ), WC()->version, false );
}
if ( $this->is_wc_gte_32() ) {
wp_register_script( 'wc-sc-select2', WC()->plugin_url() . '/assets/js/selectWoo/selectWoo.full' . $suffix . '.js', array( 'jquery' ), WC()->version, false );
} else {
wp_register_script( 'wc-sc-select2', WC()->plugin_url() . '/assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WC()->version, false );
}
if ( ! wp_script_is( 'wc-enhanced-select', 'registered' ) ) {
wp_register_script( 'wc-enhanced-select', WC()->plugin_url() . '/assets/js/admin/wc-enhanced-select' . $suffix . '.js', array( 'jquery', 'wc-sc-select2' ), WC()->version, false );
}
$wc_sc_select_params = array(
'i18n_matches_1' => _x( 'One result is available, press enter to select it.', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_matches_n' => _x( '%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_no_matches' => _x( 'No matches found', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_ajax_error' => _x( 'Searching…', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_input_too_short_1' => _x( 'Please enter 1 or more characters', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_input_too_short_n' => _x( 'Please enter %qty% or more characters', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_input_too_long_1' => _x( 'Please delete 1 character', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_input_too_long_n' => _x( 'Please delete %qty% characters', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_selection_too_long_1' => _x( 'You can only select 1 item', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_selection_too_long_n' => _x( 'You can only select %qty% items', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_load_more' => _x( 'Loading more results…', 'enhanced select', 'woocommerce-smart-coupons' ),
'i18n_searching' => _x( 'Searching…', 'enhanced select', 'woocommerce-smart-coupons' ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'search_products_nonce' => wp_create_nonce( 'search-products' ),
'search_customers_nonce' => wp_create_nonce( 'search-customers' ),
);
$params = array(
'strings' => array(
'import_products' => '',
'export_products' => '',
),
'urls' => array(
'import_products' => '',
'export_products' => '',
),
);
wp_localize_script( 'woocommerce_admin', 'woocommerce_admin', $params );
wp_localize_script( 'wc-sc-select2', 'wc_enhanced_select_params', $wc_sc_select_params );
wp_enqueue_script( 'woocommerce_admin' );
wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', array(), WC()->version );
wp_enqueue_script( 'wc-sc-select2' );
wp_enqueue_script( 'wc-enhanced-select' );
wp_enqueue_style( 'wc-sc-select2', $assets_path . 'css/select2.css', array(), WC()->version );
}
/**
* Enhanced select script end
*/
public function enhanced_select_script_end() {
?>
'false',
'message' => __( 'Could not locate WooCommerce', 'woocommerce-smart-coupons' ),
)
);
}
include_once dirname( WC_PLUGIN_FILE ) . '/includes/class-wc-ajax.php';
}
$term = (string) urldecode( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); // phpcs:ignore
WC_AJAX::json_search_products( $term, true );
}
/**
* Remove variation parent from search result
*
* @param array $products Array of product ids with product name.
* @return array $products Array of product ids with product name after removing variation parent
*/
public function exclude_variation_parent( $products = null ) {
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore
if ( 'wc_sc_json_search_products_and_variations' === $backtrace[5]['function'] ) {
if ( ! empty( $products ) ) {
$product_ids = array_keys( $products );
$parent_ids = array_map( 'wp_get_post_parent_id', $product_ids );
$parent_ids = array_filter( array_unique( $parent_ids ) );
foreach ( $parent_ids as $parent ) {
unset( $products[ $parent ] );
}
}
}
return $products;
}
/**
* Function to add new discount type 'smart_coupon'
*
* @param array $discount_types Existing discount types.
* @return array $discount_types Including smart coupon discount type.
*/
public function add_smart_coupon_discount_type( $discount_types ) {
global $store_credit_label;
$discount_types['smart_coupon'] = ! empty( $store_credit_label['singular'] ) ? ucfirst( $store_credit_label['singular'] ) : __( 'Store Credit / Gift Certificate', 'woocommerce-smart-coupons' );
return $discount_types;
}
/**
* Function to enqueue required styles and scripts for Smart Coupons' custom fields
*/
public function enqueue_styles_scripts() {
global $post;
if ( ! empty( $post->post_type ) && 'shop_coupon' === $post->post_type && ! empty( $post->ID ) ) {
$coupon = ( ! empty( $post->ID ) ) ? new WC_Coupon( $post->ID ) : null;
$is_callable_coupon_get_meta = $this->is_callable( $coupon, 'get_meta' );
// Compatibility between expiry_date & date_expires meta field.
if ( $this->is_wc_gte_30() ) {
$date_expires = ( true === $is_callable_coupon_get_meta ) ? $coupon->get_date_expires() : intval( get_post_meta( $post->ID, 'date_expires', true ) );
$expiry_date = get_post_meta( $post->ID, 'expiry_date', true );
if ( is_object( $date_expires ) && is_a( $date_expires, 'WC_DateTime' ) && $this->is_callable( $date_expires, 'getTimestamp' ) ) {
$date_expires = $date_expires->getTimestamp();
}
if ( ! empty( $expiry_date ) && empty( $date_expires ) ) {
$date_expires = $this->wc_string_to_datetime_to_timestamp( $expiry_date );
$date_expires = $this->get_date_expires_value( $date_expires );
$this->update_post_meta( $post->ID, 'date_expires', $date_expires );
delete_post_meta( $post->ID, 'expiry_date' ); // No need to run CRUD function as this meta_key no longer exists.
$js = "jQuery('input#expiry_date').val('" . $expiry_date . "');";
wc_enqueue_js( $js );
}
}
if ( is_callable( 'WC_Smart_Coupons::get_smart_coupons_plugin_data' ) ) {
$plugin_data = WC_Smart_Coupons::get_smart_coupons_plugin_data();
$version = $plugin_data['Version'];
} else {
$version = '';
}
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( ! wp_style_is( 'jquery-ui-style', 'registered' ) ) {
wp_register_style( 'jquery-ui-style', WC()->plugin_url() . '/assets/css/jquery-ui/jquery-ui' . $suffix . '.css', array(), WC()->version );
}
if ( ! wp_style_is( 'jquery-ui-timepicker', 'registered' ) ) {
wp_register_style( 'jquery-ui-timepicker', untrailingslashit( plugins_url( '/', WC_SC_PLUGIN_FILE ) ) . '/assets/css/jquery-ui-timepicker-addon' . $suffix . '.css', array( 'jquery-ui-style' ), $version );
}
if ( ! wp_style_is( 'jquery-ui-timepicker' ) ) {
wp_enqueue_style( 'jquery-ui-timepicker' );
}
if ( ! wp_script_is( 'jquery-ui-timepicker', 'registered' ) ) {
wp_register_script( 'jquery-ui-timepicker', untrailingslashit( plugins_url( '/', WC_SC_PLUGIN_FILE ) ) . '/assets/js/jquery-ui-timepicker-addon' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-slider', 'wp-i18n' ), $version, true );
}
if ( ! wp_script_is( 'jquery-ui-timepicker' ) ) {
wp_enqueue_script( 'jquery-ui-timepicker' );
}
$screen = get_current_screen();
if ( 'shop_coupon' === $screen->id ) {
?>