重写类';s方法在PHP中不起作用
这已经是一个多星期了,我一直在努力解决这个问题,还没有解决它。我试图扩展一个类并覆盖它的一些方法,以便在那里显示我自己的消息: 父类:重写类';s方法在PHP中不起作用,php,wordpress,Php,Wordpress,这已经是一个多星期了,我一直在努力解决这个问题,还没有解决它。我试图扩展一个类并覆盖它的一些方法,以便在那里显示我自己的消息: 父类: class Order { public function details( $echo = true ) { $cart = $this->get_cart(); $currency = $this->get_meta( 'mp_payment_info->currency', '' ); $cart =
class Order {
public function details( $echo = true ) {
$cart = $this->get_cart();
$currency = $this->get_meta( 'mp_payment_info->currency', '' );
$cart = $this->get_meta( 'mp_cart_items' );
if ( ! $cart ) {
$cart = $this->get_meta( 'mp_cart_info' );
}
/**
* Filter the confirmation text
*
* @since 3.0
*
* @param string The current confirmation text.
* @param MP_Order The order object.
*/
$confirmation_text = apply_filters( 'mp_order/confirmation_text', '', $this );
$confirmation_text = apply_filters( 'mp_order/confirmation_text/' . $this->get_meta( 'mp_payment_info->gateway_plugin_name' ), $confirmation_text, $this );
$cart_contents = '';
ob_start();
?>
<?php if ( is_array( $cart ) ): ?>
<?php foreach ( $cart as $product_id => $items ): ?>
<?php foreach ( $items as $item ): ?>
<?php $product = new MP_Product( $product_id ); ?>
<div class="mp_cart_item" id="mp-cart-item-104">
<div class="mp_cart_item_content mp_cart_item_content-thumb"><img
src="<?php echo $product->image_url( false ) ?>"
width="75" height="75" style="max-height: 75px;">
</div>
<!-- end mp_cart_item_content -->
<div class="mp_cart_item_content mp_cart_item_content-title">
<h2 class="mp_cart_item_title">
<a href="<?php echo $item['url'] ?>"><?php echo $item['name'] ?></a>
</h2>
<?php
if ( $product->is_download() && mp_is_shop_page( 'order_status' ) ) {
echo '<a target="_blank" href="' . $product->download_url( get_query_var( 'mp_order_id' ), false ) . '">' . __( 'Download', 'mp' ) . '</a>';
}
?>
</div>
<!-- end mp_cart_item_content -->
<div class="mp_cart_item_content mp_cart_item_content-price"><!-- MP Product Price -->
<div class="mp_product_price" itemtype="http://schema.org/Offer" itemscope=""
itemprop="offers">
<span class="mp_product_price-normal"
itemprop="price"><?php echo mp_format_currency( '', $item['price'] ) ?></span>
</div>
<!-- end mp_product_price -->
</div>
<!-- end mp_cart_item_content -->
<div
class="mp_cart_item_content mp_cart_item_content-qty"><?php echo $item['quantity'] ?>
</div>
<!-- end mp_cart_item_content --></div><!-- end mp_cart_item -->
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<?php
$cart->display( array(
'echo' => true,
'view' => 'order-status',
'editable' => false,
) );
?>
<?php endif; ?>
<?php
$cart_contents = ob_get_clean();
$html = '
<!-- MP Single Order Details -->
<section id="mp-single-order-details" class="mp_orders">
<div class="mp_order_details">
<div class="mp_order">' .
$this->header( false ) .
'</div><!-- end mp_order -->' .
$confirmation_text . '
<div class="mp_order_cart">' .
$cart_contents . '
</div><!-- end mp_order_cart -->
<div class="mp_order_address">' .
$this->get_addresses() . '
</div><!-- end mp_order_address -->
</div><!-- end mp_order_details -->
</section><!-- end mp-single-order-details -->';
/**
* Filter the order details
*
* @since 3.0
*
* @param string $html The current details.
* @param MP_Order $this The current order object.
*/
$html = apply_filters( 'mp_order/details', $html, $this );
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
}
class My_Order extends Order {
public function details( $echo = true ) {
$cart = $this->get_cart();
$currency = $this->get_meta( 'mp_payment_info->currency', '' );
$cart = $this->get_meta( 'mp_cart_items' );
if ( ! $cart ) {
$cart = $this->get_meta( 'mp_cart_info' );
}
/**
* Filter the confirmation text
*
* @since 3.0
*
* @param string The current confirmation text.
* @param MP_Order The order object.
*/
$confirmation_text = apply_filters( 'mp_order/confirmation_text', '', $this );
$confirmation_text = apply_filters( 'mp_order/confirmation_text/' . $this->get_meta( 'mp_payment_info->gateway_plugin_name' ), $confirmation_text, $this );
$cart_contents = '';
ob_start();
?>
<?php if ( is_array( $cart ) ): ?>
<?php foreach ( $cart as $product_id => $items ): ?>
<?php foreach ( $items as $item ): ?>
<?php $product = new MP_Product( $product_id ); ?>
<div class="mp_cart_item" id="mp-cart-item-104">
<div class="mp_cart_item_content mp_cart_item_content-thumb"><img
src="<?php echo $product->image_url( false ) ?>"
width="75" height="75" style="max-height: 75px;">
</div>
<!-- end mp_cart_item_content -->
<div class="mp_cart_item_content mp_cart_item_content-title">
<h2 class="mp_cart_item_title">
<a href="<?php echo $item['url'] ?>"><?php echo $item['name'] ?></a>
</h2>
<?php
if ( $product->is_download() && mp_is_shop_page( 'order_status' ) ) {
echo '<a target="_blank" href="' . $product->download_url( get_query_var( 'mp_order_id' ), false ) . '">' . __( 'Download', 'mp' ) . '</a>';
}
?>
</div>
<!-- end mp_cart_item_content -->
<div class="mp_cart_item_content mp_cart_item_content-price"><!-- MP Product Price -->
<div class="mp_product_price" itemtype="http://schema.org/Offer" itemscope=""
itemprop="offers">
<span class="mp_product_price-normal"
itemprop="price"><?php echo mp_format_currency( '', $item['price'] ) ?></span>
</div>
<!-- end mp_product_price -->
</div>
<!-- end mp_cart_item_content -->
<div
class="mp_cart_item_content mp_cart_item_content-qty"><?php echo $item['quantity'] ?>
</div>
<!-- end mp_cart_item_content --></div><!-- end mp_cart_item -->
<?php endforeach; ?>
<?php endforeach; ?>
<?php else: ?>
<?php
$cart->display( array(
'echo' => true,
'view' => 'order-status',
'editable' => false,
) );
?>
<?php endif; ?>
<?php
$cart_contents = ob_get_clean();
$html = 'I JUST WANT TO REPLACE THIS';
/**
* Filter the order details
*
* @since 3.0
*
* @param string $html The current details.
* @param MP_Order $this The current order object.
*/
$html = apply_filters( 'mp_order/details', $html, $this );
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
}
类顺序{
公共函数详细信息($echo=true){
$cart=$this->get_cart();
$currency=$this->get\u meta('mp\u payment\u info->currency','';
$cart=$this->get_meta('mp_cart_items');
如果(!$cart){
$cart=$this->get_meta('mp_cart_info');
}
/**
*过滤确认文本
*
*@自3.0以来
*
*@param字符串当前确认文本。
*@param MP_对订单对象进行排序。
*/
$confirmation\u text=应用过滤器('mp\u订单/确认\u text','',$this);
$confirmation\u text=apply\u filters('mp\u order/confirmation\u text/'。$this->get\u meta('mp\u payment\u info->gateway\u plugin\u name'),$confirmation\u text,$this);
$cart_contents='';
ob_start();
?>
图像\u url(错误)?>“
width=“75”height=“75”style=“最大高度:75px;”>
图像\u url(错误)?>“
width=“75”height=“75”style=“最大高度:75px;”>
如果能够操作基类,则可以创建受保护的或公共的方法,并覆盖此方法:
基类:
class Order {
public function details( $echo = true ) {
//...
$html = applyMyAwesomeFilters('mp_order/details', $html, $this);
//...
}
protected function applyMyAwesomeFilters($path, $html, $this)
{
return apply_filters($path, $html, $this);
}
}
然后重写继承类中的函数:
class My_Order extends Order {
protected function applyMyAwesomeFilters($path, $html, $this)
{
//Do something totally different here...
return $somethingAwesome;
}
}
您使用的是My_Order
而不是Order
?只需在子类中调用$parent\u html=parent::details(false);
,然后对结果执行任何您想要的操作?