使用wooccommerce时,覆盖includes/abstracts文件夹中包含的php函数/文件的正确方法是什么


What is the proper way to override a php function / file containted in the includes / abstracts folder when using woocommerce?

使用wooccommerce时,覆盖includes文件夹中php文件的正确方法是什么?

作为我想改变的许多事情之一的一个例子:"我认为用<del></del>包装销售价格是愚蠢的(<span> or <div>对我来说会更好),我无论如何都想改变布局,所以我想更改代码

/**
 * Functions for getting parts of a price, in html, used by get_price_html.
 *
 * @param  mixed $from String or float to wrap with 'from' text
 * @param  mixed $to String or float to wrap with 'to' text
 * @return string
 */
public function get_price_html_from_to( $from, $to ) {
    return '<del>' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</del> <ins>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</ins>';
}

文件中

plugins/woocommerce/includes/abstracts/abstract-wc-product.php

我可以在这个文件中修改它,但我知道这是不正确的做法,而且它也会被下一次woo更新覆盖。

那么,更改此代码的正确方法是什么

如果价格大于0,您可以通过woocommerce_sale_price_html过滤器更改价格html的值。或者,如果价格为0,则可以针对woocommerce_free_sale_price_html过滤器。参见WC_Product摘要中的get_price_html()方法。

如果价格大于0:,这是相关的比特

if ( $this->is_on_sale() && $this->get_regular_price() ) {
    $price .= $this->get_price_html_from_to( $display_regular_price, $display_price ) . $this->get_price_suffix();
    $price = apply_filters( 'woocommerce_sale_price_html', $price, $this );
} 

因此,你可以过滤html,而不是使用WC的默认get_price_html_from_to方法,你可以用自己的自定义标记创建自己的函数,比如:

add_filter( 'woocommerce_sale_price_html', 'so_26002687_sale_price_html', 10, 2);
function so_26002687_sale_price_html( $html, $product ){
    $tax_display_mode      = get_option( 'woocommerce_tax_display_shop' );
    $display_price  = $tax_display_mode == 'incl' ? $product->get_price_including_tax() : $product->get_price_excluding_tax();
    $display_regular_price = $tax_display_mode == 'incl' ? $product->get_price_including_tax( 1, $product->get_regular_price() ) : $product->get_price_excluding_tax( 1, $product->get_regular_price() );
    $html = so_26002687_get_price_html_from_to( $display_regular_price, $display_price ) . $product->get_price_suffix();
    return $html;
}
function so_26002687_get_price_html_from_to( $from, $to ) {
    return '<span class="from">' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</span> <span class="to">' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</span>';
}

然后我会重复woocommerce_free_sale_price_html的过程。

我想您可以创建自己的文件,并在包含来自wooccommerce的原始文件后包含/需要它。在您的文件中,您重新定义需要执行的功能:

bool runkit_function_redefine ( string $funcname , string $arglist , string $code );

我认为这是你最好的选择,尽管它有点晦涩难懂。

$funcname是要重新定义的函数的名称。

$arglist是新的参数列表(采用string格式!)。

$code是新代码(同样是string格式)。

希望能有所帮助。

更多信息:重新定义PHP手册

创建您自己的类来实现WC_Product的"抽象"类,并确保您的产品注册为该类型,以便WC_Product_Factory返回该类的实例,而不是WC_Product;例如

class WC_Product_Your_Type extends WC_Product {
  public function get_price_html_from_to( $from, $to ) {
    return '<span>' . ( ( is_numeric( $from ) ) ? wc_price( $from ) : $from ) . '</span> <div>' . ( ( is_numeric( $to ) ) ? wc_price( $to ) : $to ) . '</div>';
  }
}