'Woocommerce Subscriptions custom pricing php

I am working on a php code (adjusting from here: https://gist.github.com/xandl/743fb6af60827eb95ad42b20b478b020)

Basically I want to change the price for the subscription product, if a customer visited for the first time (checked with 24h cookie) > I am aware that cookies is not the best for discounting.

I added a field for the product & the value is showing on the frontend correctly. However I have two options for the product:

  1. One-Time purchas
  2. Payment Plan (via plugin: https://sgwebpartners.com/woocommerce-payment-plans/; uses normal Woocommerce subscription functionality)

With my script the price gets displayed correctly, but for both of the options of the product, see screenshot: https://imgur.com/a/LRayOFM

Would be great if you could help to show this price (in the example 2€) only for the payment plan option.

/**
 * Step #1: create the field used to store the new sale_price for product_variation and for products
 */
add_action( 'woocommerce_product_after_variable_attributes', 'ran_variation_settings_fields_2', 10, 3 );
function ran_variation_settings_fields_2( $loop, $variation_data, $variation ) {
    woocommerce_wp_text_input(
        array(
            'id'          => '_firsttimer_ppl_price[' . $variation->ID . ']',
            'label'       => __( 'FirstTimer-Paymentplan-Price', 'ran' ),
            'desc_tip'    => 'true',
            'description' => __( 'What is the FirstTimer price of this product?', 'ran' ),
            'value'       => get_post_meta( $variation->ID, '_firsttimer_ppl_price', true ),
            'type'        => 'text'
        )
    );
}

add_action( 'woocommerce_product_options_general_product_data', 'ran_add_custom_general_fields_2' );
function ran_add_custom_general_fields_2() {
    global $woocommerce, $post;
    echo '<div class="options_group">';
    woocommerce_wp_text_input(
        array(
            'id'          => '_firsttimer_ppl_price[' . $post->ID . ']',
            'label'       => __( 'FirstTimer-Price', 'ran' ),
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'What is the FirstTimer price of this product?', 'ran' ),
            'value'       => get_post_meta( $post->ID, '_firsttimer_ppl_price', true ),
            'type'        => 'text'
        )
    );
    echo '</div>';
}

/**
 * Step #2: save the merchant value
 */

add_action( 'woocommerce_process_product_meta', 'ran_add_custom_general_fields_save_2' );
add_action( 'woocommerce_save_product_variation', 'ran_add_custom_general_fields_save_2', 10, 2 );
function ran_add_custom_general_fields_save_2( $post_id ) {
    if (!array_key_exists('_firsttimer_ppl_price', $_POST)) return;
    if (!is_array($_POST['_firsttimer_ppl_price'])) return;
    if (!array_key_exists($post_id, $_POST['_firsttimer_ppl_price'])) return;

    $woocommerce_field = $_POST['_firsttimer_ppl_price'][ $post_id ];
    update_post_meta( $post_id, '_firsttimer_ppl_price', esc_attr( $woocommerce_field ) );
}

/**
 * Step #3: define when the new price will be used for discounts
 */
function ran_current_user_is_merchant_2() {
if (!isset($_COOKIE['firsttimer_pricing'])){
    $valid_to = time() + (86400);
    setcookie("firsttimer_pricing", $valid_to, 2147483647, '/');
    return true;
}

if (isset($_COOKIE['firsttimer_pricing']) && $_COOKIE['firsttimer_pricing'] >= time()){
  return true;
}else{
  return false;
}

}



/**
 * Step #4: change the price based on the above function
 */
add_filter( 'wpp_purchase_option_price', 'ran_custom_price_2', 10, 2 );
add_filter( 'woocommerce_product_get_price', 'ran_custom_price_2', 10, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'ran_custom_price_2', 10, 2 );

add_filter( 'woocommerce_product_get_sale_price', 'ran_custom_price', 10, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'ran_custom_price_2', 10, 2 );

add_filter( 'woocommerce_variation_prices_price', 'ran_custom_price_2', 10, 2 );
add_filter( 'woocommerce_variation_prices_sale_price', 'ran_custom_price_2', 10, 2 );


function ran_custom_price_2( $price, $product ) {
    if ( ran_current_user_is_merchant_2() ){
        // get the merchant_price and return if available
        $merchant_price_2 = floatval( $product->get_meta( '_firsttimer_ppl_price', true ) );
        if ( $merchant_price_2 ) {
            return $merchant_price_2;
        }
    }
    

    return $price;
}


/**
 * Step #5: Make sure the variation price hash is is affected by the function defined in step #3
 */
add_filter( 'woocommerce_get_variation_prices_hash', 'ran_woocommerce_get_variation_prices_hash_2', 10, 3 );
function ran_woocommerce_get_variation_prices_hash_2( $price_hash, $product, $for_display ) {
    $price_hash['merchant'] = ran_current_user_is_merchant_2() ? 'true' : 'false';

    return $price_hash;
}


/**
 * (optional) Step #6: Make the cart look a little nicer
 */
add_filter( 'woocommerce_cart_product_price', 'ran_woocommerce_cart_product_price_2', 20, 2 );
function ran_woocommerce_cart_product_price_2( $price_html, $product ) {
    if ( ! $product->get_sale_price() || ! $product->get_regular_price() || $product->get_sale_price() >= $product->get_regular_price() ) {
        return $price_html;
    }

    return wc_format_sale_price(
               wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price() ) ),
               wc_get_price_to_display( $product, array( 'price' => $product->get_sale_price() ) )
           ) . $product->get_price_suffix();
}

FYI: Also tried only this filter: add_filter( 'wpp_purchase_option_price', 'ran_custom_price_2', 10, 2 ); without the others from above. Then the purchase price is only visible for the payment plan option, but when adding the option to the cart the price is not correct.

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source