is_wc_gte_30() ) { if ( ! is_object( $product ) || ! is_callable( array( $product, 'get_id' ) ) ) { return; } $product_id = $product->get_id(); if ( empty( $product_id ) ) { return; } } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; } $coupons = $this->get_coupon_titles( array( 'product_object' => $product ) ); if ( ! function_exists( 'is_plugin_active' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $product_price = $product->get_price(); $_nyp = ( $this->is_callable( $product, 'get_meta' ) ) ? $product->get_meta( '_nyp' ) : get_post_meta( $product_id, '_nyp', true ); // MADE CHANGES IN THE CONDITION TO SHOW INPUT FIELD FOR PRICE ONLY FOR COUPON AS A PRODUCT. if ( ! empty( $coupons ) && $this->is_coupon_amount_pick_from_product_price( $coupons ) && ( ! ( ! empty( $product_price ) || ( is_plugin_active( 'woocommerce-name-your-price/woocommerce-name-your-price.php' ) && ( 'yes' === $_nyp ) ) ) ) ) { $js = " const minCreditAmount = parseFloat( jQuery('input#credit_called').attr('min') ); const maxCreditAmount = parseFloat( jQuery('input#credit_called').attr('max') ); var validateCreditCalled = function(){ var enteredCreditAmount = parseFloat( jQuery('input#credit_called').val() ); if ( isNaN(enteredCreditAmount) || enteredCreditAmount < minCreditAmount || ( maxCreditAmount > 0 && enteredCreditAmount > maxCreditAmount ) ) { var creditErrorMsg = '" . __( 'Invalid amount.', 'woocommerce-smart-coupons' ) . "'; if ( isNaN(enteredCreditAmount) ) { creditErrorMsg += ' " . __( 'Enter a numeric value.', 'woocommerce-smart-coupons' ) . "'; } if ( enteredCreditAmount < minCreditAmount ) { creditErrorMsg += ' " . __( 'The value should not be less than', 'woocommerce-smart-coupons' ) . " ' + minCreditAmount; } else if ( enteredCreditAmount > maxCreditAmount ) { creditErrorMsg += ' " . __( 'The value should not be greater than', 'woocommerce-smart-coupons' ) . " ' + maxCreditAmount; } jQuery('#error_message').text(creditErrorMsg); jQuery('input#credit_called').css('border-color', 'red'); return false; } else { jQuery('form.cart').unbind('submit'); jQuery('#error_message').text(''); jQuery('input#credit_called').css('border-color', ''); return true; } }; jQuery('input#credit_called').bind('change keyup', function(){ validateCreditCalled(); jQuery('input#hidden_credit').remove(); if ( jQuery('input[name=quantity]').length ) { jQuery('input[name=quantity]').append(''); } else { jQuery('input[name=\"add-to-cart\"]').after(''); } }); jQuery('button.single_add_to_cart_button').on('click', function(e) { if ( validateCreditCalled() == false ) { e.preventDefault(); } }); jQuery('input#credit_called').on( 'keypress', function (e) { if (e.which == 13) { jQuery('form.cart').find('button[name=\"add-to-cart\"]').trigger('click'); } }); // To handle, if the call for credit form is included twice. jQuery.each( jQuery('body').find('div#call_for_credit'), function(){ let current_element = jQuery(this); let is_visible = current_element.is(':visible'); if ( false === is_visible ) { current_element.remove(); } }); "; // To handle, if the call for credit form is included twice. if ( did_action( 'wc_sc_call_for_credit_script_enqueued' ) <= 0 ) { wc_enqueue_js( $js ); do_action( 'wc_sc_call_for_credit_script_enqueued', array( 'source' => $this ) ); } $smart_coupon_store_gift_page_text = get_option( 'smart_coupon_store_gift_page_text' ); /* translators: %s: singular name for store credit */ $smart_coupon_store_gift_page_text = ( ! empty( $smart_coupon_store_gift_page_text ) ) ? $smart_coupon_store_gift_page_text . ' ' : ( ! empty( $store_credit_label['singular'] ) ? sprintf( __( 'Purchase %s worth', 'woocommerce-smart-coupons' ), ucwords( $store_credit_label['singular'] ) ) : __( 'Purchase credit worth', 'woocommerce-smart-coupons' ) ) . ' '; $custom_classes = array( 'container' => '', 'row' => '', 'label' => '', 'input' => '', 'error' => '', ); $custom_classes = apply_filters( 'wc_sc_call_for_credit_template_custom_classes', $custom_classes ); $currency_symbol = get_woocommerce_currency_symbol(); $input = array( 'type' => 'number', 'autocomplete' => 'off', 'autofocus' => 'autofocus', 'height' => '', 'max' => '', 'maxlength' => '', 'min' => '1', 'minlength' => '', 'name' => '', 'pattern' => '', 'placeholder' => '', 'required' => 'required', 'size' => '', 'step' => 'any', 'width' => '', 'class' => 'input-text', ); $input = apply_filters( 'wc_sc_call_for_credit_template_input', $input, array( 'source' => $this ) ); $input = array_filter( $input ); $input['id'] = 'credit_called'; $input['name'] = $input['id']; $input['value'] = ''; $allowed_html = wp_kses_allowed_html( 'post' ); $allowed_html['input'] = array( 'aria-describedby' => true, 'aria-details' => true, 'aria-label' => true, 'aria-labelledby' => true, 'aria-hidden' => true, ); $input_element = ' $value ) { $input_element .= $attribute . '="' . esc_attr( $value ) . '" '; $allowed_html['input'][ $attribute ] = true; } $input_element .= ' />'; if ( function_exists( 'wc_get_template' ) ) { $args = array( 'custom_classes' => $custom_classes, 'currency_symbol' => $currency_symbol, 'smart_coupon_store_gift_page_text' => $smart_coupon_store_gift_page_text, 'allowed_html' => $allowed_html, 'input' => $input, 'input_element' => $input_element, ); wc_get_template( 'call-for-credit-form.php', $args, '', plugin_dir_path( WC_SC_PLUGIN_FILE ) . 'templates/' ); } else { include apply_filters( 'woocommerce_call_for_credit_form_template', 'templates/call-for-credit-form.php' ); } } } /** * Allow overriding of Smart Coupon's template for credit of any amount * * @param string $template The template name. * @return mixed $template */ public function woocommerce_call_for_credit_form_template_path( $template ) { $template_name = 'call-for-credit-form.php'; $template = $this->locate_template_for_smart_coupons( $template_name, $template ); // Return what we found. return $template; } /** * Make product whose price is set as zero but is for purchasing credit, purchasable * * @param boolean $purchasable Is purchasable. * @param WC_Product $product The product. * @return boolean $purchasable */ public function make_product_purchasable( $purchasable, $product ) { if ( $this->is_wc_gte_30() ) { $product_id = ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0; } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; } $coupons = $this->get_coupon_titles( array( 'product_object' => $product ) ); if ( ! empty( $coupons ) && $product instanceof WC_Product && $product->get_price() === '' && $this->is_coupon_amount_pick_from_product_price( $coupons ) && ! ( $product->get_price() > 0 ) ) { return true; } return $purchasable; } /** * Remove price html for product which is selling any amount of storecredit * * @param string $price Current price HTML. * @param WC_Product $product The product object. * @return string $price */ public function price_html_for_purchasing_credit( $price = null, $product = null ) { if ( $this->is_wc_gte_30() ) { $product_id = ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0; } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; } $coupons = $this->get_coupon_titles( array( 'product_object' => $product ) ); $is_product = is_a( $product, 'WC_Product' ); $is_purchasable_credit = $this->is_coupon_amount_pick_from_product_price( $coupons ); $product_price = $product->get_price(); if ( ! empty( $coupons ) && true === $is_product && true === $is_purchasable_credit && ( ! ( $product_price > 0 ) || empty( $product_price ) ) ) { return ''; } return $price; } /** * Set price for store credit to be purchased before calculating total in cart * * @param WC_Cart $cart_object The cart object. */ public function override_price_before_calculate_totals( $cart_object ) { $credit_called = $this->get_session( 'credit_called' ); foreach ( $cart_object->cart_contents as $key => $value ) { $product = $value['data']; $credit_amount = ( ! empty( $value['credit_amount'] ) ) ? floatval( $value['credit_amount'] ) : 0; if ( $this->is_wc_gte_30() ) { $product_type = ( is_object( $product ) && is_callable( array( $product, 'get_type' ) ) ) ? $product->get_type() : ''; $product_id = ( in_array( $product_type, array( 'variable', 'variable-subscription', 'variation', 'subscription_variation' ), true ) ) ? ( ( is_object( $product ) && is_callable( array( $product, 'get_parent_id' ) ) ) ? $product->get_parent_id() : 0 ) : ( ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0 ); $product_price = ( is_object( $product ) && is_callable( array( $product, 'get_price' ) ) ) ? $product->get_price() : 0; } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; $product_price = ( ! empty( $product->price ) ) ? $product->price : 0; } $coupons = $this->get_coupon_titles( array( 'product_object' => $product ) ); if ( ! empty( $coupons ) && $this->is_coupon_amount_pick_from_product_price( $coupons ) && ! ( $product_price > 0 ) ) { $price = ( ! empty( $credit_called[ $key ] ) ) ? floatval( $credit_called[ $key ] ) : $credit_amount; if ( $price <= 0 ) { WC()->cart->set_quantity( $key, 0 ); // Remove product from cart if price is not found either in session or in product. continue; } $price = $this->read_price( $price, true ); if ( $this->is_wc_gte_30() && is_callable( array( $cart_object->cart_contents[ $key ]['data'], 'set_price' ) ) ) { $cart_object->cart_contents[ $key ]['data']->set_price( $price ); } else { $cart_object->cart_contents[ $key ]['data']->price = $price; } } } } /** * Override empty price of voucher product by 0 to avoid 'non numeric value' warning for product price being triggered on coupon validation. * * @param string $price product price. * @param WC_Product $product WooCommerce product. * @return integer $price new product price */ public function override_voucher_product_empty_price( $price, $product ) { if ( ! doing_action( 'woocommerce_single_product_summary' ) ) { if ( $this->is_wc_gte_30() ) { $product_type = ( is_object( $product ) && is_callable( array( $product, 'get_type' ) ) ) ? $product->get_type() : ''; $product_id = ( in_array( $product_type, array( 'variable', 'variable-subscription', 'variation', 'subscription_variation' ), true ) ) ? ( ( is_object( $product ) && is_callable( array( $product, 'get_parent_id' ) ) ) ? $product->get_parent_id() : 0 ) : ( ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0 ); } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; } $coupons = $this->get_coupon_titles( array( 'product_object' => $product ) ); // Override product price only if product contains coupon and price is already an empty string. if ( ! empty( $coupons ) && '' === $price ) { $price = 0; } } return $price; } /** * Display store credit's value as cart item's price * * @param string $product_price The product price HTML. * @param array $cart_item Associative array of cart item. * @param string $cart_item_key The cart item key. * @return string product's price with currency symbol */ public function woocommerce_cart_item_price_html( $product_price, $cart_item, $cart_item_key ) { $gift_certificate = $this->get_session( 'credit_called' ); if ( ! empty( $gift_certificate ) && isset( $gift_certificate[ $cart_item_key ] ) && ! empty( $gift_certificate[ $cart_item_key ] ) ) { $price = $gift_certificate[ $cart_item_key ]; $price = $this->read_price( $price, true ); // Hook for 3rd party plugin to modify value of the credit. $price = apply_filters( 'wc_sc_credit_called_price_cart', $price, array( 'source' => $this, 'cart_item_key' => $cart_item_key, 'cart_item' => $cart_item, 'credit_called' => $gift_certificate, ) ); return wc_price( $price ); } elseif ( ! empty( $cart_item['credit_amount'] ) ) { $price = $cart_item['credit_amount']; $price = $this->read_price( $price, true ); // Hook for 3rd party plugin to modify value of the credit. $price = apply_filters( 'wc_sc_credit_called_price_cart', $price, array( 'source' => $this, 'cart_item_key' => $cart_item_key, 'cart_item' => $cart_item, 'credit_called' => $gift_certificate, ) ); return wc_price( $price ); } return $product_price; } /** * Receiver Detail Form Styles And Scripts */ public function receiver_detail_form_styles_and_scripts() { global $wp_locale; if ( ! wp_script_is( 'jquery' ) ) { wp_enqueue_script( 'jquery' ); } $deliver_coupon_option = apply_filters( 'wc_sc_deliver_coupon', 'now', array( 'source' => $this, 'cart_contents' => ( isset( WC()->cart->cart_contents ) && is_array( WC()->cart->cart_contents ) ) ? WC()->cart->cart_contents : array(), 'schedule_store_credit' => get_option( 'smart_coupons_schedule_store_credit' ), ) ); ?> get_coupon_receiver_details_session(); if ( ! is_null( $coupon_receiver_details_session ) ) { $is_gift = ( ! empty( $coupon_receiver_details_session['is_gift'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['is_gift'] ) ) : ''; $sc_send_to = ( ! empty( $coupon_receiver_details_session['sc_send_to'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['sc_send_to'] ) ) : ''; $wc_sc_schedule_gift_sending = ( ! empty( $coupon_receiver_details_session['wc_sc_schedule_gift_sending'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['wc_sc_schedule_gift_sending'] ) ) : ''; $gift_receiver_email = ( ! empty( $coupon_receiver_details_session['gift_receiver_email'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['gift_receiver_email'] ) ) : array(); $gift_sending_date_time = ( ! empty( $coupon_receiver_details_session['gift_sending_date_time'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['gift_sending_date_time'] ) ) : array(); $gift_sending_timestamp = ( ! empty( $coupon_receiver_details_session['gift_sending_timestamp'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['gift_sending_timestamp'] ) ) : array(); $gift_receiver_message = ( ! empty( $coupon_receiver_details_session['gift_receiver_message'] ) ) ? wc_clean( wp_unslash( $coupon_receiver_details_session['gift_receiver_message'] ) ) : array(); } else { $is_gift = ''; $sc_send_to = ''; $wc_sc_schedule_gift_sending = ''; $gift_receiver_email = array(); $gift_sending_date_time = array(); $gift_sending_timestamp = array(); $gift_receiver_message = array(); } $coupon_receiver_details_session = array( 'is_gift' => $is_gift, 'sc_send_to' => $sc_send_to, 'wc_sc_schedule_gift_sending' => $wc_sc_schedule_gift_sending, 'gift_receiver_email' => $gift_receiver_email, 'gift_sending_date_time' => $gift_sending_date_time, 'gift_sending_timestamp' => $gift_sending_timestamp, 'gift_receiver_message' => $gift_receiver_message, ); foreach ( WC()->cart->cart_contents as $product ) { if ( ! empty( $product['variation_id'] ) ) { $_product = wc_get_product( $product['variation_id'] ); } elseif ( ! empty( $product['product_id'] ) ) { $_product = wc_get_product( $product['product_id'] ); } else { continue; } $coupon_titles = $this->get_coupon_titles( array( 'product_object' => $_product ) ); $price = $_product->get_price(); if ( $coupon_titles ) { foreach ( $coupon_titles as $coupon_title ) { $coupon = new WC_Coupon( $coupon_title ); if ( $this->is_wc_gte_30() ) { if ( ! is_object( $coupon ) || ! is_callable( array( $coupon, 'get_id' ) ) ) { continue; } $coupon_id = $coupon->get_id(); if ( empty( $coupon_id ) ) { continue; } $discount_type = $coupon->get_discount_type(); } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; $discount_type = ( ! empty( $coupon->discount_type ) ) ? $coupon->discount_type : ''; } $coupon_amount = $this->get_amount( $coupon, true ); $pick_price_of_prod = ( $this->is_callable( $coupon, 'get_meta' ) ) ? $coupon->get_meta( 'is_pick_price_of_product' ) : get_post_meta( $coupon_id, 'is_pick_price_of_product', true ); $smart_coupon_gift_certificate_form_page_text = get_option( 'smart_coupon_gift_certificate_form_page_text' ); $smart_coupon_gift_certificate_form_page_text = ( ! empty( $smart_coupon_gift_certificate_form_page_text ) ) ? $smart_coupon_gift_certificate_form_page_text : __( 'Send Coupons to...', 'woocommerce-smart-coupons' ); $smart_coupon_gift_certificate_form_details_text = get_option( 'smart_coupon_gift_certificate_form_details_text' ); $smart_coupon_gift_certificate_form_details_text = ( ! empty( $smart_coupon_gift_certificate_form_details_text ) ) ? $smart_coupon_gift_certificate_form_details_text : ''; // Enter email address and optional message for Gift Card receiver. // MADE CHANGES IN THE CONDITION TO SHOW FORM. if ( array_key_exists( $discount_type, $all_discount_types ) || ( 'yes' === $pick_price_of_prod && '' === $price ) || ( 'yes' === $pick_price_of_prod && '' !== $price && $coupon_amount > 0 ) ) { if ( ! $form_started ) { $is_show_coupon_receiver_form = get_option( 'smart_coupons_display_coupon_receiver_details_form', 'yes' ); if ( 'no' === $is_show_coupon_receiver_form ) { ?>

>

add_text_field_for_email( $coupon, $product, $coupon_receiver_details_session ); } } } } if ( $form_started ) { ?>
is_wc_gte_30() ) { add_action( 'woocommerce_new_order_item', array( $this, 'save_called_credit_details_in_order_item' ), 10, 3 ); } else { add_action( 'woocommerce_add_order_item_meta', array( $this, 'save_called_credit_details_in_order_item_meta' ), 10, 2 ); } } /** * Display form to enter receiver's details on checkout page * * @param WC_Coupon $coupon The coupon object. * @param array $product The product object. * @param array $coupon_receiver_details_session Coupon receiver details from. */ public function add_text_field_for_email( $coupon = '', $product = '', $coupon_receiver_details_session = array() ) { global $total_coupon_amount; if ( empty( $coupon ) ) { return; } extract( $coupon_receiver_details_session ); // phpcs:ignore $sell_sc_at_less_price = get_option( 'smart_coupons_sell_store_credit_at_less_price', 'no' ); $coupon_data = $this->get_coupon_meta_data( $coupon ); if ( $this->is_wc_gte_30() ) { $coupon_id = ( is_object( $coupon ) && is_callable( array( $coupon, 'get_id' ) ) ) ? $coupon->get_id() : ''; $coupon_code = ( is_object( $coupon ) && is_callable( array( $coupon, 'get_code' ) ) ) ? $coupon->get_code() : ''; $product_price = ( is_object( $product['data'] ) && is_callable( array( $product['data'], 'get_price' ) ) ) ? $product['data']->get_price() : 0; $is_free_shipping = ( is_object( $coupon ) && is_callable( array( $coupon, 'get_free_shipping' ) ) ) ? ( ( $coupon->get_free_shipping() ) ? 'yes' : 'no' ) : ''; $discount_type = ( is_object( $coupon ) && is_callable( array( $coupon, 'get_discount_type' ) ) ) ? $coupon->get_discount_type() : ''; } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; $coupon_code = ( ! empty( $coupon->code ) ) ? $coupon->code : ''; $product_price = ( ! empty( $product['data']->price ) ) ? $product['data']->price : 0; $is_free_shipping = ( ! empty( $coupon->free_shipping ) ) ? $coupon->free_shipping : ''; $discount_type = ( ! empty( $coupon->discount_type ) ) ? $coupon->discount_type : ''; } $coupon_amount = $this->get_amount( $coupon, true ); for ( $i = 0; $i < $product['quantity']; $i++ ) { if ( $this->is_coupon_amount_pick_from_product_price( array( $coupon_code ) ) ) { if ( 'yes' === $sell_sc_at_less_price ) { $_coupon_amount = ( is_object( $product['data'] ) && is_callable( array( $product['data'], 'get_regular_price' ) ) ) ? $product['data']->get_regular_price() : 0; if ( empty( $_coupon_amount ) && ! empty( $product_price ) ) { $_coupon_amount = $product_price; } } else { $_coupon_amount = $product_price; } if ( empty( $_coupon_amount ) && ! empty( $product['credit_amount'] ) ) { $_coupon_amount = (float) $product['credit_amount']; } } else { $_coupon_amount = $coupon_amount; } // NEWLY ADDED CONDITION TO NOT TO SHOW TEXTFIELD IF COUPON AMOUNT IS "0". // TODO: Free Gift Coupon: Due to coupon amount as zero, following condition is not showing multiple form to send coupon to different people. if ( '' !== $_coupon_amount || $_coupon_amount > 0 || $coupon_amount > 0 || 'yes' === $is_free_shipping ) { $total_coupon_amount += $_coupon_amount; $formatted_coupon_text = ''; $formatted_coupon_amount = 0; if ( ! empty( $_coupon_amount ) || ! empty( $coupon_amount ) ) { $formatted_coupon_amount = ( $coupon_amount <= 0 ) ? wc_price( $_coupon_amount ) : $coupon_data['coupon_amount']; $formatted_coupon_text .= $coupon_data['coupon_type']; if ( 'yes' === $is_free_shipping ) { $formatted_coupon_text .= ' & '; } } if ( 'yes' === $is_free_shipping ) { $formatted_coupon_text .= __( 'Free Shipping coupon', 'woocommerce-smart-coupons' ); } if ( 'smart_coupon' !== $discount_type && strpos( $formatted_coupon_text, 'coupon' ) === false ) { $formatted_coupon_text .= ' ' . __( 'coupon', 'woocommerce-smart-coupons' ); } ?>
is_callable( $order, 'get_id' ) ? $order->get_id() : 0; $this->save_called_credit_details_in_order( $order_id ); } } /** * Save entered credit value by customer in order for further processing * * @param int $order_id The order id. * @param array $posted Associative array of posted data. */ public function save_called_credit_details_in_order( $order_id = 0, $posted = array() ) { $order = wc_get_order( $order_id ); $order_items = $order->get_items(); $sc_called_credit = array(); $update = false; $prices_include_tax = ( get_option( 'woocommerce_prices_include_tax' ) === 'yes' ) ? true : false; foreach ( $order_items as $item_id => $order_item ) { $item_sc_called_credit = $this->get_meta( $order_item, 'sc_called_credit', true ); if ( ! empty( $item_sc_called_credit ) ) { $product = ( is_object( $order_item ) && is_callable( array( $order_item, 'get_product' ) ) ) ? $order_item->get_product() : $order->get_product_from_item( $order_item ); if ( ! is_object( $product ) ) { continue; } if ( $this->is_wc_gte_30() ) { $product_type = ( is_object( $product ) && is_callable( array( $product, 'get_type' ) ) ) ? $product->get_type() : ''; $product_id = ( in_array( $product_type, array( 'variable', 'variable-subscription', 'variation', 'subscription_variation' ), true ) ) ? ( ( is_object( $product ) && is_callable( array( $product, 'get_parent_id' ) ) ) ? $product->get_parent_id() : 0 ) : ( ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0 ); $item_qty = ( is_object( $order_item ) && is_callable( array( $order_item, 'get_quantity' ) ) ) ? $order_item->get_quantity() : 1; $item_tax = ( is_object( $order_item ) && is_callable( array( $order_item, 'get_subtotal_tax' ) ) ) ? $order_item->get_subtotal_tax() : 0; } else { $product_id = ( ! empty( $product->id ) ) ? $product->id : 0; $item_qty = ( ! empty( $order_item['qty'] ) ) ? $order_item['qty'] : 1; $item_tax = ( ! empty( $order_item['line_subtotal_tax'] ) ) ? $order_item['line_subtotal_tax'] : 0; } if ( true === $prices_include_tax && ! $this->is_generated_store_credit_includes_tax() ) { $item_total = $item_sc_called_credit - $item_tax; } elseif ( false === $prices_include_tax && $this->is_generated_store_credit_includes_tax() ) { $item_total = $item_sc_called_credit + $item_tax; } else { $item_total = $item_sc_called_credit; } if ( ! empty( $item_qty ) ) { $qty = $item_qty; } else { $qty = 1; } $coupon_titles = $this->get_coupon_titles( array( 'product_object' => $product ) ); if ( $coupon_titles ) { foreach ( $coupon_titles as $coupon_title ) { $coupon = new WC_Coupon( $coupon_title ); if ( $this->is_wc_gte_30() ) { if ( ! is_object( $coupon ) || ! is_callable( array( $coupon, 'get_id' ) ) ) { continue; } $coupon_id = $coupon->get_id(); if ( empty( $coupon_id ) ) { continue; } } else { $coupon_id = ( ! empty( $coupon->id ) ) ? $coupon->id : 0; } if ( $this->is_coupon_amount_pick_from_product_price( array( $coupon_title ) ) ) { $products_price = ( ! $prices_include_tax ) ? $item_total : $item_total + $item_tax; $amount = $products_price / $qty; $sc_called_credit[ $item_id ] = wc_round_discount( $amount, 2 ); $update = true; } } } $this->delete_order_item_meta( $item_id, 'sc_called_credit' ); } } if ( $update ) { $this->update_post_meta( $order_id, 'sc_called_credit_details', $sc_called_credit, true, $order ); } $this->maybe_clear_credit_called_session(); } /** * Save entered credit value by customer in order item meta * * @param int $item_id The order item id. * @param array $values Associative array containing item's details. */ public function save_called_credit_details_in_order_item_meta( $item_id = 0, $values = array() ) { if ( empty( $item_id ) || empty( $values ) ) { return; } $sell_sc_at_less_price = get_option( 'smart_coupons_sell_store_credit_at_less_price', 'no' ); if ( $this->is_wc_gte_30() ) { if ( ! $values instanceof WC_Order_Item_Product ) { return; } $product = $values->get_product(); if ( ! is_object( $product ) || ! is_a( $product, 'WC_Product' ) ) { return; } $product_type = ( is_object( $product ) && is_callable( array( $product, 'get_type' ) ) ) ? $product->get_type() : ''; $product_id = ( in_array( $product_type, array( 'variable', 'variable-subscription', 'variation', 'subscription_variation' ), true ) ) ? ( ( is_object( $product ) && is_callable( array( $product, 'get_parent_id' ) ) ) ? $product->get_parent_id() : 0 ) : ( ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0 ); $qty = ( is_callable( array( $values, 'get_quantity' ) ) ) ? $values->get_quantity() : 1; $qty = ( ! empty( $qty ) ) ? absint( $qty ) : 1; // Check if selling store credit at less price is enabled. if ( 'yes' === $sell_sc_at_less_price ) { // For gift certificate of any amount. if ( isset( $values->legacy_values['credit_amount'] ) && ! empty( $values->legacy_values['credit_amount'] ) ) { $order_id = ( ! empty( $item_id ) ) ? wc_get_order_id_by_order_item_id( $item_id ) : 0; $order = ( ! empty( $order_id ) ) ? wc_get_order( $order_id ) : null; $credit_amount = $this->read_price( $values->legacy_values['credit_amount'], true, $order ); $product_price = floatval( $credit_amount ) * $qty; } else { // For gift certificate of fixed price. $product_price = ( is_callable( array( $product, 'get_regular_price' ) ) ) ? floatval( $product->get_regular_price() ) * $qty : 0; } } else { $subtotal = ( is_callable( array( $values, 'get_subtotal' ) ) ) ? $values->get_subtotal() : 0; $product_price = $subtotal; } if ( wp_doing_ajax() && defined( 'WP_ADMIN' ) && true === WP_ADMIN ) { $action = ( ! empty( $_POST['action'] ) ) ? wc_clean( wp_unslash( $_POST['action'] ) ) : ''; // phpcs:ignore $order_id = ( ! empty( $_POST['order_id'] ) ) ? absint( $_POST['order_id'] ) : 0; if ( 'woocommerce_add_order_item' === $action && check_ajax_referer( 'order-item', 'security' ) && ! empty( $order_id ) ) { $product_price = $product_price / $qty; } } } else { if ( empty( $values['data'] ) ) { return; } $product = $values['data']; $product_id = ( ! empty( $values['product_id'] ) ) ? $values['product_id'] : 0; $product_price = ( ! empty( $values['data']->price ) ) ? $values['data']->price : 0; } if ( empty( $product_id ) ) { return; } $coupon_titles = $this->get_coupon_titles( array( 'product_object' => $product ) ); if ( $this->is_coupon_amount_pick_from_product_price( $coupon_titles ) && $product_price > 0 ) { $this->add_order_item_meta( $item_id, 'sc_called_credit', $product_price, true ); } } /** * Save entered credit value by customer in order item * * @param int $item_id The order item id. * @param array $values Associative array containing item's details. * @param int $order_id The order id. */ public function save_called_credit_details_in_order_item( $item_id = 0, $values = array(), $order_id = 0 ) { $this->save_called_credit_details_in_order_item_meta( $item_id, $values ); } /** * Save entered credit value by customer in order for PayPal Express Checkout * * @param WC_Order $order The order object. */ public function ppe_save_called_credit_details_in_order( $order ) { if ( $this->is_wc_gte_30() ) { $order_id = ( is_object( $order ) && is_callable( array( $order, 'get_id' ) ) ) ? $order->get_id() : 0; } else { $order_id = ( ! empty( $order->id ) ) ? $order->id : 0; } $this->save_called_credit_details_in_order( $order_id, null ); } /** * Save entered credit value by customer in cart item data * * This function is only for simple products. * * @param array $cart_item_data The cart item data. * @param int $product_id The product id. * @param int $variation_id The variation id. * @return array $cart_item_data */ public function call_for_credit_cart_item_data( $cart_item_data = array(), $product_id = '', $variation_id = '' ) { if ( ! empty( $variation_id ) && $variation_id > 0 || empty( $product_id ) ) { return $cart_item_data; } $_product = wc_get_product( $product_id ); $coupons = ( $this->is_callable( $_product, 'get_meta' ) ) ? $_product->get_meta( '_coupon_title' ) : get_post_meta( $product_id, '_coupon_title', true ); if ( ! empty( $coupons ) && $this->is_coupon_amount_pick_from_product_price( $coupons ) && ! ( $_product->get_price() > 0 ) ) { $request_credit_called = ( ! empty( $_REQUEST['credit_called'] ) ) ? wc_clean( wp_unslash( $_REQUEST['credit_called'] ) ) : array(); // phpcs:ignore $request_add_to_cart = ( ! empty( $_REQUEST['add-to-cart'] ) ) ? wc_clean( wp_unslash( $_REQUEST['add-to-cart'] ) ) : 0; // phpcs:ignore $request_wc_ajax = ( ! empty( $_REQUEST['wc-ajax'] ) ) ? wc_clean( wp_unslash( $_REQUEST['wc-ajax'] ) ) : ''; // phpcs:ignore $request_product_id = ( ! empty( $_REQUEST['product_id'] ) ) ? absint( $_REQUEST['product_id'] ) : 0; // phpcs:ignore $request_add_to_cart = ( 'add_to_cart' === $request_wc_ajax && ! empty( $request_product_id ) ) ? $request_product_id : $request_add_to_cart; $product_id = apply_filters( 'wc_sc_call_for_credit_product_id', $request_add_to_cart, array( 'cart_item_data' => $cart_item_data, 'product_id' => $product_id, 'variation_id' => $variation_id, 'source' => $this, ) ); $cart_item_data['credit_amount'] = ( ! empty( $request_credit_called ) && ! empty( $product_id ) && ! empty( $request_credit_called[ $product_id ] ) ) ? $this->write_price( $request_credit_called[ $product_id ], true ) : 0; return $cart_item_data; } return $cart_item_data; } /** * Save entered credit value by customer in session * * This function is only for simple products. * * @param string $cart_item_key The cart item key. * @param int $product_id The product id. * @param int $quantity The product quantity. * @param int $variation_id The variation id. * @param array $variation The variation data. * @param array $cart_item_data The cart item data. */ public function save_called_credit_in_session( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { if ( ! empty( $variation_id ) && $variation_id > 0 ) { return; } if ( ! isset( $cart_item_data['credit_amount'] ) || empty( $cart_item_data['credit_amount'] ) ) { return; } $_product = wc_get_product( $product_id ); $coupons = ( $this->is_callable( $_product, 'get_meta' ) ) ? $_product->get_meta( '_coupon_title' ) : get_post_meta( $product_id, '_coupon_title', true ); if ( ! empty( $coupons ) && $this->is_coupon_amount_pick_from_product_price( $coupons ) && ! ( $_product->get_price() > 0 ) ) { $credit_called = $this->get_session( 'credit_called' ); if ( empty( $credit_called ) || ! is_array( $credit_called ) ) { $credit_called = array(); } $credit_called[ $cart_item_key ] = $cart_item_data['credit_amount']; $this->set_session( 'credit_called', $credit_called ); } } /** * Enqueue required styles/scripts for store credit frontend form */ public function enqueue_styles_scripts() { // Return if gift certificate form is not shown. if ( ! did_action( 'wc_sc_gift_certificate_form_shown' ) ) { return; } $this->enqueue_timepicker(); ?> 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' ); } } /** * Clear session variable credit_called (if set) when cart is empty */ public function maybe_clear_credit_called_session() { if ( WC()->session->__isset( 'credit_called' ) ) { WC()->session->__unset( 'credit_called' ); } } /** * Validate checkout based on whether delivery time is required or not * * @param array $fields checkout fields. * @param WP_Error $errors WP Error Object. * @return void */ public function maybe_required_coupon_delivery_date_time( $fields = array(), $errors = object ) { $nonce_value = ( ! empty( $_POST['woocommerce-process-checkout-nonce'] ) ) ? wc_clean( wp_unslash( $_POST['woocommerce-process-checkout-nonce'] ) ) : ''; // phpcs:ignore if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-process_checkout' ) ) { return; } global $store_credit_label; $is_schedule_gift_sending = ( ! empty( $_POST['wc_sc_schedule_gift_sending'] ) ) ? wc_clean( wp_unslash( $_POST['wc_sc_schedule_gift_sending'] ) ) : ''; // phpcs:ignore $gift_sending_timestamp = ( ! empty( $_POST['gift_sending_timestamp'] ) && is_array( $_POST['gift_sending_timestamp'] ) ) ? wc_clean( wp_unslash( $_POST['gift_sending_timestamp'] ) ) : array(); // phpcs:ignore $send_to = ( ! empty( $_POST['sc_send_to'] ) ) ? wc_clean( wp_unslash( $_POST['sc_send_to'] ) ) : ''; // phpcs:ignore $is_gift = ( ! empty( $_POST['is_gift'] ) ) ? wc_clean( wp_unslash( $_POST['is_gift'] ) ) : ''; // phpcs:ignore if ( 'yes' === $is_gift && 'yes' === $is_schedule_gift_sending ) { $is_date_available = true; if ( is_array( $gift_sending_timestamp ) && ! empty( $gift_sending_timestamp ) ) { if ( 'many' === $send_to ) { foreach ( $gift_sending_timestamp as $coupon_id => $timestamp ) { if ( 0 === absint( $coupon_id ) ) { continue; } $filtered = array_filter( $timestamp ); if ( count( $filtered ) !== count( $timestamp ) ) { $is_date_available = false; break; } } } elseif ( 'one' === $send_to ) { if ( empty( $gift_sending_timestamp[0][0] ) ) { $is_date_available = false; } } } if ( false === $is_date_available ) { if ( is_object( $errors ) && is_callable( array( $errors, 'add' ) ) ) { /* translators: %s: field name */ $errors->add( 'validation', __( 'Coupon delivery date and time is a required field.', 'woocommerce-smart-coupons' ) ); } } } } /** * Save coupon receiver details in session * * @param array $fields checkout fields. * @param WP_Error $errors WP Error Object. */ public function save_coupon_receiver_details_in_session( $fields = array(), $errors = object ) { $nonce_value = ( ! empty( $_POST['woocommerce-process-checkout-nonce'] ) ) ? wc_clean( wp_unslash( $_POST['woocommerce-process-checkout-nonce'] ) ) : ''; // phpcs:ignore if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-process_checkout' ) ) { return; } $posted_data = array_map( 'wp_unslash', $_POST ); // phpcs:ignore $posted_data = wc_clean( $posted_data ); // phpcs:ignore $this->set_coupon_receiver_details_session( $posted_data ); } /** * Set coupon receiver details in session variable * * @param array $posted_data The posted data. */ public function set_coupon_receiver_details_session( $posted_data = array() ) { if ( is_callable( 'WC' ) && is_object( WC() ) && is_object( WC()->session ) && is_callable( array( WC()->session, 'set' ) ) ) { $keys_to_copy = array( 'is_gift', 'sc_send_to', 'wc_sc_schedule_gift_sending', 'gift_receiver_email', 'gift_sending_date_time', 'gift_sending_timestamp', 'gift_receiver_message' ); $coupon_receiver_details = array(); foreach ( $keys_to_copy as $key ) { if ( isset( $posted_data[ $key ] ) ) { $coupon_receiver_details[ $key ] = $posted_data[ $key ]; } } $coupon_receiver_details = apply_filters( 'wc_sc_before_set_coupon_receiver_details_session', $coupon_receiver_details, array( 'source' => $this, 'posted_data' => $posted_data, ) ); if ( ! empty( $coupon_receiver_details ) ) { WC()->session->set( 'wc_sc_coupon_receiver_details_session', $coupon_receiver_details ); } else { WC()->session->set( 'wc_sc_coupon_receiver_details_session', null ); } } } /** * Get coupon receiver details from session variable */ public function get_coupon_receiver_details_session() { if ( is_callable( 'WC' ) && is_object( WC() ) && is_object( WC()->session ) && is_callable( array( WC()->session, 'get' ) ) ) { return WC()->session->get( 'wc_sc_coupon_receiver_details_session' ); } return null; } /** * Function to cleanup order item meta if not required * * @param integer $order_id The order id. */ public function cleanup_order_item_meta( $order_id = 0 ) { if ( empty( $order_id ) ) { return; } $order = wc_get_order( $order_id ); $order_items = ( is_object( $order ) && is_callable( array( $order, 'get_items' ) ) ) ? $order->get_items() : array(); foreach ( $order_items as $item_id => $order_item ) { $this->delete_order_item_meta( $item_id, 'sc_called_credit' ); } } /** * Save coupon receiver details in session during update order review * * @param array $fragments The fragments. * @return array */ public function update_order_review_fragments( $fragments = array() ) { if ( ! empty( $_POST['post_data'] ) ) { // phpcs:ignore wp_parse_str( $_POST['post_data'], $posted_data ); // phpcs:ignore $this->set_coupon_receiver_details_session( $posted_data ); } return $fragments; } /** * Save coupon receiver detail via AJAX */ public function ajax_save_coupon_receiver_details() { check_ajax_referer( 'wc-sc-save-coupon-receiver-details', 'security' ); wp_parse_str( wc_clean( wp_unslash( urldecode_deep( $_POST['data'] ) ) ), $posted_data ); // phpcs:ignore $this->set_coupon_receiver_details_session( $posted_data ); wp_send_json( array( 'success' => 'yes' ) ); } /** * Check & clear coupon receiver details from session when cart is emptied (e.g. after creation of order) */ public function maybe_clear_coupon_receiver_details_session() { $coupon_receiver_details_session = $this->get_coupon_receiver_details_session(); if ( ! is_null( $coupon_receiver_details_session ) ) { WC()->session->set( 'wc_sc_coupon_receiver_details_session', null ); } } } } WC_SC_Purchase_Credit::get_instance();