milardovich

Change price

Dec 6th, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1.     add_filter( 'woocommerce_before_calculate_totals', 'woo_custom_addons_plugin_custom_cart_items_prices', 10, 1 );
  2.     function woo_custom_addons_plugin_custom_cart_items_prices( $cart_object ) {
  3.         global $prices,$add_ons;
  4.         if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  5.             return;
  6.  
  7.         foreach ( $cart_object->get_cart() as $cart_item ) {
  8.  
  9.             // get the product id (or the variation id)
  10.             $id = $cart_item['data']->get_id();
  11.  
  12.             // GET THE NEW PRICE (code to be replace by yours)
  13.             $explode = explode(" - ",$cart_item['data']->get_name());
  14.             if(isset($explode[1])){
  15.                 $add_on_id = $add_ons->getByName($explode[1]);
  16.                 if($add_on_id !== 0 && $prices->getPricePerQuantity($add_on_id,$cart_item['quantity']) !== false){
  17.                     $new_price = $prices->getPricePerQuantity($add_on_id,$cart_item['quantity']); // <== Add your code HERE
  18.  
  19.                     // Updated cart item price
  20.                     $cart_item['data']->set_price( $new_price );
  21.                 }
  22.             }
  23.         }
  24.     }
Advertisement
Add Comment
Please, Sign In to add comment