我可以用action/filter钩子修改这个wordpress插件函数吗?


Can I modify this wordpress plugin function with a action/filter hook?

我需要修改一个插件创建的函数中的3行。

下面是原始函数(查找我在中间用星号突出显示的注释部分):

/**
         * function to modify order_items of renewal order
         *
         * @param array $order_items
         * @param int $original_order_id
         * @param int $renewal_order_id
         * @param int $product_id
         * @param string $new_order_role
         * @return array $order_items
         */
        public function sc_subscriptions_renewal_order_items( $order_items = null, $original_order_id = 0, $renewal_order_id = 0, $product_id = 0, $new_order_role = null ) {

            $is_subscription_order = wcs_order_contains_subscription( $original_order_id );
            if ( $is_subscription_order ) {
                $return = false;
            } else {
                $return = true;
            }
            if ( $return ) {
                return $order_items;
            }
            $pay_from_credit_of_original_order = get_option( 'pay_from_smart_coupon_of_original_order', 'yes' );
            if ( $pay_from_credit_of_original_order != 'yes' ) return $order_items;
            if ( $new_order_role != 'child' ) return $order_items;
            if ( empty( $renewal_order_id ) || empty( $original_order_id ) ) return $order_items;
            $original_order = $this->get_order( $original_order_id );
            $renewal_order = $this->get_order( $renewal_order_id );
            $coupon_used_in_original_order = $original_order->get_used_coupons();
            $coupon_used_in_renewal_order = $renewal_order->get_used_coupons();
            if ( sizeof( $coupon_used_in_original_order ) > 0 ) {
                $smart_coupons_contribution = array();
                foreach ( $coupon_used_in_original_order as $coupon_code ) {
                    $coupon = new WC_Coupon( $coupon_code );
                    if ( ! empty( $coupon->discount_type ) && $coupon->discount_type == 'smart_coupon' && ! empty( $coupon->amount ) && ! in_array( $coupon_code, $coupon_used_in_renewal_order, true ) ) {
                        $renewal_order_total = $renewal_order->get_total();
                       /* ************
                       THE BELOW 3 LINES ARE WHAT I NEED TO REMOVE 
                       **************** */
                        if ( $coupon->amount < $renewal_order_total ) {
                             continue;
                        }
                       /* ************
                       THE ABOVE 3 LINES ARE WHAT I NEED TO REMOVE 
                       **************** */
                        $discount = min( $renewal_order_total, $coupon->amount );
                        if ( $discount > 0 ) {
                            $new_order_total = $renewal_order_total - $discount;
                            update_post_meta( $renewal_order_id, '_order_total', $new_order_total );
                            update_post_meta( $renewal_order_id, '_order_discount', $discount );
                            if ( $new_order_total <= floatval(0) ) {
                                update_post_meta( $renewal_order_id, '_renewal_paid_by_smart_coupon', 'yes' );
                            }
                            $renewal_order->add_coupon( $coupon_code, $discount );
                            $smart_coupons_contribution[ $coupon_code ] = $discount;
                            $used_by = $renewal_order->get_user_id();
                            if ( ! $used_by ) {
                                $used_by = $renewal_order->billing_email;
                            }
                            $coupon->inc_usage_count( $used_by );
                        }
                    }
                }
                if ( ! empty( $smart_coupons_contribution ) ) {
                    update_post_meta( $renewal_order_id, 'smart_coupons_contribution', $smart_coupons_contribution );
                }
            }
            return $order_items;
        }

我想删除上面代码中间注释掉的行:

if ( $coupon->amount < $renewal_order_total ) {
    continue;
 }

有没有办法做到这一点,而不编辑核心插件代码?我能自己写函数来改变这些行吗?

感谢您提供的任何帮助!

如果这个函数与action/filter挂钩,那么你可以通过remove_actionremove_filter删除它,并在那里挂钩你的函数。

https://codex.wordpress.org/Function_Reference/remove_actionhttps://codex.wordpress.org/Function_Reference/remove_filter

检查函数被调用的位置