I’ve got the plugin WC Kalkulator installed and working with my woocommerce site. It runs with a simple product ID 4692. The problem is, the simple product only has static weight and dimensional values that get pulled into each cart item addition. That makes a 12″x12″ sign panel with a weight of 2 lbs cost the same to ship as a panel 120″ x 48″ at 80 lbs. I have fields setup in WC Kalkulator to define the calculated weight and dimensions.
I have a code started that is intended to use the calculated data to apply to the product’s data as it is entered into the cart. Each instance of the cart item should be unique to the input values.
I will say that I am not a coder, I got this far with Copilot AI…
add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 10, 1 );
function update_custom_price( $cart_object ) {
foreach ( $cart_object->get_cart() as $cart_item ) {
// Get the WC_Product object
$product = $cart_item['data'];
$product_id = $product->get_id();
// Check if the product ID matches the specific product
if ( $product_id == 4692 ) {
// Get the custom field value
$custom_field_value = get_post_meta( 4692, 'wck_weight', true );
$custom_field_value1 = get_post_meta( 4692, 'wck_length', true );
$custom_field_value2 = get_post_meta( 4692, 'wck_width', true );
$custom_field_value3 = get_post_meta( 4692, 'wck_height', true );
// Update the product weight and dimensions
$product->set_weight( $custom_field_value );
$product->set_length( $custom_field_value1 );
$product->set_width( $custom_field_value2 );
$product->set_height( $custom_field_value3 );
}
}
}
I don’t know if the “get_post_meta” is looking in the right spot to find the WCK fields. The fields are correctly named, and the product id is correct for my intentions. Any help is MUCH appreciated…
Jreppe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.