Condition address shop
-
Hi! Its possible change the address shop details for the invoice generated based on the product selected?
- This topic was modified 3 years, 7 months ago by josede.
-
Hi @josede,
It isn’t quite clear in what way you are trying to modify the address so I’ll have to take a guess: I’ll assume you just want to add some information at the end.
The following code snippet will check whether or not there’s a certain item in the order based on its ID:
add_filter( 'wpo_wcpdf_after_shop_address', 'wpo_wcpdf_conditional_shop_address', 10, 2 ); function wpo_wcpdf_conditional_shop_address( $template_type, $order ) { // if order is empty, bail if ( empty($order) ) {return;} $conditional_id = '5'; foreach ( $order->get_items() as $item_id => $item ) { // check product ID $product_id = $item->get_product_id(); // change shop address if conditional product ID is found if ($product_id == $conditional_id) { echo 'More Shop Info'; return; } } }
The line “More Shop Info” is added at the end of the shop address if a product of ID ‘5’ – from
$conditional_id = 5
– is present.
You can find the Product IDs in the Products menu, by highlighting an item with your mouse pointer. ??If you haven’t used hooks before, please read this guide: https://docs.wpovernight.com/general/how-to-use-filters/
- This reply was modified 3 years, 7 months ago by Darren Peyou. Reason: additional instructions
Thx so much.
¨I’ll assume you just want to add some information at the end¨
Its not to add some information at the end.
¨wpo_wcpdf_after_shop_address¨
I dont want add more info. I want to remove the shop address and add a new one.Thx!
Hey @josede,
What is the nature of this new address you are trying to show?
– Is it an address that is saved in WooCommerce somehow, like a custom field added by you or some external plugin? If yes, what is the name of this custom field?
– If no, then you’ll have to enter this address manually. ??I want just a different address and company name for invoices that has a specific product.
If the product is A then “this address instead the default address”
Hey @josede,
You can use this snippet instead, including if you also need to change the shop name:
add_action( 'wpo_wcpdf_before_shop_name', 'wpo_wcpdf_conditional_shop_address', 999, 2 ); function wpo_wcpdf_conditional_shop_address( $template_type, $order ) { // if order is empty, bail if ( empty($order) ) {return;} $conditional_id = '74'; $document = wcpdf_get_document( $template_type, $order ); foreach ( $order->get_items() as $item_id => $item ) { // check product ID $product_id = $item->get_product_id(); // change shop address if conditional product ID is found if ($product_id == $conditional_id) { // hide default shop name & address with CSS echo '<style>' . '.shop-name, .shop-address {display: none;}' . '</style>'; // display new shop name echo '<b>' . 'Alt Shop Name' . '</b>'; //display new shop address echo '<div>' . 'Address Line 1' . //insert line break '<br>' . 'Address Line 2' . '</div>'; return; } } }
This snippet assumes that your shop address is written on 2 lines — see “Address Line 1” & “Address Line 2”.
Enter the alternate shop name where you see “Alt Shop Name”. ??- This reply was modified 3 years, 7 months ago by Darren Peyou. Reason: extra formating
This works for pdf generation as well right? Thx
I’m not sure what you mean. Have you tested the code? It fulfills the purpose on my end. ??
Darren sorry for my english. Your code works. But I was looking not by product ID, for category from the product.
If a products/products, with specific category. Based on the category’s product bought.
Hey @josede,
I have an update, rejoice!
If you need it to be a category that triggers the change instead, here’s the updated code:
add_action( 'wpo_wcpdf_before_shop_name', 'wpo_wcpdf_conditional_shop_address', 999, 2 ); function wpo_wcpdf_conditional_shop_address( $template_type, $order ) { // if order is empty, bail if ( empty($order) ) {return;} $conditional_category = 'tshirts'; $document = wcpdf_get_document( $template_type, $order ); foreach ( $order->get_items() as $item_id => $item ) { // get product ID $product_id = $item->get_product_id(); // get the terms $terms = get_the_terms($product_id, 'product_cat'); // define empty array to gather categories $categories = []; // populate the array of categories for ( $n=0; $n < count($terms); $n++ ) { array_push($categories, $terms[$n]->slug); } // change shop address if category is found if ( in_array( strtolower($conditional_category), $categories) ) { // hide default shop name & address with CSS echo '<style>' . '.shop-name, .shop-address {display: none;}' . '</style>'; // display new shop name echo '<b>' . 'Alt Shop Name' . '</b>'; //display new shop address echo '<div>' . 'Address Line 1' . //insert line break '<br>' . 'Address Line 2' . '</div>'; return; } } }
In this case, you will need to enter the slug of the category that you want to be the conditional category in the line
conditional_category = 'tshirts'
. ??- This reply was modified 3 years, 7 months ago by Darren Peyou.
Darren How can I change the prefix of the invoice based with the same conditions. (Your code is fine) Just I need to change the prefix when $conditional_category = ‘tshirts’.
Hi @josede,
Since this is now something different you are talking about, could you create a new thread? Sorry if this sounds dismissive, it’s just to keep some order in the forums. ??
In that new thread you create, could you specify whether you are talking about invoice number or PDF filename.
See you on the new thread!- This reply was modified 3 years, 7 months ago by Darren Peyou.
- The topic ‘Condition address shop’ is closed to new replies.