首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我能用action/filter钩子修改这个wordpress插件函数吗?

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

Stack Overflow用户
提问于 2016-11-03 05:30:35
回答 1查看 111关注 0票数 1

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

以下是原始函数(请查看中间用星号突出显示的注释部分):

代码语言:javascript
复制
/**
         * 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;
        }

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

代码语言:javascript
复制
if ( $coupon->amount < $renewal_order_total ) {
    continue;
 }

有没有办法在不编辑核心插件代码的情况下做到这一点?我是否可以编写自己的函数来更改这些行?

感谢您能提供的任何帮助!

EN

回答 1

Stack Overflow用户

发布于 2016-11-03 14:35:37

如果这个函数是通过操作/过滤器钩子挂起的,那么您可以通过remove_actionremove_filter将其移除,并将其挂接到您的函数中。

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40390156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档