Created
April 25, 2020 02:51
-
-
Save iAmServer/e55c18d030d2e336a16a715aa05d5c15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1); | |
function add_custom_price( $cart_obj ) { | |
// This is necessary for WC 3.0+ | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) | |
return; | |
// Avoiding hook repetition (when using price calculations for example) | |
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) | |
return; | |
foreach ( $cart_obj->get_cart() as $key => $value ) { | |
$weight = $value['data']->weight; | |
$price = $value['data']->price; | |
$newprice = $price * (float)$weight; | |
$value['data']->set_price($newprice); | |
} | |
} | |
// Display the selected lenght value below cart item name | |
add_filter( 'woocommerce_cart_item_name', 'display_select_length_after_cart_item_name', 10, 3 ); | |
function display_select_length_after_cart_item_name( $name, $cart_item, $cart_item_key ) { | |
if( is_cart() && isset($cart_item['data']->weight) ) { | |
$name .= '<p>'.__("Weight:") . ' ' . esc_html(wc_format_weight($cart_item['data']->weight)) . '</p> <br />'; | |
$name .= '<p><b>NB:</b> <i>This will influence the product price</i></p>'; | |
} | |
return $name; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment