我可以使用动作/过滤器钩子修改这个wordpress插件功能吗?

时间:2016-11-02 21:30:35

标签: php wordpress woocommerce

我需要在插件创建的函数中修改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;
 }

有没有办法在不编辑核心插件代码的情况下执行此操作?我可以编写自己的函数来更改这些行吗?

感谢您提供任何帮助!

1 个答案:

答案 0 :(得分:0)

如果此功能与动作/过滤器挂钩挂钩,那么您可以通过remove_actionremove_filter将其删除并挂钩您的功能。

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

因此,请检查此函数的调用位置。