Mary_Pieroszkiewicz

Untitled

Apr 6th, 2021
576
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Edit cart price
  2. add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 99 );
  3. function add_custom_price( $cart_object ) {
  4.  
  5.   foreach ( $cart_object->cart_contents as $key => $value ) {
  6.  
  7.     $variation = new \WC_Product_Variation( $value['variation_id'] );
  8.     $variation_price_per_m2 = $variation->get_price();
  9.  
  10.  
  11.     if ( isset( $value[ 'fotolia_id' ] ) ) {
  12.       $image_area = floatval( $value['image_area'] );
  13.  
  14.       $fotolia_photo_price = floatval( $value['fotolia_photo_price'] );
  15.  
  16.       $new_price = $variation_price_per_m2 * $image_area + $fotolia_photo_price;
  17.  
  18.     } else if ( isset( $value[ 'image_filename' ] ) ) {
  19.       $image_area = floatval( $value['image_area'] );
  20.  
  21.       $new_price = $variation_price_per_m2 * $image_area;
  22.  
  23.     } else {
  24.       $id = $value['data']->get_id();
  25.  
  26.       $product_price_per_m2 = $value['data']->get_price();
  27.  
  28.       $new_price = intval( $value[ 'image_width' ] ) * intval( $value[ 'image_height' ] ) / 10000 * $product_price_per_m2;
  29.  
  30.     }
  31.  
  32.     $value['data']->set_price( $new_price );
  33.   }
  34. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×