Customizing WooCommerce product data label without plugin - WEIGHT - php

I'd like to change the product meta label from "Weight" to "Square Feet" in both the back end and front end.
I've tried this and a few [hundred] variations without success:
add_filter( 'woocommerce_register_post_type_product', 'custom_product_labels' );
function custom_product_labels( $args ) {
//
// change labels in $args['labels'] array
//
$args['labels']['_weight'] = 'Square Feet';
return $args;
I've successfully edited the UNITs with this:
add_filter( 'woocommerce_product_settings', 'add_woocommerce_dimension_units' );
function add_woocommerce_dimension_units( $settings ) {
foreach ( $settings as &$setting ) {
if ( $setting['id'] == 'woocommerce_dimension_unit' ) {
$setting['options']['feet'] = __( 'ft' ); // foot
}
if ( $setting['id'] == 'woocommerce_weight_unit' ) {
$setting['options']['sq ft'] = __( 'sq ft' ); // square feet
}
}
return $settings;
}
But I still can't figure out how to hook into the measurement labels to edit them. It's important to note that I don't want to ADD a meta unit of "Square Feet" because we already have thousands of products filled in with the sq ft data in the Weight field.
My quick workaround was to find the actual code on these pages and edit them. But it's a poor solution.
woocommerce/includes/admin/meta-boxes/views/html-product-data-shipping.php
woocommerce/includes/wc-formatting-functions.php
woocommerce/includes/wc-template-functions.php
Edit: Here is a page showing the use.
https://homedesigningservice.com/product/cape-house-plan-10034-cp/
Thank you in advance for saving my melting brain. :-)

You may use this snippet
add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Weight' :
$translated_text = __( 'Square Feet', $domain );
break;
case 'weight' :
$translated_text = __( 'Square Feet', $domain );
break;
}
return $translated_text;
}

Woo has a filter for front end but no filter for backend label changing.. So use below code and it won't conflict with any other label... Lakshman's gettext will change weight anywhere in the site...
add_filter( 'woocommerce_display_product_attributes',
'prefix_change_weight_label_to_square_feet', 10, 2 );
function prefix_change_weight_label_to_square_feet( $product_attributes, $product ) {
// Change Weight to Square Feet
$product_attributes[ 'weight' ]['label'] = __('Square Feet');
return $product_attributes;
}
// edit WEIGHT label to SQUARE FEET
add_action( 'admin_footer', function(){
$currentPostType = get_post_type();
if( $currentPostType != 'product' ) return;
?>
<script>
(function($){
$(document).ready(function(){
if ( jQuery('label[for="_weight"]').length ) {
jQuery('label[for="_weight"]').text("Square Feet");
}
});
})(jQuery);
</script>
<?php
});

Related

Set a default custom billing state field value in Woocommerce Admin page

I want to add some custom billing states and then set a default state in the Admin panel. So far I have added the states as follows (code below; also not sure this is right):
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce')
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
How do I set the default value to be StateA in the Admin Panel?
First, there is a mistake in your current code.
In Admin WooCommerce order pages when adding (or editing) billing (or shipping) address(es), to set custom 'PE1' as default state when selected country is Peru (PE), use the following instead:
add_filter( 'woocommerce_states', 'custom_woocommerce_states_for_peru' );
function custom_woocommerce_states_for_peru( $states ) {
// For PERU
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce'),
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
// Admin orders: Set a default state for PERU country
add_action( 'admin_footer', 'custom_admin_shop_order_js' );
function custom_admin_shop_order_js() {
global $pagenow, $post_type;
if ( in_array( $pagenow, array('post-new.php', 'post.php') ) && 'shop_order' === $post_type ) :
?><script type='text/javascript'>
jQuery( function($) {
// Billing state
$(document.body).on( 'change', 'select#_billing_country,select#_shipping_country', function(){
var country = 'PE', // Set country
defaultState = 'PE1', // Set default state (for country)
parent = $(this).parent().parent(),
billingState = parent.find('select#_billing_state'),
shippingState = parent.find('select#_shipping_state');
if( country === $(this).val() ) {
if ( '' === billingState.val() ) {
billingState.val(defaultState).trigger("change");
} else if ( '' === shippingState.val() ) {
shippingState.val(defaultState).trigger("change");
}
}
});
});
</script><?php
endif;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You can add some new custom states for a country using this code:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['XX'] = array( // XX is Country Code
'XX1' => 'State 1',
'XX2' => 'State 2'
);
return $states;
}
Then you can change the country and state default value using this code:
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
return 'XX'; // country code
}
function change_default_checkout_state() {
return 'XX'; // state code
}
If you are trying to make it dynamic and choose your default state from your admin panel, you can add a section and an option to your Woocommerce settings and get it using get_option('custom_id'). You can get help for creating a custom setting for Woocommerce here.

Remove columns from WooCommerce admin coupon list

I am trying to remove some default columns (amount & products) from the WooCommerce admin coupon list.
For that I make use of the following code:
add_filter( 'manage_posts_columns', 'custom_post_columns', 10, 2 );
function custom_post_columns( $columns, $post_type ) {
switch ( $post_type ) {
case 'shop_coupon':
unset(
$columns['amount'],
$columns['products']
);
break;
}
return $columns;
}
But it doesn't work and I'm not getting any errors. I think the code I'm using is just not being applied correctly.
You can use the manage_edit-{post type or taxonomy}_columns filter hook
So you get:
function filter_manage_edit_shop_coupon_columns( $columns ) {
// Remove
unset( $columns['products'] );
unset( $columns['amount'] );
return $columns;
}
add_filter( 'manage_edit-shop_coupon_columns', 'filter_manage_edit_shop_coupon_columns', 10, 1 );

Add class to WooCommerce pages via custom checkbox in WooCommerce product settings

I have a problem with the code below. With that code, we can select specific product and give them a different style in the shop archive. This is working.
However, I guess there is a error in the code.
Once I activated the checkbox for a product, it always appears in the new style even when I uncheck the checkbox. I assume that I made a error with the get_post_meta object.
Can someone help me with that?
Code to display the check box in the general product settings and add the class, based on the value in the check box
// Add checkbox
function action_woocommerce_product_general_options_product_style_listing_data() {
// Checkbox
woocommerce_wp_checkbox( array(
'id' => '_special_product_listing_style', // Required, it's the meta_key for storing the value (is checked or not)
'label' => __( 'Special style', 'woocommerce' ), // Text in the editor label
'desc_tip' => false, // true or false, show description directly or as tooltip
'description' => __( 'Promote a product by changing the style of the product card', 'woocommerce' ) // Provide something useful here
) );
}
add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_general_options_product_style_listing_data', 10, 0 );
// Save Field
function action_woocommerce_product_general_options_product_style_listing_save( $product ) {
// Isset, yes or no
$checkbox = isset( $_POST['_special_product_listing_style'] ) ? 'yes' : 'no';
// Update meta
$product->update_meta_data( '_special_product_listing_style', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_product_general_options_product_style_listing_save', 10, 1 );
// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {
if ( get_post_meta( $product->get_id(), '_special_product_listing_style', true ) ) {
$classes[] = 'custom-product-listing-class';
}
return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );
CSS to style the specific products:
/* Custom special product listing style */
li.sales-flash-overlay.woocommerce-text-align-left.woocommerce-image-align-center.do-quantity-buttons.product.type-product.post-10800.status-publish.first.instock.product_cat-crafty-beer-club.has-post-thumbnail.featured.sold-individually.taxable.shipping-taxable.product-type-variable.custom-product-listing-class {
border: 2px solid;
border-style: dashed;
}
Replace
// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {
if ( get_post_meta( $product->get_id(), '_special_product_listing_style', true ) ) {
$classes[] = 'custom-product-listing-class';
}
return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );
With
// Is_special style
function filter_woocommerce_post_class( $classes, $product ) {
// Get meta
$spls = $product->get_meta( '_special_product_listing_style' );
// Compare
if ( $spls == 'yes' ) {
$classes[] = 'custom-product-listing-class';
}
return $classes;
}
add_filter( 'woocommerce_post_class', 'filter_woocommerce_post_class', 10, 2 );
That should suffice for your code to function fully

Add a field to coupon settings and display the value on Woocommerce admin order list

I am having some really hard times trying to make it work.
I've had an idea in back of my head to:
be able to assign single user to a coupon code through an extra field in general coupon tab, which is listing unassigned users to coupon codes
I dont want to use third party extensions, custom-fields, etc. I was hoping I'll be able to do it through meta data but I failed. Not sure how to get it done properly.
add two extra columns on orders page and display both coupon code and user assigned to it.
After some time reading docs and xDebugging in phpstorm I have also failed to get it done.
function order_sellers_and_coupons_columns_values($column)
{
global $post, $the_order;
if ($column == 'order_coupon_code') {
$coupons = $the_order->get_used_coupons(); // not sure how to get coupon object by the coupon code
echo (count($coupons)) ? $coupons[0] : '';
}
}
// even though I see the order objects have an items and coupon lines property,
// which is an object, i can't get access to it
$the_order->items["coupon_lines"]
I am not asking for ready-to-go solution, but to show me the way how to get it done.
Thanks in advance for any kind of help.
In WooCommerce admin single coupon pages, we add an extra field for the seller (dealer):
// Add a custom field to Admin coupon settings pages
add_action( 'woocommerce_coupon_options', 'add_coupon_text_field', 10 );
function add_coupon_text_field() {
woocommerce_wp_text_input( array(
'id' => 'seller_id',
'label' => __( 'Assing a seller (dealer)', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Assign a seller / dealer to a coupon', 'woocommerce' ),
'desc_tip' => true,
) );
}
// Save the custom field value from Admin coupon settings pages
add_action( 'woocommerce_coupon_options_save', 'save_coupon_text_field', 10, 2 );
function save_coupon_text_field( $post_id, $coupon ) {
if( isset( $_POST['seller_id'] ) ) {
$coupon->update_meta_data( 'seller_id', sanitize_text_field( $_POST['seller_id'] ) );
$coupon->save();
}
}
Then using the following you will add to admin orders list the coupon code (when it's used in the order) with the seller / dealer name:
// Adding a new column to admin orders list
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column' );
function custom_shop_order_column($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['coupons'] = __( 'Coupon','theme_domain');
}
}
return $reordered_columns;
}
// Adding used coupon codes
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 10, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
global $the_order;
if ( $column == 'coupons' ) {
$coupons = (array) $the_order->get_used_coupons();
$dealers = [];
foreach( $coupons as $coupon_code ) {
$coupon = new WC_Coupon( $coupon_code );
$dealers[] = $coupon->get_meta('seller_id');
}
if( count($coupons) > 0 )
echo implode( ', ', $coupons );
if( count($dealers) > 0 )
echo '<br><small>(' . implode( ', ', $dealers ) . ')</small>';
}
}
All code goes in functions.php file of your active child theme (or active theme). Tested and works.
On Admin coupon single pages:
On Admin edit orders list:

Add a custom column to My Account Orders table in Woocommerce 3+

Woocommerce 3.5.x has a special page at the user account (My Account) area where it displays the user's previous Orders.
This page is now 5 column displays as default.
Here the screenshot of the woocommerce Orders area with 5 column:
My Orders
I Can't find the way to change this.
How can I add a new column in the default?
This requires 2 functions that will add a new column
The second function hook is a composite hook: woocommerce_my_account_my_orders_column_{$column_id} where {$column_id} need to be replaced by the column key slug that is set in the first function.
That second function manage the displayed row values and you can add for example a custom field to get custom order meta data values.
The code:
add_filter( 'woocommerce_account_orders_columns', 'add_account_orders_column', 10, 1 );
function add_account_orders_column( $columns ){
$columns['custom-column'] = __( 'New Column', 'woocommerce' );
return $columns;
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'add_account_orders_column_rows' );
function add_account_orders_column_rows( $order ) {
// Example with a custom field
if ( $value = $order->get_meta( '_custom_field' ) ) {
echo esc_html( $value );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You are done and have added a custom column to My account orders table:
If you which to make changes in the table html output, you will have to override the template file: myaccount/orders.php
If you don't wanna change the order template under myaccount page. Here's what you have to do.
First:
function wc_add_myaccount_order_column( $columns ) {
$columns[ 'custom-column' ] = __( 'Custom Column', 'woocommerce' );
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'wc_add_myaccount_order_column' );
Second:
function wc_custom_column_display( $order ) {
// do something here
echo "testing";
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'wc_custom_column_display' );
The code above will display "testing" in each order under "Custom Column" column.
Note: If you actually wanna change the entire template, like the design for example. You can follow the first answer above.
Just to improve the accepted answer I add a line to choose the position of the column (after total):
function sv_wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add ship-to after order status column
if ( 'order-total' === $key ) { //this is the line!
$new_columns['custom-column'] = __( 'Custom Column', 'woocommerce' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'sv_wc_add_my_account_orders_column' );
function wc_custom_column_display( $order ) {
// do something here
echo "testing";
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'wc_custom_column_display' );
With WooCommerce 5.9, I couldn't get LiocTheAztect's answer to work. What worked for me was:
add_filter( 'woocommerce_account_orders_columns',
'add_customer_email_column');
function add_customer_email_column( $columns ){
$new_columns = [
"order-number" => $columns["order-number"],
// ...
"customer-email" => __( 'Customer Email', '' ),
// ...
"order-actions" => $columns["order-actions"]
];
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_customer-email',
'add_customer_email_content' );
function add_customer_email_content($order) {
echo esc_html($order->get_billing_email());
}
Without the if ($value = $order->get_meta( '_custom_field' )) block. Hope it helps.

Categories