Advertisement
Mary_Pieroszkiewicz

Untitled

Apr 6th, 2021
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement