| |
| <?php
|
| if ( ! defined( 'ABSPATH' ) ) {
|
| exit;
|
| }
|
|
|
| if ( class_exists( 'WooCommerce' ) ) {
|
|
|
| add_action( 'woocommerce_coupon_options', 'rd_wc_add_hide_coupon_code_field', 10, 2 );
|
| function rd_wc_add_hide_coupon_code_field( $coupon_id, $coupon ) {
|
|
|
| $current = get_post_meta( $coupon_id, '_rd_hide_coupon_code_public', true );
|
|
|
| $description = 'If ticked, the coupon code will be removed from customer-facing WooCommerce notices and the cart/checkout totals label.';
|
|
|
| if ( class_exists( 'WC_Smart_Coupons' ) ) {
|
| $description .= ' This coupon will also be hidden from all Smart Coupons customer-facing coupon displays (including My Account, cart, checkout, and available-coupons lists).';
|
| }
|
|
|
| $description .= '<br><em>This functionality is added by a custom Rosso Digital code snippet.</em>';
|
|
|
| woocommerce_wp_checkbox(
|
| array(
|
| 'id' => '_rd_hide_coupon_code_public',
|
| 'label' => __( 'Hide coupon code', 'woocommerce' ),
|
| 'description' => __( $description, 'woocommerce' ),
|
| 'value' => ( 'yes' === $current ) ? 'yes' : 'no',
|
| 'cbvalue' => 'yes',
|
| )
|
| );
|
| }
|
|
|
| add_action( 'admin_head', 'rd_wc_style_hide_coupon_code_field' );
|
| function rd_wc_style_hide_coupon_code_field() {
|
|
|
| if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
|
| return;
|
| }
|
|
|
| $screen = get_current_screen();
|
| if ( ! $screen || 'shop_coupon' !== $screen->post_type || 'post' !== $screen->base ) {
|
| return;
|
| }
|
|
|
| echo '<style>
|
| #woocommerce-coupon-data p.form-field._rd_hide_coupon_code_public_field{
|
| background:#FBE9EA;
|
| border-top:1px solid #eee;
|
| margin:0;
|
| padding-top:12px;
|
| padding-bottom:12px;
|
| }
|
| </style>';
|
| }
|
|
|
| add_action( 'woocommerce_coupon_options_save', 'rd_wc_save_hide_coupon_code_field', 10, 2 );
|
| function rd_wc_save_hide_coupon_code_field( $post_id, $coupon ) {
|
|
|
| $posted = '';
|
| if ( isset( $_POST['_rd_hide_coupon_code_public'] ) ) {
|
| $posted = wc_clean( wp_unslash( $_POST['_rd_hide_coupon_code_public'] ) );
|
| }
|
|
|
| $value = ( 'yes' === $posted || '1' === $posted ) ? 'yes' : 'no';
|
| update_post_meta( $post_id, '_rd_hide_coupon_code_public', $value );
|
| }
|
|
|
| function rd_wc_get_coupon_id_from_code( $coupon_code ) {
|
|
|
| if ( ! is_string( $coupon_code ) || $coupon_code === '' ) {
|
| return 0;
|
| }
|
|
|
| if ( function_exists( 'wc_format_coupon_code' ) ) {
|
| $coupon_code = wc_format_coupon_code( $coupon_code );
|
| } else {
|
| $coupon_code = strtolower( trim( $coupon_code ) );
|
| }
|
|
|
| try {
|
| $coupon = new WC_Coupon( $coupon_code );
|
| return (int) $coupon->get_id();
|
| } catch ( Exception $e ) {
|
| return 0;
|
| }
|
| }
|
|
|
| function rd_wc_get_hide_meta_value( $coupon_id ) {
|
|
|
| if ( ! $coupon_id ) {
|
| return '';
|
| }
|
|
|
| $val = get_post_meta( $coupon_id, '_rd_hide_coupon_code_public', true );
|
| return is_string( $val ) ? $val : '';
|
| }
|
|
|
| function rd_wc_get_url_coupon_codes() {
|
|
|
| $raw = '';
|
|
|
| if ( isset( $_REQUEST['coupon-code'] ) && is_string( $_REQUEST['coupon-code'] ) ) {
|
| $raw = wp_unslash( $_REQUEST['coupon-code'] );
|
| } elseif ( function_exists( 'get_query_var' ) ) {
|
| $qv = get_query_var( 'coupon-code' );
|
| if ( is_string( $qv ) && $qv !== '' ) {
|
| $raw = $qv;
|
| }
|
| }
|
|
|
| if ( $raw === '' && isset( $_SERVER['REQUEST_URI'] ) && is_string( $_SERVER['REQUEST_URI'] ) ) {
|
| $path = wp_parse_url( 'http://example.com' . wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH );
|
| $path = is_string( $path ) ? trim( $path, '/' ) : '';
|
| $segments = ( $path !== '' ) ? explode( '/', $path ) : array();
|
| $idx = array_search( 'coupon-code', $segments, true );
|
| if ( $idx !== false && isset( $segments[ $idx + 1 ] ) && is_string( $segments[ $idx + 1 ] ) ) {
|
| $raw = $segments[ $idx + 1 ];
|
| }
|
| }
|
|
|
| if ( ! is_string( $raw ) || $raw === '' ) {
|
| return array();
|
| }
|
|
|
| $raw = urldecode( $raw );
|
|
|
| $codes = array();
|
| foreach ( explode( ',', $raw ) as $code ) {
|
| if ( ! is_string( $code ) ) {
|
| continue;
|
| }
|
| $code = trim( $code );
|
| if ( $code === '' ) {
|
| continue;
|
| }
|
| if ( function_exists( 'wc_format_coupon_code' ) ) {
|
| $code = wc_format_coupon_code( $code );
|
| } else {
|
| $code = strtolower( $code );
|
| }
|
| if ( $code !== '' ) {
|
| $codes[] = $code;
|
| }
|
| }
|
|
|
| return array_values( array_unique( $codes ) );
|
| }
|
|
|
| add_filter( 'woocommerce_cart_totals_coupon_label', 'rd_wc_hide_coupon_code_in_totals_label', 99, 2 );
|
| function rd_wc_hide_coupon_code_in_totals_label( $label, $coupon ) {
|
|
|
| $code = '';
|
|
|
| if ( is_object( $coupon ) && method_exists( $coupon, 'get_code' ) ) {
|
| $code = (string) $coupon->get_code();
|
| } elseif ( is_string( $coupon ) ) {
|
| $code = $coupon;
|
| }
|
|
|
| if ( $code === '' ) {
|
| return $label;
|
| }
|
|
|
| $coupon_id = rd_wc_get_coupon_id_from_code( $code );
|
| if ( ! $coupon_id || 'yes' !== rd_wc_get_hide_meta_value( $coupon_id ) ) {
|
| return $label;
|
| }
|
|
|
| return __( 'Coupon', 'woocommerce' );
|
| }
|
|
|
| function rd_wc_remove_code_token_from_message( $message, $code ) {
|
|
|
| if ( ! is_string( $message ) || ! is_string( $code ) || $message === '' || $code === '' ) {
|
| return $message;
|
| }
|
|
|
| $message = str_ireplace( $code, '', $message );
|
|
|
| $message = str_replace(
|
| array( '""', "''", '“”', '‘’', '""' ),
|
| '',
|
| $message
|
| );
|
|
|
| $message = preg_replace( '/\s{2,}/', ' ', $message );
|
| $message = preg_replace( '/\(\s*\)/', '', $message );
|
| $message = str_replace( ' :', ':', $message );
|
|
|
| return trim( $message );
|
| }
|
|
|
| add_filter( 'woocommerce_coupon_message', 'rd_wc_hide_coupon_code_in_coupon_message', 99, 3 );
|
| function rd_wc_hide_coupon_code_in_coupon_message( $message, $message_code, $coupon ) {
|
|
|
| if ( is_admin() ) {
|
| return $message;
|
| }
|
|
|
| if ( ! is_object( $coupon ) || ! method_exists( $coupon, 'get_code' ) ) {
|
| return $message;
|
| }
|
|
|
| $code = (string) $coupon->get_code();
|
| $coupon_id = (int) $coupon->get_id();
|
|
|
| if ( ! $coupon_id || 'yes' !== rd_wc_get_hide_meta_value( $coupon_id ) ) {
|
| return $message;
|
| }
|
|
|
| return rd_wc_remove_code_token_from_message( $message, $code );
|
| }
|
|
|
| add_filter( 'woocommerce_add_success', 'rd_wc_strip_hidden_coupon_codes_from_notice', 99, 1 );
|
| add_filter( 'woocommerce_add_error', 'rd_wc_strip_hidden_coupon_codes_from_notice', 99, 1 );
|
| add_filter( 'woocommerce_add_notice', 'rd_wc_strip_hidden_coupon_codes_from_notice', 99, 1 );
|
| function rd_wc_strip_hidden_coupon_codes_from_notice( $message ) {
|
|
|
| if ( is_admin() || ! is_string( $message ) || $message === '' ) {
|
| return $message;
|
| }
|
|
|
| $codes = rd_wc_get_url_coupon_codes();
|
| if ( empty( $codes ) ) {
|
| return $message;
|
| }
|
|
|
| foreach ( $codes as $code ) {
|
| $coupon_id = rd_wc_get_coupon_id_from_code( $code );
|
| if ( $coupon_id && 'yes' === rd_wc_get_hide_meta_value( $coupon_id ) ) {
|
| $message = rd_wc_remove_code_token_from_message( $message, $code );
|
| }
|
| }
|
|
|
| return $message;
|
| }
|
|
|
| add_action( 'plugins_loaded', 'rd_wc_register_smart_coupons_hide_filter', 20 );
|
| function rd_wc_register_smart_coupons_hide_filter() {
|
| if ( ! class_exists( 'WC_Smart_Coupons' ) ) {
|
| return;
|
| }
|
| add_filter( 'wc_sc_display_available_coupons', 'rd_wc_sc_hide_hidden_coupons_from_available_lists', 0, 2 );
|
| }
|
|
|
| function rd_wc_sc_hide_hidden_coupons_from_available_lists( $coupons, $context ) {
|
|
|
| if ( ! is_array( $coupons ) || empty( $coupons ) ) {
|
| return $coupons;
|
| }
|
|
|
| foreach ( $coupons as $key => $item ) {
|
|
|
| $coupon_id = 0;
|
|
|
| if ( is_object( $item ) && isset( $item->id ) ) {
|
| $coupon_id = (int) $item->id;
|
| } elseif ( is_array( $item ) && isset( $item['id'] ) ) {
|
| $coupon_id = (int) $item['id'];
|
| } elseif ( is_numeric( $item ) ) {
|
| $coupon_id = (int) $item;
|
| } elseif ( is_object( $item ) && method_exists( $item, 'get_id' ) ) {
|
| $coupon_id = (int) $item->get_id();
|
| }
|
|
|
| if ( ! $coupon_id ) {
|
| continue;
|
| }
|
|
|
| $meta = get_post_meta( $coupon_id, '_rd_hide_coupon_code_public', true );
|
| if ( is_string( $meta ) && 'yes' === $meta ) {
|
| unset( $coupons[ $key ] );
|
| }
|
| }
|
|
|
| return array_values( $coupons );
|
| }
|
| }
|
| |
| |
Comments