是否可以在不创建模块的情况下通过挂钩在 Prestashop 中执行功能?
Is it possible execute a function in Prestashop via hooks without creating a module?
我创建了这个测试函数(我们称之为 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 函数。
这可能吗?我应该把它放在哪里?
我不这么认为。不是以正确的方式。
但是您可以在 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
}
}
您也可以覆盖任何模块来执行此操作,或挂钩执行程序并检查它是否正在尝试执行 hookValidateOrder 然后放置您的代码。
如果您确实创建了覆盖,请不要忘记在之后删除(或重命名)文件 cache/class_index。php,以便为您创建新的覆盖。
我创建了这个测试函数(我们称之为 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 函数。
这可能吗?我应该把它放在哪里?
我不这么认为。不是以正确的方式。 但是您可以在 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
}
}
您也可以覆盖任何模块来执行此操作,或挂钩执行程序并检查它是否正在尝试执行 hookValidateOrder 然后放置您的代码。
如果您确实创建了覆盖,请不要忘记在之后删除(或重命名)文件 cache/class_index。php,以便为您创建新的覆盖。