Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
Set product shipping class based on metadata in WooCommerce

I'm trying to write a specific function that allow to change automatically the product shipping class when a WooCommerce custom field is set with a specific value.

Please find below the screenshot about the custom fields

enter image description here


This is the code I had thought of but obviously it doesn't work.

function woo_on_product_save( $post_id ) {
    $title = $product->get_meta( 'meta_easyfatt_libero_1' );
    if ( $title = 'Trenta' ) {
        $shipping_class_id = 1182;
    }

    $product = wc_get_product( $post_id );
    $product->set_shipping_class_id( $shipping_class_id );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'woo_on_product_save', 100 );

Any suggestions would be greatly appreciated!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have some minor mistakes

  • You use $product->get_meta(.. while $product is not defined
  • The comparison operator in an if statement is ==, not =
  • Use woocommerce_admin_process_product_object to save instead of the outdated woocommerce_process_product_meta hook

So you get:

// Save
function action_woocommerce_admin_process_product_object( $product ) {
    // Get meta
    $title = $product->get_meta( 'meta_easyfatt_libero_1' );
    
    if ( $title == 'Trenta' ) {
        // The targeted shipping class ID to be set for the product
        $shipping_class_id = 1182;

        // Set the shipping class ID 
        $product->set_shipping_class_id( $shipping_class_id );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 ); 
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!