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
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!
Solutions
You have some minor mistakes
-
You use
$product->get_meta(..while$productis not defined -
The comparison operator in an if statement is
==, not= -
Use
woocommerce_admin_process_product_objectto save instead of the outdatedwoocommerce_process_product_metahook
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 );
Similar questions
Hide and unhide a specific shipping methods based on shipping class in WooCommerce
i have a cart that has three method standard delivery(0-10kg), standard delivery(11-20kg) and next day delivery(0-20kg), my problem is when i a product with a frozen food shipping class to the cart the shipping method should only be the next day delivery and if there is now shipping class on the cart it only has the standard delivery, my problem is...
Filter Shipping method based on shipping class in Woocommerce 3
I have been searching for code to filter out any shipping methods other than local pick up, on checkout, when a product that has a specific shipping class selected (Only pickup, ex.) is in the cart (among other products). I only found code that was outdated and does not work on WC3+.
Woocommerce Change shipping method title on checkout based on shipping class selected
I want to change the shipping method title displayed in the checkout of my store based on the shipping class the product has. e.g. Shipping method title is currently Flat Rate and I have 2 products: Sadly I have to do my shipping using classes so alternative methods won't work. Any help would be appreciated.
Conditional free shipping based on shipping class and minimal amount in Woocommerce
In woocommerce regarding Shipping methods, I am trying to have the following: I have tried by using flat rate and shipping classes I am getting like if product A and product B is there then if the cart doesn't reach 200 it is taking 15 shipping charge. Any help is appreciated.
Custom calculated shipping costs based on shipping class in WooCommerce
After updating Woocommerce to the latest version 3.5.7 last night, the checkout is freezing if country is switched from US to Canada on the checkout page. If we go back to the cart page, an error is shown. The error details are at the end of this question. What could be causing this? I have also copied the piece of code below that's leading to this...
Woocommerce shipping cost based on item quantity for specific shipping class
I posted this code a few hours ago. I managed to get through one of the issues, but i have only one question now. This code works well but I need to multiply the cost for every single item using an specific shipping class and then add it to the regular shipping cost. Example if I have 5 products in the cart: So if (for example) the regular shipping...