php – Price Capture, WordPress


I am making a stock plugin. The bottom line: in the admin panel, create a promotion, indicate different settings and a discount. When forming goods, a request is made to the database and for those goods that are included in the promotion, the price is multiplied by the% discount.

  foreach($products_id as $goods_id){      
  $price = $product->get_price($post_id);      
  echo '<span class="price"><span class="amount">'.$price.'</span>';            
  for($i=0; $i<count($products_id); $i++){
    if(in_array($post_id, $products_id[$i])){          
      $new_price = $price-($price*$discount);
      echo '<span class="discount_price">'.$new_price.'</span></span>';                           

The problem is the price for the cart / checkout, etc. does not change. I used

add_filter( 'woocommerce_get_price', 'camp_woocommerce_get_price', 10, 2);

but nothing worked for me. What hook to hook on this good?


Use the hook woocommerce_before_calculate_totals

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function add_custom_price( $cart_object ) {        
    foreach ( $cart_object->cart_contents as $key => $value ) {
        $discount = get_post_meta( $value['product_id'], 'название поля со скидкой', true ); // Здесь скидка
        $value['data']->price -= ($value['data']->price/100) * $discount;
Scroll to Top