i’ve created a custom plugin add-on for woocommerce in my local.
and i add some custom endpoints like below:
<code>function add_custom_invoice_endpoints()
{
add_rewrite_endpoint('invoices', EP_ROOT | EP_PAGES);
}
add_action('init', 'add_custom_invoice_endpoints');
function custom_invoice_query_vars($vars)
{
$vars[] = 'invoices';
return $vars;
}
add_filter('query_vars', 'custom_invoice_query_vars');
</code>
<code>function add_custom_invoice_endpoints()
{
add_rewrite_endpoint('invoices', EP_ROOT | EP_PAGES);
}
add_action('init', 'add_custom_invoice_endpoints');
function custom_invoice_query_vars($vars)
{
$vars[] = 'invoices';
return $vars;
}
add_filter('query_vars', 'custom_invoice_query_vars');
</code>
function add_custom_invoice_endpoints()
{
add_rewrite_endpoint('invoices', EP_ROOT | EP_PAGES);
}
add_action('init', 'add_custom_invoice_endpoints');
function custom_invoice_query_vars($vars)
{
$vars[] = 'invoices';
return $vars;
}
add_filter('query_vars', 'custom_invoice_query_vars');
and add an extra item in woocommerce customer panel as invoices:
<code>function custom_invoice_menu_items($items)
{
// Insert "Invoices" after "Orders"
$new_items = array();
foreach ($items as $key => $item) {
$new_items[$key] = $item;
if ($key === 'orders') {
$new_items['invoices'] = __('فاکتورهای فروش', 'woocommerce');
}
}
return $new_items;
}
add_filter('woocommerce_account_menu_items', 'custom_invoice_menu_items');
</code>
<code>function custom_invoice_menu_items($items)
{
// Insert "Invoices" after "Orders"
$new_items = array();
foreach ($items as $key => $item) {
$new_items[$key] = $item;
if ($key === 'orders') {
$new_items['invoices'] = __('فاکتورهای فروش', 'woocommerce');
}
}
return $new_items;
}
add_filter('woocommerce_account_menu_items', 'custom_invoice_menu_items');
</code>
function custom_invoice_menu_items($items)
{
// Insert "Invoices" after "Orders"
$new_items = array();
foreach ($items as $key => $item) {
$new_items[$key] = $item;
if ($key === 'orders') {
$new_items['invoices'] = __('فاکتورهای فروش', 'woocommerce');
}
}
return $new_items;
}
add_filter('woocommerce_account_menu_items', 'custom_invoice_menu_items');
now for invoices page i’ve created some content:
<code>function custom_invoice_endpoint_content()
{
error_log('custom_invoice_endpoint_content() called at ' . current_time('mysql'));
$customer_orders = wc_get_orders(
array(
'customer_id' => get_current_user_id(),
'status' => array('completed', 'processing', 'on-hold', 'pending'),
'limit' => -1,
)
);
if ($customer_orders) {
echo '<table class="ta-woocommerce-orders-table">';
echo '<thead><tr><th>شماره سفارش</th><th>تاریخ</th><th>گزینهها</th></tr></thead>';
echo '<tbody>';
foreach ($customer_orders as $order) {
$order_id = $order->get_id();
$order_date = $order->get_date_created()->date('Y-m-d');
echo '<tr>';
echo '<td>' . esc_html($order_id) . '</td>';
echo '<td>' . esc_html($order_date) . '</td>';
echo '<td>';
echo '<a class="btn btn-success ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('viewinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">مشاهده</a>';
echo '<a class="btn btn-primary ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('editinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">ویرایش</a>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
} else {
echo '<p>فاکتوری یافت نشد.</p>';
}
}
add_action('woocommerce_account_invoices_endpoint', 'custom_invoice_endpoint_content');
</code>
<code>function custom_invoice_endpoint_content()
{
error_log('custom_invoice_endpoint_content() called at ' . current_time('mysql'));
$customer_orders = wc_get_orders(
array(
'customer_id' => get_current_user_id(),
'status' => array('completed', 'processing', 'on-hold', 'pending'),
'limit' => -1,
)
);
if ($customer_orders) {
echo '<table class="ta-woocommerce-orders-table">';
echo '<thead><tr><th>شماره سفارش</th><th>تاریخ</th><th>گزینهها</th></tr></thead>';
echo '<tbody>';
foreach ($customer_orders as $order) {
$order_id = $order->get_id();
$order_date = $order->get_date_created()->date('Y-m-d');
echo '<tr>';
echo '<td>' . esc_html($order_id) . '</td>';
echo '<td>' . esc_html($order_date) . '</td>';
echo '<td>';
echo '<a class="btn btn-success ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('viewinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">مشاهده</a>';
echo '<a class="btn btn-primary ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('editinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">ویرایش</a>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
} else {
echo '<p>فاکتوری یافت نشد.</p>';
}
}
add_action('woocommerce_account_invoices_endpoint', 'custom_invoice_endpoint_content');
</code>
function custom_invoice_endpoint_content()
{
error_log('custom_invoice_endpoint_content() called at ' . current_time('mysql'));
$customer_orders = wc_get_orders(
array(
'customer_id' => get_current_user_id(),
'status' => array('completed', 'processing', 'on-hold', 'pending'),
'limit' => -1,
)
);
if ($customer_orders) {
echo '<table class="ta-woocommerce-orders-table">';
echo '<thead><tr><th>شماره سفارش</th><th>تاریخ</th><th>گزینهها</th></tr></thead>';
echo '<tbody>';
foreach ($customer_orders as $order) {
$order_id = $order->get_id();
$order_date = $order->get_date_created()->date('Y-m-d');
echo '<tr>';
echo '<td>' . esc_html($order_id) . '</td>';
echo '<td>' . esc_html($order_date) . '</td>';
echo '<td>';
echo '<a class="btn btn-success ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('viewinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">مشاهده</a>';
echo '<a class="btn btn-primary ta-no-dec ta-l-m" href="' . esc_url(wc_get_endpoint_url('editinvoice', $order_id, wc_get_page_permalink('myaccount'))) . '">ویرایش</a>';
echo '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
} else {
echo '<p>فاکتوری یافت نشد.</p>';
}
}
add_action('woocommerce_account_invoices_endpoint', 'custom_invoice_endpoint_content');
and all codes in my custom_invoice_endpoint_content executes twice, once in header and once in main page content.
like this:
enter image description here