Thread Starter
ele
(@luishgc93)
i try this code:
<?php
/*
Plugin Name: WooCommerce PDF Invoices & Packing Slips Desactivar PDF
Plugin URI: https://www.advancedcustomfields.com
Description: Desactivamos el generador de Facturas en PDF para los productos que NO son de OAT
Version: 1.0
Author: Luishgc93
Author URI: https://www.oatobservatorio.com
*/
add_filter( 'wpo_wcpdf_custom_attachment_condition', 'wpo_wcpdf_exclude_products', 100, 4 );
function wpo_wcpdf_exclude_products( $condition, $order, $status, $template_type ) {
// only apply check on invoices
if ($template_type != 'invoice') {
return $condition;
}
// define product IDs which shouldn't get an invoice here
$no_invoice_products = array( 1573, 2217 );
// loop through order items and cancel attachment if one of the products present
$items = $order->get_items();
foreach ($items as $item_id => $item) {
if (in_array($item->get_product_id(), $no_invoice_products)) {
// matching product, don't attach invoice
return false;
}
}
// if we got here, there were no matching products
return $condition;
}
but on the order , on panel of control, the order is generated.
Hello @luishgc93
Please add the following code snippet to your theme functions.php file (don’t forget to add the product IDs):
add_filter('wpo_wcpdf_document_is_allowed', 'disallow_document_on_certain_products', 10, 2);
function disallow_document_on_certain_products( $allowed, $document )
{
$products_ids = array(16, 23); // add here your product IDs
if ( !empty($order = $document->order) && $document->get_type() == 'invoice' ) {
foreach( $order->get_items() as $item_id => $item ) {
$product_id = $item['variation_id'] != 0 ? $item['variation_id'] : $item['product_id'];
if( in_array($product_id, $products_ids) ) {
$allowed = false;
}
}
}
return $allowed;
}
If you never worked with action/filters please read our documentation page: How to use filters