I'm completely new to PHP and WooCommerce, so this may be a very simple question, however I am trying to have the 'WooCommerce PDF Watermark' plugin print the item quantity from an order on my WordPress site. I cannot get the code right for this. The documentation that comes with the plugin provides the following example that is given below of how to add a template tag to the plugin. Any assistance with this would be hugely appreciated!
function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {
// Look for {product_title} in text and replace it with the product title
$parsed_text = str_replace( '{product_title}', $product->get_title(), $parsed_text );
return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags' );
All order related details in $order variable.
function wc_pdf_watermark_extend_template_tags( $parsed_text, $unparsed_text, $order, $product ) {
var_dump($order);
wp_die();
return $parsed_text;
}
add_filter( 'woocommerce_pdf_watermark_parse_template_tags', 'wc_pdf_watermark_extend_template_tags' );
Once you get a variable dump you will get all details extract and return as parsed_text.
Related
I'd like to print custom taxonomy, season on Woocommerce admin quick order preview. I found following code that prints the meta data in Stackoverflow but can't find one that print custom taxonomy.
Any help would be appreciated.
// Add custom order meta data to make it accessible in Order preview template
add_filter( 'woocommerce_admin_order_preview_get_order_details',
'admin_order_preview_add_custom_meta_data', 10, 2 );
function admin_order_preview_add_custom_meta_data( $data, $order ) {
// Replace '_custom_meta_key' by the correct postmeta key
if( $custom_value = $order->get_meta('pa_season') )
$data['custom_key'] = $custom_value; // <= Store the value in the data array.
return $data;
}
// Display custom values in Order preview
add_action( 'woocommerce_admin_order_preview_end', 'custom_display_order_data_in_admin' );
function custom_display_order_data_in_admin(){
// Call the stored value and display it
echo '<div>Value: {{data.custom_key}}</div><br>';
}
I'm trying to insert a shortcode in the woocommerce view-order.php template but it doesn't work.
This is the reference template: https://woocommerce.github.io/code-reference/files/woocommerce-templates-myaccount-view-order.html
In the functions.php file I wrote the following code:
add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
$customer_id = get_current_user_id();
$order = new WC_Order( $order_id ); //I think this is the problem I don't know if that's right
return $order->get_id();
}
The shortcode shows the number 0, so I'm not getting the order id which in my case is 40001.
To structure the code I followed these references:
How to create a shortcode for Woocommerce view-order template?
Create Woocommerce shortcodes with order details
Maybe I should change the line that affects the $order part, but I'm not sure.
I don't understand where I'm wrong, does anyone kindly have a suggestion?
Have found a solution.
After some research I came across this post: How can I get the order ID from the order Key in WooCommerce?
After that I tried to insert this $order_id = absint( get_query_var('view-order') ); Everything worked fine. Here is the solution for anyone in the same situation.
add_shortcode( 'order_view_id' , 'order_view_01' );
function order_view_01(){
// Get Order ID
$order_id = absint( get_query_var('view-order') );
// Then you can get the order object
$order = new WC_Order( $order_id );
// What you want to see, in my case the order ID
return $order->get_id();
}
Here you can find everything you are interested in showing via shortcode: https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
Remember, I wrote the code in functions.php. This allows me to insert the [order_view_id] shortcode inside the woocommerce view-order.php template.
In a site that I am developing in woocommerce I needed to be able to create 6 price lists with fixed prices for each product and to be able to apply a global discount in percentage for the price list assigned to the customer.
For the 6 price lists i've managed it through the plugin "Role Based Price For WooCommerce"
For the discount per client part i've created a little plugin. Here the code
function get_price_divider() {
$user_id = get_current_user_id();
$perc = get_user_meta( $user_id, 'sconto_cliente', true );
$sconto = ((int)$perc);
return $sconto;
}
add_filter('wc_rbp_product_selling_price', 'custom_price', 99, 2 );
function custom_price( $price, $product ) {
return ((int)$price)-((((int)$price)/100)*get_price_divider());
}
Everything works fine on the product page. It handle the hook 'wc_rbp_product_selling_price' correctly.
However, I need to pass this price to another plugin with which I made the "tool finder" for the products on sale.
The authors of the plugin (LSCF) give me the option to Create New Data Tag as specified here: https://pixolette.com/docs/lscf/custom-template/create-new-data-tag/
So i should display in their html template with this code
<div class="customdata">
Prezzo: {{post.custom_price}}
</div>
I've created the code below to do this but output always 0
add_action( 'lscf_custom_tags', 'lscf_custom_tags_function' );
function lscf_custom_tags_function( &$args ) {
global $price, $product;
$post_id = (int) $args['ID'];
$output = apply_filters( 'wc_rbp_product_selling_price', $price, $product );
$args['custom_price'] = $output;
}
What's wrong?
I'm not an expert but maybe you should reduce priority argument in the
add_filter('wc_rbp_product_selling_price', 'custom_price', 99, 2 );
It probably doesn't fire in time to adjust the price.
I am working on salient theme for my E-commerce website. I want to show "B" after price in woocommerce product page.
The page displays like "$99,99"
I want to make it displays like this: "$99,99 TEXT"
I have the code for it :
function change_product_price( $price ) {
$price .= ' TEXT';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
Now I want to know where to add the code in woo-commerce directory. Any help would be appreciated.
thanks
Add this code inside functions.php of your theme:
function cw_change_product_price_display( $price ) {
$price .= ' TEXT';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
The name of the function you're calling in add_filter isn't the same as the name of your actual function. It seems you've forgotten the _display in the function name.
In the following code, the function is name cw_change_product_price_display and cw_change_product_price_display is called in add_filter
function cw_change_product_price_display( $price ) {
$price .= ' TEXT';
return $price;
}
add_filter( 'woocommerce_get_price_html', 'cw_change_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'cw_change_product_price_display' );
Tested for woocommerce 6.x.x - 2022
IMPORTANT: If you have enabled the tax calculation (woocommerce -> Settings -> Enable Tax) inside woocommerce, you have the option from the admin panel to add whatever you want after the price (suffix). This option can be found inside the Admin dashboard and under the woocommerce -> Settings -> Tax.
The code snippets provided below can be added inside the file functions.php of your child's theme folder. I would strongly recommend you to use a child theme (if provided by the theme or create one) so that when you will update your theme, none of these changes will get erased.
Woocommerce provides a filter, for quite some time now, that you can add a price suffix, and that's what you want in your case.
add_filter( 'woocommerce_get_price_suffix', 'add_some_text_after_price', 99, 4 );
function add_some_text_after_price ($html, $product, $price, $qty){
$html .= ' TEXT'; //You can write whatever you want here. Just stay inside the ' '
return $html;
}
Also, as the other answers suggested, the function's name that is inside your filter is not the same as the function's name that you do your action.
The name of your function: change_product_price
And inside your filters that you want to add this action, you call: cw_change_product_price_display
So nothing will happen and nothing will work, like the function, you call I nowhere to be found.
Also except for adding a suffix, you can also add a prefix using one of the filters that you already use (in case someone needs something like that)
add_filter( 'woocommerce_get_price_html', 'my_function', 99, 2 );)
function my_function( $price, $product ){
$price = 'Text ' . $price;
return $price;
}
General Knowledge
The 99 inside the filters is the priority number that the filter will start, before any other filter. That means that if you want to start some filters before others, then you can change the priority. The second number is the number of the arguments ($price, $html, ...) that you put inside your function.
Both numbers are needed in most cases.
I've got a WooCommerce store set up with products in several categories and using the Print Invoice & Packing List plugin to generate packing lists for orders.
The plugin orders products by default based on the first category they are listed in, in alphabetical order.
I'm trying to set the plugin to ignore a preset list of categories by ID, so all products will still display, but only listed under the category I've allowed instead of the first category in alphabetical order.
I've read through the Invoice & Packing List dev reference grabbed a SkyVerge snippet and created a custom plugin which will disable categories completely, but can't for the life of me figure out how the WooCommerce hooks should work to ignore certain categories.
This is what I have in the custom plguin at the moment, which simply removes the categories from packing lists:
if ( ! defined( 'ABSPATH' ) )
add_filter( 'wc_pip_packing_list_group_items_by_category', '__return_false' );
function sv_wc_pip_packing_list_grouping( $group_items, $order_id, $document_type ) {
if ( 'pick-list' !== $document_type ) {
return $group_items;
}
$order = wc_get_order( $order_id );
if ( ! $order->is_paid() ) {
return false;
}
return $group_items;
}
add_filter( 'wc_pip_packing_list_group_items_by_category', 'sv_wc_pip_packing_list_grouping', 10, 3 );
Appreciate any help to identify how to define item categories in this function.
i think you could try this, section Document Filters from your documentation link:
wc_pip_packing_list_exclude_item
Params: $exclude, $product, $item, $item_data
Return: (bool)
Filters if an order item should be excluded from the packing list.
in your filter function, you should be able to test the category from $product, or at least get relevant info to query it. Then you just return true or false following the case