1

Woocomerce - Actual product price will be offered in installments, just to show an installment amount to the customer.

Suppose, Product A has a Price of 1500 and we need to show 375 only.

$product->get_price()) / 4), any possibility to get it through a shortcode

Zubir Ahmed
  • 49
  • 11

1 Answers1

0

you can try this one for altering product price (actual price to instalment price).

/**
 * @description    Alter Product Pricing For WooCommerce Product
 * @compatible    WooCommerce 4.1
 */

add_filter( 'woocommerce_get_price_html', 'woo_alter_price_display', 9999, 2 );

function woo_alter_price_display( $price_html, $product ) {
    
    // ONLY ON FRONTEND
    if ( is_admin() ) return $price_html;

    // ONLY IF PRICE NOT NULL
    if ( '' === $product->get_price() ) return $price_html;

    // IF CUSTOMER LOGGED IN
    // if ( wc_current_user_has_role( 'customer' ) ) {
        $price = $product->get_price();
        $installment_price = $price / 4;
        $orig_price = wc_get_price_to_display( $product );
        $price_html = wc_price($installment_price);
    // }
    
    return $price_html;

}

/**
 * @description   Alter Product Pricing For WooCommerce Cart/Checkout
 */

add_action( 'woocommerce_before_calculate_totals', 'woo_alter_price_cart', 9999 );

function woo_alter_price_cart( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    // IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
    //if ( ! wc_current_user_has_role( 'customer' ) ) return;

    // LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price = $product->get_price();
        $installment_price = $price / 4;
        $cart_item['data']->set_price($installment_price);
    }
}

You can update/customize price role wise like for customer, subscriber, logged in user, get user etc... I have already validated only for customers that are commented (//).

Note: You can easily create a shortcode on any function like this:

add_shortcode('shortcode_name', 'your function name');

Example:

add_shortcode('product_price_display', 'woo_alter_price_display');

Source URL is - https://www.sitepoint.com/wordpress-shortcodes-tutorial/

Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23