Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
Set WooCommerce Price based on Custom Field and Custom Date (Not Working)

I am working on a reversed "sale price" as in "Future Price" for single products in WooCommerce.

The general idea is to set a price (future price) and a start + end date. Then, using the woocommerce_product_get_price filter, check if today is the same as the value of "future price start date" and if they are the same, change the product price.

When the future price end date has passed, restore the product price into whatever the regular price is.

Problem is: I am not getting any errors or notices or anything like that -- which means I am lost :)

To accomplish this whole thing, I have created a custom field (future price). I've also created the date fields. They are all saved and updated.

If anyone can help and give me tips / pointers, I would really appreciate it. This is my second (2 out of 3) school projects due within a week.

This is my code in full:

add_filter( 'woocommerce_product_get_price', 'future_product_price', 20, 2 );
function future_product_price( $price, $product ) {

    if ( is_admin() ) return $price;

    $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
    $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
    $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date

    if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {

        $future_date_time   = (int) strtotime( $future_start_date ); // convert
        $now_time           = (int) strtotime("now"); // now in seconds

        // check if right now is the same day as the future price start date
        if ( $now_time == $future_start_date )

            // if they are the same, set the product price to the future price
            $price = $future_price;
    }

    return $price;
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

change "add_filter woocommerce_product_get_price" to "woocommerce_get_price_html" for display fuctre price and few small change in your future_product_price function :

add_filter( 'woocommerce_get_price_html', 'future_product_price', 20, 2 );
function future_product_price( $price, $product ) {

    if ( is_admin() ) return $price;

    $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
    $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
    $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date

    if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {

        $future_start = (int) strtotime( $future_start_date ); // convert
        $future_end   = (int) strtotime( $future_end_date ); // convert
        $now_time     = (int) strtotime(date('Y-m-d')); // now in seconds
        // check if right now is the same day as the future price start date
        if($future_start <= $now_time && $now_time <= $future_end) {
            // if they are the same, set the product price to the future price
            $price = $future_price;
        }
    }
    return $price;  
}

Add other functions for set future prices in a cart or checkout:

add_action( 'woocommerce_before_calculate_totals', 'set_alter_price_cart', 20, 2 );

function set_alter_price_cart( $cart ) {

if ( is_admin()) return;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {

        $product = $cart_item['data'];
        $price = $product->get_price();

        $future_price       = get_post_meta( $product->get_id(), 'future_price', true ); // future product price
        $future_start_date  = get_post_meta( $product->get_id(), '_future_price_date_from', true ); // future product price start date
        $future_end_date    = get_post_meta( $product->get_id(), '_future_price_date_to', true ); // future product price end date

        if ( ! empty( $future_price ) && ! empty( $future_start_date ) && ! empty( $future_end_date ) ) {
            
            $future_start = (int) strtotime( $future_start_date ); // convert
            $future_end   = (int) strtotime( $future_end_date ); // convert
            $now_time     = (int) strtotime(date('Y-m-d')); // now in seconds
            // check if right now is the same day as the future price start date
            if($future_start <= $now_time && $now_time <= $future_end) {
                $price =  $future_price;
            }
            $cart_item['data']->set_price($price);
        }
    } 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda