Search code examples
phpwordpresswoocommerceproduct

Show monthly installment prices on WooCommerce single product page with a treshold


I have been having troubles implementing this on our WordPress/WooCommerce system.

We devised a method where users can see monthly installment prices on the product page.

add_action( 'woocommerce_before_add_to_cart_form', 'price_per_month', 5 );

function  price_per_month() {

    global $product;
    $price = $product->get_regular_price();
    $months1 = 3;
    $months2 = 6;
    $months3 = 12;
    $months4 = 24;

    $price_per_month1 = ($price / $months1);
    $price_per_month2 = ($price / $months2);
    $price_per_month3 = ($price / $months3);
    $price_per_month4 = ($price / $months4);
 
    echo "
    <div id='ppm-container'>
        <div id='ppm-text'>
            <b>Insert Title</b></br>
            Pay from only:</br>
            <b>PHP ". round($price/3,2,PHP_ROUND_HALF_UP) ."</b> for 3 months</br>
            <b>PHP ". round($price/6,2,PHP_ROUND_HALF_UP) ."</b> for 6 months</br>
            <b>PHP ". round($price/12,2,PHP_ROUND_HALF_UP) ."</b> for 12 months</br>
            <b>PHP ". round($price/24,2,PHP_ROUND_HALF_UP) ."</b> for 24 months</br>
            Insert Footer.</br>
        </div>
    </div>
    ";
     
}

What we wanted for the code is not to run if the original price is below, say, 3000. What code should I use for this? I tried a basic if/else program but that did not work.

I'd also love feedback on this code as well. We were looking for something similar to this one, but that was the only available plugin we saw. The tables automatically computed for every month on a specified range and so I decided to code it on my own.


Solution

  • Instead of using multiple if/else conditions you can use an array and a foreach loop.

    For the threshold you can add a simple if condition

    So you get:

    function price_per_month() {
        global $product;
        
        // Is a WC product 
        if ( is_a( $product, 'WC_Product' ) ) {
            
            // Only for 'simple' products
            if ( $product->is_type( 'simple' ) ) {          
                // Set months
                $months = array ( 3, 6, 12, 24 );
                
                // Treshold
                $treshold = 3000;
                
                // Get price
                $price = $product->get_regular_price();
                
                // Price greather than or equal to treshold
                if ( $price >= $treshold ) {        
                    echo '<div id="ppm-container"><div id="ppm-text">';
                    echo '<h2>Insert title</h2>';
                    echo '<p>Pay from only:</p>';
                    
                    // Iterate over array
                    foreach ( $months as $month ) {
                        echo '<p>PHP ' . round( $price / $month, 2, PHP_ROUND_HALF_UP ) . ' for ' . $month .  ' months</p>';
                    }
                    
                    echo '<p>Insert Footer</p>';
                    echo '</div></div>';
                }
            }
        }
    }
    add_action( 'woocommerce_before_add_to_cart_form', 'price_per_month', 5 );