Answer the question
In order to leave comments, you need to log in
How to fix this calculator (you literally need to insert the correct array in the correct place)?
Hello.
I had a problem - I found a calculator for goods (on some site), it works, only a little almost to the end.
Here is the code for "calculating" the price so to speak:
add_filter( 'woocommerce_add_cart_item_data', 'custom_add_cart_item_data', 20, 2 );
function custom_add_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['height_option'] ) )
$cart_item['custom_data']['height'] = sanitize_key( $_POST['height_option'] );
if( isset( $_POST['width_option'] ) )
$cart_item['custom_data']['width'] = sanitize_key( $_POST['width_option'] );
// Make calculation and save calculated price
if( isset( $_POST['height_option'] ) && isset( $_POST['width_option'] ) ){
$height = (int) sanitize_key( $_POST['height_option'] );
$width = (int) sanitize_key( $_POST['width_option'] );
if( $width > 0 && $height > 0 ){
$total_price = ( ( $height / 3 ) * ( $width / 30 ) * $base_price ); // <== The calculation
$cart_item['custom_data']['price'] = round($total_price, 2); // Save the price in the custom data
}
}
return $cart_item;
}
// Display custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_data, $cart_item ) {
$domain = 'woocommerce';
if ( isset( $cart_item['custom_data']['height'] ) ){
$cart_data[] = array(
'name' => __( 'Height', $domain ),
'value' => $cart_item['custom_data']['height']
);
}
if ( isset( $cart_item['custom_data']['width'] ) ){
$cart_data[] = array(
'name' => __( 'Width', $domain ),
'value' => $cart_item['custom_data']['width']
);
}
return $cart_data;
}
// Set the new calculated price replacing cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_cart_item_calculated_price', 20, 1 );
function set_cart_item_calculated_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ){
if( ! isset( $cart_item['custom_data']['price'] ) ){
continue;
}
if( $cart_item['custom_data']['price'] > 0 ){
// Set the calculated item price (if there is one)
$cart_item['data']->set_price( (float) $cart_item['custom_data']['price'] );
}
}
}
// Get cart item custom data and update order item meta and display in orders and emails
add_action( 'woocommerce_checkout_create_order_line_item', 'save_order_item_custom_meta_data', 10, 2 );
function custom_add_order_item_meta( $item_id, $values ) {
$domain = 'woocommerce';
if( isset( $cart_item['custom_data']['height'] ) )
$item->update_meta_data( __( 'Height', $domain ), $values['custom_data']['height'] );
if( isset( $cart_item['custom_data']['width'] ) )
$item->update_meta_data( __( 'Width', $domain ), $values['custom_data']['width'] );
}
// Save custom field value in cart item as custom data
add_action( 'woocommerce_before_add_to_cart_button', 'add_product_custom_fields', 20 );
function add_product_custom_fields(){
$domain = 'woocommerce';
$value = isset( $_POST['height_option'] ) ? sanitize_key( $_POST['height_option'] ) : '';
printf( '<label>%s</label><input name="height_option" value="%s" type="number"/><br>', __( 'Height', $domain ), esc_attr( $value ) );
$value = isset( $_POST['width_option'] ) ? sanitize_key( $_POST['width_option'] ) : '';
printf( '<label>%s</label><input name="width_option" value="%s" type="number"/><br>', __( 'Width', $domain ), esc_attr( $value ) );
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question