Add hidden meta data to product Woocommerce - php

I've added a custom field to my product for admin only meta data, I have hidden it using CSS.
However it still shows up in emails. Is there any way I can create a custom field where the meta data only shows up in the admin orders page?

You could try to use woocommerce_email_order_meta_fields filter hook to remove this custom field from order metadata, using unset() php function this way:
add_filter( 'woocommerce_email_order_meta_fields', 'wc_email_order_meta_remove_custom_field', 10, 3 );
function wc_email_order_meta_remove_custom_field( $fields, $sent_to_admin, $order ) {
// Replace HERE 'meta_key' by your custom field meta key or slug.
unset($fields['meta_key']);
return $fields;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work, but not sure as you don't provide any information and code related to the way you have set this custom field.

Related

How To Use do_shortcode with WooCommerce One Page Checkout

I'm trying to use the WordPress do_shortcode function for the WooCommerce One Page Checkout Plugin which uses shortcode like this: [woocommerce_one_page_checkout template="product-table" product_ids="product, ids, here"]. It seems like I can only use this shortcode IF it's in the content editor and won't allow me to add this to a page template using the do_shortcode function.
Their documentation here says:
If you wish to display the One Page Checkout Shortcode using WordPress’ do_shortcode() function instead of including the shortcode in a post or page’s content, you will also need to attach custom code to the 'is_wcopc_checkout' filter and make sure a boolean true value is returned.
So I tried adding the following to the functions.php file:
add_filter( 'is_wcopc_checkout', function(){ return true; } );
and it didn't seem to do the trick.
I also tried:
add_filter( 'is_wcopc_checkout', 'my_one_page_checkout' );
function my_one_page_checkout(){
return true;
}
add_filter( 'is_wcopc_checkout', 'true' );
That didn't seem to do it either.
Am I adding this code to the functions.php wrong? Any help on how I can get the One Page Checkout Plugin to work using do_shortcode?
Here's my full code in the page template for reference:
<?php
echo do_shortcode('[woocommerce_one_page_checkout template="product-table" product_ids="62, 122, 438, 52, 433, 435, 512, 514"]');
?>
Thanks for your help.
(I tried contacting WooCommerce support and they were no help saying that this is custom code and they can't do anything to help.)
The simplest way to return a true to a filter is like sitting the call back to WP default __return_true. So the function will be like
add_filter( 'is_wcopc_checkout', '__return_true' );
There is no filter named is_wcopc_checkout in the code of WooCommerce one page checkout version 1.0.2
From their doc- You can also manually add a shortcode [woocommerce_one_page_checkout] to any page or post and use the shortcode's attributes.
Usage: [woocommerce_one_page_checkout product_ids="30,45,12"]
Some context from One page checkout readme.
To register your template, attach a callback to the 'wcopc_templates' filter and add a new array of your template's details to the $templates array passed to your function.
For example, to register a custom pricing table template, the code would be similar to:
function eg_add_opc_template( $templates ) {
$templates['my-custom-pricing-table'] = array(
'label' => __( 'My Pricing Table', 'eg' ),
'description' => __( "Display a sophisticated and colourful pricing table with each product's attributes, but not weight or dimensions.", 'eg' ),
);
return $templates;
}
add_filter( 'wcopc_templates', 'eg_add_opc_template' ) );
The key used in the $templates array should be the template's file name (excluding the extension). The label element of the array is the name displayed on the One Page Checkout dialog. The description element is used for the tooltip next to the template's name.

Remove "Custom fields" metabox in Woocommerce order edit pages

I'm trying to remove the 'postcustom' meta box from the order details for a simple wooCommerce shop. The div #postcustom appears in Orders--->Click on single order --> bottom of page as 'Custom Fields'. I want to get rid of it. There are loads of examples of how to do this by calling the following function:
function remove_custom_field_meta_box()
{
remove_meta_box('postcustom', 'page', 'normal');
}
I've hooked it (currently) into:
//Remove postcustom meta box
add_action('admin_menu', 'remove_custom_field_meta_box');
I've also tried 'dashboard' and 'post' as $context to no avail.
I've also tried hooking to remove_meta_boxes, admin_init, and a few others.
I'm working in a child themes functions.php and using the default wooCommerce theme. Any thoughts on why this isn't firing? That pesky #postcustom div is still there in the admin menu! Is my context incorrect? I also tried 'orders'. Thanks#
Updated: For orders in Woocommerce the post type is 'shop_order', so your code should be:
add_action( 'add_meta_boxes', 'remove_shop_order_meta_boxe', 90 );
function remove_shop_order_meta_boxe() {
remove_meta_box( 'postcustom', 'shop_order', 'normal' );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Or you can hide it hitting the "screen options" tab and unchecking "Custom Fields" checkbox:

How to add custom fee to woocommerce from a custom filed

I have added a custom text field (named add_for_charity_field) in woocommerce. This filed is for adding desired amount for charity. I have used the WooCommerce Checkout Field Editor plugin to adding this field in checkout form. Now I need to add this field content to cart by add_fee function but I don't have access to the field content in php. Is there a solution for it ? I am not familiar with jQuery and I think that I must use jQuery.
I have use this code in function.php
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Charity', 'woocommerce'), $_POST['add_for_charity_field'] );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

How to remove attributes from product title in woocomerce order view page? [duplicate]

Ever since we upgraded to Woocommerce version 3 our order confirmations are showing huge titles that include the variation detail. I don't like how it looks and it breaks some important functionalities in some custom-made plugins.
Reference: Order Name Showing Variations since update to WC version 3
There is a filter that can be used to disable this data displaying in the title called woocommerce_product_variation_title_include_attribute_name from what I understand. But I have no idea where to apply the filter.
Is there a quick way to apply the filter to change it back to display as it did before?
This filter should work returning a false value for $should_include_attributes first argument in woocommerce_product_variation_title_include_attributes filter hook this way:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
$should_include_attributes = false;
return $should_include_attributes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should just work as you expect.
Update: The shorter way is:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
just works too.
A quick gotcha if you're using this filter to remove attributes from e-mail items. It appears that once an item has been written to an order the properties of it will not change.
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
As #freemason_17 pointed out, #LoicTheAztec's answer could potentially hide those details at other places as well so just to be sure I added a condition that would limit this to the cart page alone:
function custom_product_variation_title($should_include_attributes, $product){
if(is_cart()) {
$should_include_attributes = false;
return $should_include_attributes;
}
}
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
I use this:
/* -------------------- Remove variation names from title ------------------- */
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Wordpress meta box values display to custom fields

I've added meta boxes to my posts using the action "add_meta_boxes" to add/change custom settings like background-color, etc.
When I enable the custom fields in my screen options, all values of my meta boxes are displayd in these custom fields!
There are also showing up in the selectbox to add a new "custom field".
If you want to hide your post meta data from custom fields metabox, you should start your meta keys with underscore. Example _background-color
Added:
Also you can use is_protected_meta filter, which return boolean value (true - hide, false - show).
Filter parameters: $protected, $meta_key. See wp-includes/meta.php file. function is_protected_meta()
Normally WP hides meta keys that start with an underscore/_ from the Custom Fields (default/core) MetaBox.
Now imagine that you do not want to give the user of your plugin the possibility to alter Post Meta Data through the ugly and user unfriendly Custom Fields meta box. And therefore you build a custom meta box and prefix your meta key with an underscore/_. Then the user changes his mind and deactivates or uninstalls your plugin. What happens now is that the user has exactly no access to any UI to alter the (still present) meta data. This is a really, really bad situation for the user.
So we need a switch to turn off the Custom Fields MetaBox access as long as your plugin is activated. Therefore WP Core got the is_protected_meta() function. It basically consists out of two lines of code:
$protected = ( '_' == $meta_key[0] );
return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
As it's not nice to only offer a filter to handle that, WordPress today has a simple function that you can use:
register_meta( $meta_type, $key, $sanitize_callback, $auth_callback );
And the last argument, the $auth_callback does the following inside this function:
if ( empty( $auth_callback ) ) {
if ( is_protected_meta( $meta_key, $meta_type ) )
$auth_callback = '__return_false';
else
$auth_callback = '__return_true';
}
if ( is_callable( $auth_callback ) )
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
As you can see, you want to just add '__return_false' as $auth_callback to deactivate Custom Fields MetaBox access as long as your plugin is active. When the user removes or deactivates your plugin, he instantly has access to the meta field via the standard Custom Fields MetaBox.
Notes: WP core at v4.0.1 while writing this question. Make use of the $sanitize_callback! Thanks to Trepmal for posting about the is_protected_meta filter on her blog. Else I would have never stumbled upon the use case for that.

Categories