php - 如何更改 woocommerce 的核心功能

标签 php wordpress oop woocommerce

我正在尝试更改 woocommerce\includes\class-wc-coupon.php 中存在的公共(public)函数

更改核心文件不好,因为我不想每次更新 woocommerce 时都更改核心文件,这就是为什么我试图找到一种方法,可以在我的主题functions.php文件使其工作

这里是核心功能

/**
 * Get discount amount for a cart item
 * 
 * @param  float $discounting_amount Amount the coupon is being applied to
 * @param  array|null $cart_item Cart item being discounted if applicable
 * @param  boolean $single True if discounting a single qty item, false if its the line
 * @return float Amount this coupon has discounted
 */
public function get_discount_amount( $discounting_amount, $cart_item = null, $single = false ) {
    $discount = 0;

    if ( $this->type == 'fixed_product') {

        $discount = $discounting_amount < $this->amount ? $discounting_amount : $this->amount;

        // If dealing with a line and not a single item, we need to multiple fixed discount by cart item qty.
        if ( ! $single && ! is_null( $cart_item ) ) {
            // Discount for the line.
            $discount = $discount * $cart_item['quantity'];
        }

    } elseif ( $this->type == 'percent_product' || $this->type == 'percent' ) {

        $discount = round( ( $discounting_amount / 100 ) * $this->amount, WC()->cart->dp );

    } elseif ( $this->type == 'fixed_cart' ) {
        if ( ! is_null( $cart_item ) ) {
            /**
             * This is the most complex discount - we need to divide the discount between rows based on their price in
             * proportion to the subtotal. This is so rows with different tax rates get a fair discount, and so rows
             * with no price (free) don't get discounted.
             *
             * Get item discount by dividing item cost by subtotal to get a %
             */
            $discount_percent = 0;

            if ( WC()->cart->subtotal_ex_tax )
                $discount_percent = ( $cart_item['data']->get_price_excluding_tax() * $cart_item['quantity'] ) / WC()->cart->subtotal_ex_tax;

            $discount = min( ( $this->amount * $discount_percent ) / $cart_item['quantity'], $discounting_amount );
        } else {
            $discount = min( $this->amount, $discounting_amount );
        }
    }

    // Handle the limit_usage_to_x_items option
    if ( in_array( $this->type, array( 'percent_product', 'fixed_product' ) ) && ! is_null( $cart_item ) ) {
        $qty = empty( $this->limit_usage_to_x_items ) ? $cart_item['quantity'] : min( $this->limit_usage_to_x_items, $cart_item['quantity'] );

        if ( $single ) {
            $discount = ( $discount * $qty ) / $cart_item['quantity'];
        } else {
            $discount = ( $discount / $cart_item['quantity'] ) * $qty;
        }
    }
    return $discount;
}

我想在 return $discount; 之前添加 $discount = $discount * -1;

谢谢

最佳答案

有点晚了,但在当前的 WooCommerce 版本 2.3.x 中,您可以使用过滤器“woocommerce_coupon_get_discount_amount”在主题中对其进行过滤,以防止更新删除您的更改。

关于php - 如何更改 woocommerce 的核心功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22800398/

相关文章:

c# - 字段和属性有什么区别?

php - 在 Woocommerce 电子邮件中获取一些订单和订单项目数据

php - 如何在codeigniter中从动态表插入多个数据到mysql

php - 将 IN 与数组一起使用时如何使用多个 WHERE 语句

mysql - wordpress docker无法连接mysql docker

java - 协方差在这里安全吗?

javascript - WordPress: fatal error :调用未定义的函数 wp_get_current_user()

php - 在 where 子句中添加什么才能获得所需的结果?

html - 如何将文本和图像保持在 div 内的一行中?

matlab - 对象方法中的自引用