覆盖功能并跳过部分原始功能

时间:2017-08-18 06:18:08

标签: php

我的问题如下。我想为现有方法创建一个覆盖,并禁用或跳过原始代码的一部分代码。

在扩展抽象类的类中,我添加了相同的方法。我无法创建新的方法来解决我的问题,因为该方法在此特定CMS系统的多个版本上有所不同。

我想通过创建覆盖禁用或跳过的部分在所有这些不同的版本中都是相同的。

我已经阅读了PHP手册并找到了PECL apd函数override_function。

不能使用PECL,因为不是每个人都会使用我想要创建的插件,而是为PHP安装了PECL扩展。

这是我到目前为止所做的:

public function validateOrder($id_cart, $id_order_state, $amount_paid, $payment_method = 'Unknown',
        $message = null, $extra_vars = array(), $currency_special = null, $dont_touch_amount = false,
        $secure_key = false, Shop $shop = null)
    {

//This is the part that has to be disabled
if (Validate::isEmail($this->context->customer->email)) {
                            Mail::Send(
                                (int)$order->id_lang,
                                'order_conf',
                                Mail::l('Order confirmation', (int)$order->id_lang),
                                $data,
                                $this->context->customer->email,
                                $this->context->customer->firstname.' '.$this->context->customer->lastname,
                                null,
                                null,
                                $file_attachement,
                                null, _PS_MAIL_DIR_, false, (int)$order->id_shop
                            );
          }

        parent::validateOrder(
            $id_cart,
            $id_order_state,
            $amount_paid,
            $payment_method,
            $message,
            $extra_vars,
            $currency_special,
            $dont_touch_amount,
            $secure_key,
            $shop
        );

    } 

在下面你会找到原始方法的最后一部分:

                    $data = array(
                    '{firstname}' => $this->context->customer->firstname,
                    '{lastname}' => $this->context->customer->lastname,
                    '{email}' => $this->context->customer->email,
                    '{delivery_block_txt}' => $this->_getFormatedAddress($delivery, "\n"),
                    '{invoice_block_txt}' => $this->_getFormatedAddress($invoice, "\n"),
                    '{delivery_block_html}' => $this->_getFormatedAddress($delivery, '<br />', array(
                        'firstname'    => '<span style="font-weight:bold;">%s</span>',
                        'lastname'    => '<span style="font-weight:bold;">%s</span>'
                    )),
                    '{invoice_block_html}' => $this->_getFormatedAddress($invoice, '<br />', array(
                            'firstname'    => '<span style="font-weight:bold;">%s</span>',
                            'lastname'    => '<span style="font-weight:bold;">%s</span>'
                    )),
                    '{delivery_company}' => $delivery->company,
                    '{delivery_firstname}' => $delivery->firstname,
                    '{delivery_lastname}' => $delivery->lastname,
                    '{delivery_address1}' => $delivery->address1,
                    '{delivery_address2}' => $delivery->address2,
                    '{delivery_city}' => $delivery->city,
                    '{delivery_postal_code}' => $delivery->postcode,
                    '{delivery_country}' => $delivery->country,
                    '{delivery_state}' => $delivery->id_state ? $delivery_state->name : '',
                    '{delivery_phone}' => ($delivery->phone) ? $delivery->phone : $delivery->phone_mobile,
                    '{delivery_other}' => $delivery->other,
                    '{invoice_company}' => $invoice->company,
                    '{invoice_vat_number}' => $invoice->vat_number,
                    '{invoice_firstname}' => $invoice->firstname,
                    '{invoice_lastname}' => $invoice->lastname,
                    '{invoice_address2}' => $invoice->address2,
                    '{invoice_address1}' => $invoice->address1,
                    '{invoice_city}' => $invoice->city,
                    '{invoice_postal_code}' => $invoice->postcode,
                    '{invoice_country}' => $invoice->country,
                    '{invoice_state}' => $invoice->id_state ? $invoice_state->name : '',
                    '{invoice_phone}' => ($invoice->phone) ? $invoice->phone : $invoice->phone_mobile,
                    '{invoice_other}' => $invoice->other,
                    '{order_name}' => $order->getUniqReference(),
                    '{date}' => Tools::displayDate(date('Y-m-d H:i:s'), null, 1),
                    '{carrier}' => ($virtual_product || !isset($carrier->name)) ? Tools::displayError('No carrier') : $carrier->name,
                    '{payment}' => Tools::substr($order->payment, 0, 32),
                    '{products}' => $product_list_html,
                    '{products_txt}' => $product_list_txt,
                    '{discounts}' => $cart_rules_list_html,
                    '{discounts_txt}' => $cart_rules_list_txt,
                    '{total_paid}' => Tools::displayPrice($order->total_paid, $this->context->currency, false),
                    '{total_products}' => Tools::displayPrice(Product::getTaxCalculationMethod() == PS_TAX_EXC ? $order->total_products : $order->total_products_wt, $this->context->currency, false),
                    '{total_discounts}' => Tools::displayPrice($order->total_discounts, $this->context->currency, false),
                    '{total_shipping}' => Tools::displayPrice($order->total_shipping, $this->context->currency, false),
                    '{total_wrapping}' => Tools::displayPrice($order->total_wrapping, $this->context->currency, false),
                    '{total_tax_paid}' => Tools::displayPrice(($order->total_products_wt - $order->total_products) + ($order->total_shipping_tax_incl - $order->total_shipping_tax_excl), $this->context->currency, false));

                    if (is_array($extra_vars)) {
                        $data = array_merge($data, $extra_vars);
                    }

                    // Join PDF invoice
                    if ((int)Configuration::get('PS_INVOICE') && $order_status->invoice && $order->invoice_number) {
                        $order_invoice_list = $order->getInvoicesCollection();
                        Hook::exec('actionPDFInvoiceRender', array('order_invoice_list' => $order_invoice_list));
                        $pdf = new PDF($order_invoice_list, PDF::TEMPLATE_INVOICE, $this->context->smarty);
                        $file_attachement['content'] = $pdf->render(false);
                        $file_attachement['name'] = Configuration::get('PS_INVOICE_PREFIX', (int)$order->id_lang, null, $order->id_shop).sprintf('%06d', $order->invoice_number).'.pdf';
                        $file_attachement['mime'] = 'application/pdf';
                    } else {
                        $file_attachement = null;
                    }

                    if (self::DEBUG_MODE) {
                        PrestaShopLogger::addLog('PaymentModule::validateOrder - Mail is about to be sent', 1, null, 'Cart', (int)$id_cart, true);
                    }

                    if (Validate::isEmail($this->context->customer->email)) {
                        Mail::Send(
                            (int)$order->id_lang,
                            'order_conf',
                            Mail::l('Order confirmation', (int)$order->id_lang),
                            $data,
                            $this->context->customer->email,
                            $this->context->customer->firstname.' '.$this->context->customer->lastname,
                            null,
                            null,
                            $file_attachement,
                            null, _PS_MAIL_DIR_, false, (int)$order->id_shop
                        );
                    }
                }

                // updates stock in shops
                if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) {
                    $product_list = $order->getProducts();
                    foreach ($product_list as $product) {
                        // if the available quantities depends on the physical stock
                        if (StockAvailable::dependsOnStock($product['product_id'])) {
                            // synchronizes
                            StockAvailable::synchronize($product['product_id'], $order->id_shop);
                        }
                    }
                }

                $order->updateOrderDetailTax();
            } else {
                $error = Tools::displayError('Order creation failed');
                PrestaShopLogger::addLog($error, 4, '0000002', 'Cart', intval($order->id_cart));
                die($error);
            }
        } // End foreach $order_detail_list

        // Use the last order as currentOrder
        if (isset($order) && $order->id) {
            $this->currentOrder = (int)$order->id;
        }

        if (self::DEBUG_MODE) {
            PrestaShopLogger::addLog('PaymentModule::validateOrder - End of validateOrder', 1, null, 'Cart', (int)$id_cart, true);
        }

        return true;
    } else {
        $error = Tools::displayError('Cart cannot be loaded or an order has already been placed using this cart');
        PrestaShopLogger::addLog($error, 4, '0000001', 'Cart', intval($this->context->cart->id));
        die($error);
    }
}

你会发现我试图在方法的底部几乎跳过的if语句。

如您所见,其中一个参数$ extra_vars = array()与函数内的$ data数组合并。有没有办法以这种方式跳过if语句?因为我对如何解决这个问题没有任何想法。

1 个答案:

答案 0 :(得分:1)

如果$this->context->customer->email是可写的,您可以执行以下操作:

<?php

//...

$customerEmail = $this->context->customer->email;
$this->context->customer->email = null;

parent::validateOrder(
    $id_cart,
    $id_order_state,
    $amount_paid,
    $payment_method,
    $message,
    $extra_vars,
    $currency_special,
    $dont_touch_amount,
    $secure_key,
    $shop
);

$this->context->customer->email = $customerEmail;   

我不建议这样做,但如果你绝望,它可能会有用。