• Resolved poldacchio

    (@poldacchio)


    Hello to all,
    I need a help for a problem: I need to add some html texts at the bottom of the invoices, but they must appear only under some restrictions, for example:

    if product category is “Eventi” and buyer user role is “socio” -> TEXT1
    if product category is “Eventi” and buyer user roles are all but not “socio” -> TEXT2
    If product Category is “Corsi” and buyer user role is “socio” -> TEXT3
    If product category is “Corsi” and buyer user roles are all but not “socio” -> TEXT4

    Is that possible?
    thanks a lot

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ewout

    (@pomegranate)

    Yes that is possible, but this is quite a complex conditional. Also note that a product can have multiple categories and an order can have multiple products, so you will always need to loop through the product list of the order.

    Here’s how you can find the user role of a customer:

    
    if ( $user = $order->get_user() ) {
    	$allowed_customer_roles = array( 'wholesale_customer', 'customer' );
    	foreach ( $allowed_customer_roles as $customer_role ) {
    		if ( in_array( $customer_role, (array) $user->roles ) ) {
    			# we have a matching role
    		}
    	}
    }
    

    and here’s an example of how to create an array that holds all the categories of products in an order:

    
    $order_cats = array();
    foreach ($order->get_items() as $item_id => $item) {
        $terms = get_the_terms( $item['product_id'], 'product_cat' );
        if (empty($terms)) {
            continue;
        }
        foreach ($terms as $key => $term) {
            $order_cats[$term->term_id] = $term->name;
        }
    }
    

    Using these snippets you can print a text using one of the action hooks in the template.

    Thread Starter poldacchio

    (@poldacchio)

    Fortunately in my ecommerce products can have only 1 category and the cart is “limited to 1 product only” if a product of the “eventi” category is added (and automatically deletes other products if there were)
    Known this, could you suggest me a code that does what I need?
    Many thanks in advance

    Plugin Contributor Ewout

    (@pomegranate)

    The above code examples will deal with that regardless of whether there is 1 or more product in the cart and whether the products have 1 or more categories. If you need more help with this, I suggest hiring a developer, this is quite advanced stuff.

    Thread Starter poldacchio

    (@poldacchio)

    thanks very much, I knew it wasn’t so simple…
    Another question: I have this code you wrote me some days ago, this code adds a text in the invoice pdfs if the tax calculation equals to zero:

    add_action( ‘wpo_wcpdf_after_order_details’, ‘wpo_wcpdf_tax_exempt’, 10, 2 );
    function wpo_wcpdf_tax_exempt( $document_type, $order ) {
    // only in financial documents
    if ( !in_array( $document_type, array(‘invoice’, ‘proforma’, ‘credit-note’) ) ) {
    return;
    }

    // check if any tax was charged
    if ( $order->get_total_tax() == 0 && $order->get_total() > 0 ) {
    ?>
    <div class=”tax-free”>
    TEXT TEXT TEXT TEXT
    </div>
    <?php
    }
    }

    what can I do to make the code do the same thing but by selecting the type of tax and not whit the calculation, I better explain:
    if “zero tax” is selected -> html text 1
    if “reduced tax” i selected -> html text 2
    Thaanks again

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add text by category and by user role’ is closed to new replies.