我正在尝试创建一个按钮,该按钮将在woocommerce的客户订单页面中执行我的FPDF脚本,以便客户可以根据按钮旁边的特定订单生成pdf。
我在我的functions.php文件中添加了以下内容,为按钮创建了新列,并添加了我希望按钮也执行的功能。
我对如何添加一个新按钮以将功能执行到myaccount / orders.php文件感到困惑。我在过去两天的大部分时间里都在尝试寻找解决方案-因此,任何帮助将不胜感激!
// END ENQUEUE PARENT ACTION
add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
$ordered_columns['order-downloads'] = __( 'Downloads', 'woocommerce' ); // <== New column
return $ordered_columns;
}
调用FPDF的功能
function exFDPI () {
require_once("/file/directory/FPDF.php");
}
答案 0 :(得分:1)
您可以通过按钮传递参数,然后在init
钩子上处理请求。
add_filter( 'woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1 );
function add_custom_account_orders_column( $columns ) {
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
$ordered_columns['order-downloads'] = __('Downloads', 'woocommerce'); // <== New column
return $ordered_columns;
}
function exFDPI () {
if(isset($_POST['order_id_for_pdf'])) {
require_once("/file/directory/FPDF.php");
//
return;
}
else {
return;
}
}
add_action( 'init', 'exFDPI' );
不要忘记添加return;
更新
按照@kashalo的回答向按钮添加内容。
//Insert the content into the new column
function pdf_column_content($order)
{
?>
<div id="content">
<form action="<?php esc_url( $_SERVER['REQUEST_URI'] ) ?>" method="post">
<input type="hidden" name="order_id_for_pdf" value="<?php echo $order->get_order_number() ?>">
<input type="submit" value="Downloads">
</form>
</div>
<?php
}
add_action('woocommerce_my_account_my_orders_column_order-downloads', 'pdf_column_content');
答案 1 :(得分:1)
首先,您会丢失新列中的内容,因此,请按以下步骤操作:
第1步:添加我们的列
add_filter('woocommerce_account_orders_columns', 'add_custom_account_orders_column', 10, 1);
function add_custom_account_orders_column($columns)
{
$ordered_columns = array();
// Inserting a new column in a specific location
$ordered_columns['order-number'] = $columns['order-number'];
$ordered_columns['order-date'] = $columns['order-date'];
$ordered_columns['order-status'] = $columns['order-status'];
$ordered_columns['order-total'] = $columns['order-total'];
$ordered_columns['order-actions'] = $columns['order-actions'];
$ordered_columns['order-downloads'] = __('Downloads', 'woocommerce'); // <== New column
return $ordered_columns;
}
第2步:将表单内容添加到新列中
//Insert the content into the new column
function pdf_column_content($order)
{
?>
<form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
<?php wp_nonce_field('pdf_temp');?> //Security Check
<input type="hidden" name="order_id" value="<?php echo $order->get_order_number() ?>">
<input type="hidden" name="action" value="pdf_temp">
<input type="submit" value="Submit">
</form>
<?php
}
add_action('woocommerce_my_account_my_orders_column_order-downloads', 'pdf_column_content');
第3步最终步骤:以WordPress方式添加我们的操作
//when the user click submit then we will call our template
function exFDPI()
{
$retrieved_nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($retrieved_nonce, 'pdf_temp')) { //if passed the security check proceed
wp_die('Failed security check');
} else {
$order_id = $_POST['order_id'];
echo $order_id;
require_once "/file/directory/FPDF.php";
}
}
add_action('admin_post_nopriv_pdf_temp', 'exFDPI');
add_action('admin_post_pdf_temp', 'exFDPI');
当然,完整的代码应该包含在您的主题functions.php
代码经过测试。