覆盖类不能在PHP中工作的方法

时间:2016-03-24 14:21:58

标签: php wordpress-plugin

我已经用了一个多星期的时间来解决这个问题并且没有解决这个问题。我试图扩展一个类并覆盖它的一些方法,以便我可以在那里显示自己的消息:

父类:

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;
    }
}
}

我唯一想要覆盖的是: $html = 'I JUST WANT TO REPLACE THIS';

我也试过覆盖这个类的其他方法,但它们都不起作用。

由于

1 个答案:

答案 0 :(得分:0)

如果您能够操作基类而不是创建受保护或公共方法而不是覆盖此方法:

基类:

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;
    }
}