get_post_meta( $coupon_id, 'wc_sc_payment_method_ids', true ); if ( empty( $payment_method_ids ) || ! is_array( $payment_method_ids ) ) { $payment_method_ids = array(); } } $available_payment_methods = WC()->payment_gateways->get_available_payment_gateways(); ?>

is_callable( $coupon, 'update_meta_data' ) && $this->is_callable( $coupon, 'save' ) ) { $coupon->update_meta_data( 'wc_sc_payment_method_ids', $payment_method_ids ); $coupon->save(); } else { $this->update_post_meta( $post_id, 'wc_sc_payment_method_ids', $payment_method_ids ); } } /** * Validate the coupon based on payment method * * @param boolean $valid Is valid or not. * @param WC_Coupon $coupon The coupon object. * @param WC_Discounts $discounts The discount object. * * @throws Exception If the coupon is invalid. * @return boolean Is valid or not */ public function validate( $valid = false, $coupon = object, $discounts = object ) { // If coupon is already invalid, no need for further checks. if ( false === $valid ) { return $valid; } if ( ! $coupon instanceof WC_Coupon ) { return $valid; } if ( ! $discounts instanceof WC_Discounts ) { return $valid; } if ( $this->is_wc_gte_30() ) { $coupon_id = ( ! empty( $coupon ) && is_callable( array( $coupon, 'get_id' ) ) ) ? $coupon->get_id() : 0; $coupon_code = ( ! empty( $coupon ) && is_callable( array( $coupon, 'get_code' ) ) ) ? $coupon->get_code() : ''; } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; $coupon_code = ( ! empty( $coupon->code ) ) ? $coupon->code : ''; } if ( empty( $coupon_id ) ) { return $valid; } $payment_method_ids = $this->get_post_meta( $coupon_id, 'wc_sc_payment_method_ids', true ); $cart_or_order_object = is_callable( array( $discounts, 'get_object' ) ) ? $discounts->get_object() : null; $is_wc_session = is_a( $cart_or_order_object, 'WC_Cart' ) && function_exists( 'WC' ) && isset( WC()->session ) && is_object( WC()->session ); $needs_payment = ( $this->is_callable( $cart_or_order_object, 'needs_payment' ) ) ? $cart_or_order_object->needs_payment() : true; $posted_data = array(); $post_data = isset( $_POST['post_data'] ) && ! empty( $_POST['post_data'] ) ? wc_clean( wp_unslash( $_POST['post_data'] ) ) : ''; // phpcs:ignore wp_parse_str( $post_data, $posted_data ); if ( true === $needs_payment && is_array( $payment_method_ids ) && ! empty( $payment_method_ids ) ) { $payment_titles = $this->get_payment_method_titles_by_ids( $payment_method_ids ); $chosen_payment_method = ''; if ( is_a( $cart_or_order_object, 'WC_Order' ) ) { $chosen_payment_method = is_callable( array( $cart_or_order_object, 'get_payment_method' ) ) ? $cart_or_order_object->get_payment_method() : ''; } elseif ( ! empty( $posted_data ) && isset( $posted_data['payment_method'] ) ) { $chosen_payment_method = $posted_data['payment_method']; } elseif ( true === $is_wc_session ) { $chosen_payment_method = ( WC()->session->__isset( 'chosen_payment_method' ) ) ? WC()->session->get( 'chosen_payment_method' ) : ''; } if ( ! in_array( $chosen_payment_method, $payment_method_ids, true ) ) { if ( true === $is_wc_session && is_callable( array( WC()->session, 'set' ) ) ) { WC()->session->set( 'wc_sc_reload_payment_method', 'yes' ); } $applied_coupons = ( WC()->cart instanceof WC_Cart && is_callable( array( WC()->cart, 'get_applied_coupons' ) ) ) ? WC()->cart->get_applied_coupons() : array(); if ( ! empty( $applied_coupons ) && in_array( $coupon_code, $applied_coupons, true ) ) { WC()->cart->remove_coupon( $coupon_code ); /* translators: 1. The coupon code 2. The text 'payment method/s' 3. List of payment method names 4. Link to the checkout page */ wc_add_notice( sprintf( __( 'Coupon code %1$s has been removed. It is valid only for %2$s: %3$s. You can change the payment method from the %4$s page.', 'woocommerce-smart-coupons' ), '' . $coupon_code . '', _n( 'payment method', 'payment methods', count( $payment_titles ), 'woocommerce-smart-coupons' ), '"' . implode( '", "', $payment_titles ) . '"', '' . __( 'Checkout', 'woocommerce-smart-coupons' ) . '' ), 'error' ); } /* translators: 1. The coupon code 2. The text 'payment method/s' 3. List of payment method names 4. Link to the checkout page */ throw new Exception( sprintf( __( 'Coupon code %1$s is valid only for %2$s: %3$s. You can change payment method from the %4$s page.', 'woocommerce-smart-coupons' ), '' . $coupon_code . '', _n( 'payment method', 'payment methods', count( $payment_titles ), 'woocommerce-smart-coupons' ), '"' . implode( '", "', $payment_titles ) . '"', '' . __( 'Checkout', 'woocommerce-smart-coupons' ) . '' ) ); } if ( true === $is_wc_session && is_callable( array( WC()->session, 'set' ) ) ) { WC()->session->set( 'wc_sc_reload_payment_method', 'no' ); } } return $valid; } /** * Add meta in export headers * * @param array $headers Existing headers. * @return array */ public function export_headers( $headers = array() ) { $headers['wc_sc_payment_method_ids'] = __( 'Payment methods', '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_payment_method_ids' === $args['meta_key'] ) { if ( isset( $args['meta_value'] ) && ! empty( $args['meta_value'] ) ) { $payment_method_ids = maybe_unserialize( stripslashes( $args['meta_value'] ) ); if ( is_array( $payment_method_ids ) && ! empty( $payment_method_ids ) ) { $payment_method_titles = $this->get_payment_method_titles_by_ids( $payment_method_ids ); if ( is_array( $payment_method_titles ) && ! empty( $payment_method_titles ) ) { $meta_value = implode( '|', wc_clean( wp_unslash( $payment_method_titles ) ) ); // Replace payment method ids with their respective method titles. } } } } return $meta_value; } /** * Post meta defaults for payment method ids meta * * @param array $defaults Existing postmeta defaults. * @return array $defaults Modified postmeta defaults */ public function postmeta_defaults( $defaults = array() ) { $defaults['wc_sc_payment_method_ids'] = ''; return $defaults; } /** * Add payment method's meta with value in coupon meta * * @param array $data The row data. * @param array $post The POST values. * @return array $data Modified row data */ public function generate_coupon_meta( $data = array(), $post = array() ) { $payment_method_titles = ''; if ( ! empty( $post['wc_sc_payment_method_ids'] ) && is_array( $post['wc_sc_payment_method_ids'] ) ) { $payment_method_titles = $this->get_payment_method_titles_by_ids( $post['wc_sc_payment_method_ids'] ); if ( is_array( $payment_method_titles ) && ! empty( $payment_method_titles ) ) { $payment_method_titles = implode( '|', wc_clean( wp_unslash( $payment_method_titles ) ) ); } } $data['wc_sc_payment_method_ids'] = $payment_method_titles; // Replace payment method ids with their respective method titles. return $data; } /** * Function to get payment method titles for given payment method ids * * @param array $payment_method_ids ids of payment methods. * @return array $payment_method_titles titles of payment methods */ public function get_payment_method_titles_by_ids( $payment_method_ids = array() ) { $payment_method_titles = array(); if ( is_array( $payment_method_ids ) && ! empty( $payment_method_ids ) ) { $available_payment_methods = WC()->payment_gateways->get_available_payment_gateways(); foreach ( $payment_method_ids as $index => $payment_method_id ) { $payment_method = ( isset( $available_payment_methods[ $payment_method_id ] ) && ! empty( $available_payment_methods[ $payment_method_id ] ) ) ? $available_payment_methods[ $payment_method_id ] : ''; if ( ! empty( $payment_method ) && is_a( $payment_method, 'WC_Payment_Gateway' ) ) { $payment_method_title = is_callable( array( $payment_method, 'get_title' ) ) ? $payment_method->get_title() : ''; if ( ! empty( $payment_method_title ) ) { $payment_method_titles[ $index ] = $payment_method_title; // Replace payment method id with it's repective title. } else { $payment_method_titles[ $index ] = $payment_method->id; // In case of empty payment method title replace it with method id. } } } } return $payment_method_titles; } /** * 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_payment_method_ids' === $args['meta_key'] ) { $meta_value = ( ! empty( $args['postmeta']['wc_sc_payment_method_ids'] ) ) ? explode( '|', wc_clean( wp_unslash( $args['postmeta']['wc_sc_payment_method_ids'] ) ) ) : array(); if ( is_array( $meta_value ) && ! empty( $meta_value ) ) { $available_payment_methods = WC()->payment_gateways->get_available_payment_gateways(); if ( is_array( $available_payment_methods ) && ! empty( $available_payment_methods ) ) { foreach ( $meta_value as $index => $payment_method_title ) { foreach ( $available_payment_methods as $payment_method ) { $method_title = is_callable( array( $payment_method, 'get_title' ) ) ? $payment_method->get_title() : ''; if ( $method_title === $payment_method_title && ! empty( $payment_method->id ) ) { $meta_value[ $index ] = $payment_method->id; // Replace payment method title with it's respective id. } } } } } } return $meta_value; } /** * Function to copy payment method restriction meta in newly generated coupon * * @param array $args The arguments. */ public function copy_coupon_payment_method_meta( $args = array() ) { // Copy meta data to new coupon. $this->copy_coupon_meta_data( $args, array( 'wc_sc_payment_method_ids' ) ); } /** * Make meta data of payment method 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_payment_method_ids' === $meta_key ) { return true; } return $protected; } /** * Function to add styles & scripts */ public function styles_and_scripts() { if ( is_woocommerce() || is_cart() || is_checkout() || is_product_category() || is_product_tag() || is_account_page() ) { $js = " try { let checkoutForm = document.querySelector('form.checkout'); if( checkoutForm ){ checkoutForm.addEventListener('change', function(event) { if (event.target && event.target.name === 'payment_method') { setTimeout(function() { document.body.dispatchEvent(new Event('update_checkout')); }, 1000); } }); } } catch( error ) { console.error( '" . __( 'An error occurred:', 'woocommerce-smart-coupons' ) . "', error); }"; wc_enqueue_js( $js ); } } } } WC_SC_Coupons_By_Payment_Method::get_instance();