Search code examples
phpwoocommerceproductcartinput-field

Use WC Kalkulator product field values to update WooCommerce cart item product properties


In WooCommerce, I use the WC Kalkulator plugin for a simple product (ID 4692). The problem is, this simple product has only static weight and dimensions 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 plugin 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 inputted values.

I will say that I am not a coder, and I got this far with Copilot AI help:

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.


Solution

  • There are some mistakes in your code, as the data is posted as custom cart item data, but there is no related product metadata. Try the following:

    add_action( 'woocommerce_before_calculate_totals', 'custom_item_weight_and_dimensions' );
    function custom_item_weight_and_dimensions( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $item ) {
            if ( isset($item['wckalkulator_fields']) ) {
                $fields = (array) $item['wckalkulator_fields'];
                if ( isset($fields['wck_weight']) && !empty($fields['wck_weight']) ) {
                    $item['data']->set_weight($fields['wck_weight']);
                }
    
                if ( isset($fields['wck_length']) && !empty($fields['wck_length']) ) {
                    $item['data']->set_length($fields['wck_length']);
                }
    
                if ( isset($fields['wck_width']) && !empty($fields['wck_width']) ) {
                    $item['data']->set_width($fields['wck_width']);
                }
    
                if ( isset($fields['wck_height']) && !empty($fields['wck_height']) ) {
                    $item['data']->set_height($fields['wck_height']);
                }
            }
        }
    }
    

    Code goes in function.php file of your child theme (or in a plugin). It should work if you gave the correct field keys in your code attempt.

    Note that in my code attempt, there is no need to define a product ID as my code will check if each related custom cart item data exists and has a value, to update the related cart item (product) properties.