• Resolved isabelk2j

    (@isabelk2j)


    Hello WooCommerce,
    Please i’ve been trying to figure out how to update shipping class from using php. i created a form which users can use to create product from their account dashboard, i was able to update all the fields using
    update_post_meta( $product_id, '_regular_price', $productPrice_converted ); and i created a custom dropdown field and was able to update the dropdown field using update_post_meta( $product_id, '_list_of_stores', 'custom-order' ); (where custom-order is the value of the select option i want updated).

    Now when i tried the same thing i did for the dropdown for the shipping class, i wasnt able to update the shipping class.
    update_post_meta( $product_id, 'product_shipping_class', 29 ); (where 29 is the ID/value of the shipping class).

    Is there something i’m doing wrong? why is the shipping not updated when product is created. please help

Viewing 1 replies (of 1 total)
  • Thread Starter isabelk2j

    (@isabelk2j)

    I have gotten a way to do it. Just incase someone has similar task to accomplish. I was told by a developer on stack that;

    Shipping classes are not managed by post meta data for the products. They are managed by a custom taxonomy, so you can’t use update_post_meta() function.

    In Woocommerce, shipping classes are managed by the custom taxonomy product_shipping_class and you will need to use wp_set_post_terms() function to make it work programmatically like:

    $shipping_class_id = 25; // the targeted shipping class ID to be set for the product
    
    // Set the shipping class for the product
    wp_set_post_terms( $product_id, array($shipping_class_id), 'product_shipping_class' );

    Peace!

    • This reply was modified 5 years, 2 months ago by isabelk2j.
Viewing 1 replies (of 1 total)
  • The topic ‘How to update shipping class programmatically’ is closed to new replies.