skip to Main Content

My question is how to sum a fixed price with product subtotal en calculate.
Woocommerce prices are based on quantity but i want the subtotal of a product in cart sum+ with a certain price my code:

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        //  $cart_item['data']->set_price($custom_price);
        $_product =  wc_get_product( $cart_item['data']->get_id() );
        $getProductDetail = wc_get_product( $cart_item['product_id'] );

        $quantity_cr =  $cart_item['quantity'];
        $price = get_post_meta($cart_item['product_id'] , '_price', true);



        /*Regular Price and Sale Price*/
        //echo "Regular Price: ".get_post_meta($cart_item['product_id'] , '_regular_price', true)."<br>";
        //echo "Sale Price: ".get_post_meta($cart_item['product_id'] , '_sale_price', true)."<br>";

            $opdruk = $cart_item['thwepof_options'];
                foreach($opdruk as $cr_item) {

                     $total_price_each_prduct = $cart_item['line_total'];
                    $opdruk_value = $cr_item['value'];
                    //print_r($opdruk_value);

                    if ($opdruk_value != "Geen opdruk") {

                 $price_opdruk =  $price + 39; // this is the fixed price i want to add (not on quantity based)


                    }
                }
    }

3

Answers


  1. Hello you can do this like adding a tax/shipping/etc… in total price

    function prefix_add_price_line( $cart ) {
    
      $add_price = 39;
    
      $cart->add_fee( __( 'Vat Exempt Tax', 'yourtext-domain' ) , $add_price );
    
    }
    add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_price_line' );
    
    Login or Signup to reply.
  2. Try this code

    add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 10, 1);
    
    function add_custom_price( $cart_object ) {
    
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;
    
    foreach ( $cart_object->get_cart() as $cart_item ) {
        ## Price calculation ##
        $price = $cart_item['data']->price * 12;
    
        ## Set the price with WooCommerce compatibility ##
        if ( version_compare( WC_VERSION, '3.0', '<' ) ) {
            $cart_item['data']->price = $price; // Before WC 3.0
        } else {
            $cart_item['data']->set_price( $price ); // WC 3.0+
        }
    }
    }
    
    Login or Signup to reply.
  3. You can try use this hook:

    function filter_woocommerce_cart_product_subtotal( $product_subtotal, $product, $quantity, $instance ) { 
        // some logic
        return $product_subtotal; 
    }; 
    
    // add the filter 
    add_filter( 'woocommerce_cart_product_subtotal', 'filter_woocommerce_cart_product_subtotal', 10, 4 ); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search