Skip to content

Instantly share code, notes, and snippets.

@Spreeuw
Last active June 23, 2020 06:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spreeuw/2e4c5dfa97abfca6f6cc to your computer and use it in GitHub Desktop.
Save Spreeuw/2e4c5dfa97abfca6f6cc to your computer and use it in GitHub Desktop.
Dokan PDF Invoices
<?php
/*******************************
** DOKAN PDF INVOICE FILTERS **
*******************************/
/**
* Replace the main shop name with "Store Info" (actual shop name listed below)
*/
add_filter( 'wpo_wcpdf_shop_name', 'wpo_wcpdf_add_dokan_shop_name', 10, 1 );
function wpo_wcpdf_add_dokan_shop_name ( $shop_name ) {
return __( 'Store Info', 'dokan-invoice' );
}
/**
* Use shop details for seller(s) below the main shop address
*/
add_filter( 'wpo_wcpdf_shop_address', 'wpo_wcpdf_add_dokan_shop_details', 10, 1 );
function wpo_wcpdf_add_dokan_shop_details ( $shop_address ) {
global $wpo_wcpdf;
$stores = array();
// does this also work for multiple sellers for an order?
// $seller_id = dokan_get_seller_id_by_order( $order_id );
// loop through items to get list of sellers for this order
$items = $wpo_wcpdf->export->order->get_items();
foreach ( $items as $item ) {
$store_details = array();
$seller_id = get_post_field( 'post_author', $item[ 'product_id' ] );
$store_info = dokan_get_store_info( $seller_id );
// show store name
// $store_details[] = sprintf('<h3>%s</h3>', __( 'Store Info', 'dokan-invoice' ));
$store_details[] = sprintf('<div class="shop-name"><h3>%s</h3></div>', $store_info['store_name']);
// show email if enabled
if ( $store_info[ 'show_email' ] != 'off' && $store_info[ 'show_email' ] != 'no' ) {
$store_email = get_user_meta( $seller_id, 'billing_email', true );
$store_details[] = sprintf('<div class="shop-email"><i>%s</i></div>', $store_email );
}
// show address
$store_details[] = sprintf( '<div class="shop-address">%s</div>', dokan_get_seller_address( $seller_id ) );
// show phone if enabled
if ( !empty( $store_info[ 'phone' ] ) ) {
$store_details[] = sprintf( '<div class="shop-phone">%s%s</div>', __( 'Tel : ', 'dokan-invoice' ), $store_info[ 'phone' ] );
}
$stores[$seller_id] = implode("\n", $store_details);
}
return $shop_address . implode("\n", $stores);
}
/**
* Check on user privileges when printing invoices or packing slips
*/
add_filter( 'wpo_wcpdf_check_privs', 'wpo_wcpdf_dokan_privs', 50, 2 );
function wpo_wcpdf_dokan_privs ( $not_allowed, $order_ids ) {
// check if user is seller
if ( $not_allowed && in_array('seller', $GLOBALS['current_user']->roles) ) {
foreach ($order_ids as $order_id) {
// get seller_id
$seller_id = dokan_get_seller_id_by_order( $order_id );
// loop through items to get list of sellers for this order
$order_sellers = array();
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$item_seller = get_post_field( 'post_author', $item[ 'product_id' ] );
// check if item is from this seller
if ( $item_seller != $seller_id ) {
return true; // not allowed!
}
}
}
// if we got here, that means the user is a seller and all orders and items belong to this seller
return false; // allowed!
} else {
return $not_allowed; // preserve original check result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment