是否可以通过挂钩在Prestashop中执行函数而无需创建模块?

时间:2017-02-14 15:42:14

标签: php hook prestashop-1.6

我创建了这个测试函数(让我们称之为ABtest.php)来发送测试消息:

$host = "http://localhost:8080";
$id = "123";
$email = "mine.mail@gmail.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, "mine.mail:mypassword");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPAUTH, TRUE);

function send($host,$id,$email,$ch){
    curl_setopt($ch, CURLOPT_URL, $host . "/c/" . $id . "/your ID/");
    curl_setopt($ch, CURLOPT_POSTFIELDS, "to=" . $email . "&subject=Your ID ");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

    $output = curl_exec($ch);

    if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200 && curl_getinfo($ch, CURLINFO_HTTP_CODE) != 201) {
        printf("An error occured (HTTP %d): %sn", curl_getinfo($ch, CURLINFO_HTTP_CODE), $output);
    } else {
        printf("Success");
    }
}

我想将此代码放入我的钩子hookValidateOrder($params)中,如下所示:

public function hookValidateOrder($params)
{
    $id = $params['cart']->id;
    $host = "http://localhost:8080";
    $email = "mine.mail@gmail.com";

    // + the rest of my PHP function - see the 1st code block above    
}

问题在于我真的不知道在哪里放置代码。正如您所看到的,我不是在创建模块,我只想通过hookValidateOrder($params)钩子来执行我的PHP函数。

这可能吗?我在哪里放置它?

1 个答案:

答案 0 :(得分:0)

我不这么认为。不是以正确的方式。 但您可以在PaymentModule.php的覆盖文件夹中创建覆盖:

abstract class PaymentModule extends PaymentModuleCore
{
    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)
    {
          // do what you need before

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

         // do what you need after
    }
}

您也可以覆盖任何模块来执行它,或者执行钩子exec并检查它是否尝试执行hookValidateOrder然后放置代码。

如果您确实创建了覆盖,请不要忘记删除(或重命名)文件缓存/ class_index.php,以便为新索引编制索引。