I am not able to retrieve the value of measurement “volume” after I added custom column in order page details using below code:
// Add custom column headers
add_action(
'woocommerce_admin_order_item_headers',
'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
//column name
$column_name = 'Total M3';
// display the column name
echo '<th>' . $column_name . '</th>';
}
// Add custom column values here
add_action(
'woocommerce_admin_order_item_values',
'my_woocommerce_admin_order_item_values',
10,
3);
function my_woocommerce_admin_order_item_values($_product, $item, $item_id = null) {
// get the post meta value from the associated product
$value = get_post_meta($_product->post->ID, '_custom_field_name', 1);
// display the value
echo '<td>' . $value . '</td>';
}
I am using WooCommerce Measurement Price Calculator plugin to calculate the measurement. I cannot find meta value.
Related
I'm looking for some help getting the WooCommerce variable product title to change based on variations. In this specific case I would like the title to change when a color is selected, like "Productname Black".
Is there any easy snippet to get this to work?
UPDATE 04-2021 - Successfully tested on WooCommerce 5.1+ (handle custom product attributes)
The following code, will add to variable product title the value(s) of the chosen variation from specific defined product attribute(s) (or all of them optionally too):
The code:
// Defining product Attributes term names to be displayed on variable product title
add_filter( 'woocommerce_available_variation', 'filter_available_variation_attributes', 10, 3 );
function filter_available_variation_attributes( $data, $product, $variation ){
// Here define the product attribute(s) slug(s) which values will be added to the product title
// Or replace the array with 'all' string to display all attribute values
$attribute_names = array('Custom', 'Color');
foreach( $data['attributes'] as $attribute => $value ) {
$attribute = str_replace('attribute_', '', $attribute);
$attribute_name = wc_attribute_label($attribute, $variation);
if ( ( is_array($attribute_names) && in_array($attribute_name, $attribute_names) ) || $attribute_names === 'all' ) {
$value = taxonomy_exists($attribute) ? get_term_by( 'slug', $value, $attribute )->name : $value;
$data['for_title'][$attribute_name] = $value;
}
}
return $data;
}
// Display to variable product title, defined product Attributes term names
add_action( 'woocommerce_after_variations_form', 'add_variation_attribute_on_product_title' );
function add_variation_attribute_on_product_title(){
// Here define the separator string
$separator = ' - ';
?>
<script type="text/javascript">
(function($){
var name = '<?php global $product; echo $product->get_name(); ?>';
$('form.cart').on('show_variation', function(event, data) {
var text = '';
$.each( data.for_title, function( key, value ) {
text += '<?php echo $separator; ?>' + value;
});
$('.product_title').text( name + text );
}).on('hide_variation', function(event, data) {
$('.product_title').text( name );
});
})(jQuery);
</script>
<?php
}
Displaying all attributes
You can display all variations attributes values for the chosen variation by defining the variable $attribute_names to "all" so like:
$attribute_names = "all";
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
Tested and works… you will get something like:
In Product Category admin page of Woocommerce, I'd like to have an extra column showing the Product Category IDs instead of manually find out on every product category's url.
I've found some snippet for showing the IDs in normal Wordpress categories but nothing about Woocommerce's product category.
function add_post_tag_columns( $columns ) {
$columns['id'] = 'Id';
return $columns;
}
add_filter( 'manage_edit-product_cat_columns', 'add_post_tag_columns' );
function add_post_tag_column_content( $content, $column, $id ) {
if ( 'id' === $column ) {
$content = $id;
}
return $content;
}
add_filter( 'manage_product_cat_custom_column', 'add_post_tag_column_content', 10, 3 );
The hook manage_edit-product_cat_columns is used to modify/add new column heading.
The hook manage_product_cat_custom_column is used to display custom content of a specific column. Here we conditionally check the column heading is equal to id if so we assign $id third parameter value which holds the id of the category to the first parameter $content.
I am using WooCommerce and I'm trying to display product variation SKUs on the product page below the product title. I managed to find this code, which works but displays the SKU in the wrong place:
// Display product variations SKU and GTIN info
add_filter( 'woocommerce_available_variation', 'display_variation_sku_and_gtin', 20, 3 );
function display_variation_sku_and_gtin( $variation_data, $product, $variation ) {
$html = ''; // Initializing
// Inserting SKU
if( ! empty( $variation_data['sku'] ) ){
$html .= '</div><div class="woocommerce-variation-sku">' . __('SKU:') . ' ' . $variation_data['sku'];
}
// Using the variation description to add dynamically the SKU and the GTIN
$variation_data['variation_description'] .= $html;
return $variation_data;
}
Can anyone help with changing the order of this code so the SKU shows below the product title, or help me with some new code?
Many thanks!
WooCommerce does not provide a specific action hook that will let you add anything right after the product title, that also makes use of the variation data like the hook in your current code. You can work around this by adding an element after the product title via JavaScript/jQuery.
You want this element to dynamically change based on the selected variation. Since you do not have access to the variation data directly in the action hook you will have to check the hidden input variation_id that WooCommerce uses to store the selected variation id. Then use AJAX every time that input changes to retrieve the variation SKU belonging to this variation id.
add_action( 'woocommerce_before_single_product', 'show_variation_sku_underneath_product_title' );
function show_variation_sku_underneath_product_title() {
global $product;
if ( $product->is_type('variable') ) {
?>
<script>
jQuery(document).ready(function($) {
$('input.variation_id').change( function(){
if( '' != $('input.variation_id').val() ) {
jQuery.ajax( {
url: '<?php echo admin_url( 'admin-ajax.php'); ?>',
type: 'post',
data: {
action: 'get_variation_sku',
variation_id: $('input.variation_id').val()
},
success: function(data) {
$('h1.product_title').siblings('.variation-sku').remove();
if(data.length > 0) {
$('h1.product_title').after('<p class="variation-sku">' + data + '</p>');
}
}
});
}
});
});
</script>
<?php
}
}
add_action('wp_ajax_get_variation_sku' , 'get_variation_sku');
add_action('wp_ajax_nopriv_get_variation_sku','get_variation_sku');
function get_variation_sku() {
$variation_id = intval( $_POST['variation_id'] );
$sku = '';
if ( $product = wc_get_product( $variation_id ) ) $sku = $product->get_sku();
echo $sku;
wp_die(); // this is required to terminate immediately and return a proper response
}
These code snippets should be added to the functions.php of your child theme or via a plugin like Code Snippets.
How to Display additional custom column in woocommerce products listing page:
Here is the solution:
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
//add columns
$columns['cost'] = __( 'cost','woocommerce'); // title
return $columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
// HERE get the data from your custom field (set the correct meta key below)
$cost_price = get_post_meta( $product_id, '_cost_price', true );
switch ( $column )
{
case 'cost' :
echo $cost_price; // display the data
break;
}
}
Use the above code and change the name of the column with your column name and paste it in function.php file
screenshot
I am trying to display chained products meta on admin product column but it just seems to display array. What is the bug? Thanks guys!
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
//add columns
$columns['chain_product'] = __( 'Custom','woocommerce'); // title
return $columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
$chained_parent_id = empty( $variation ) ? $post->ID : $variation->ID;
// HERE get the data from your custom field (set the correct meta key below)
$chain_product = get_post_meta( $chained_parent_id, '_chained_product_detail', true );
switch ( $column )
{
case 'chain_product' :
echo $chain_product; // display the data
break;
}
}