Customs product price on sale displayed with date

2019-08-03 13:28发布

问题:

I have code which prints text after the price when on sale and I am now getting an error using this code, which I did not get before. It says: "PHP Warning: date() expects parameter 2 to be integer" and it refers to this line:

$sales_price_date_to = date( "j.M.Y", $sales_price_to );

Any ideas on how to fix it?

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    global $post;
    $sales_price_to   = get_post_meta( $product->id, '_sale_price_dates_to', true );
    $sales_price_date_to   = date( "j.M.Y", $sales_price_to );

    if ( is_single() ) {
        if($product->is_type( 'simple' ) && $sales_price_to != "" )  {

            $sales_price_date_to = date("j.m.Y", $sales_price_to);
            return str_replace( '</ins>', ' </ins> <span class="notice-price">(on offer until '.$sales_price_date_to.')</span>', $price );

        }  else if ( $product->is_type( 'variable' ) ){

            $handle         = new WC_Product_Variable( $product->id);
            $variations1    = $handle->get_children();
            $content_variations = "";

            $count_each_var = 0;

            foreach ($variations1 as $value) {
                $count_each_var++;
                $single_variation   = new WC_Product_Variation($value);
                $var_id             = $single_variation->variation_id; 
                $sales_price_to_variable    = get_post_meta($var_id, '_sale_price_dates_to', true);
                if( $sales_price_to_variable != '' ){
                    $sales_price_date_to        = date("j.m.Y", $sales_price_to_variable);
                    if($count_each_var == 1) { 
                        $class_val = 'class="active"';
                    } else {
                        $class_val = "";
                    }

                    $content_variations         .= "<i data-id='". $var_id ."' data-order='".$count_each_var."' $class_val >".$sales_price_date_to."</i>";
                } else {
                    $content_variations .= "";
                }
            }
            if( $content_variations != ''){
                return str_replace( '</ins>', ' </ins> <span class="notice-price">(on offer until '.$content_variations.')</span>', $price );
            } else {
                return $price;
            }

        } else {

            return apply_filters( 'woocommerce_get_price', $price );

        }
    }
}

Been trying to fix it, but I can't.

回答1:

Your code is really outdated ans more since Woocommerce 3 as a lot of things have changed.

I have completely revisited your code making it up to date for Woocommerce 3+ versions:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    if ( is_product() ) {
        // Simple products and variations
        if( $product->is_type( 'simple' ) || $product->is_type( 'variation' )  )
        {
            $sales_price_to = $product->get_date_on_sale_to();
            if( ! empty($sales_price_to) ){
                $replacement = ' </ins> <span class="notice-price">(on offer until ';
                return str_replace( '</ins>', $replacement . date( 'j.M.Y', $sales_price_to->getTimestamp() ) . ')</span>', $price );
            }
        }
        // Variable products
        else if ( $product->is_type( 'variable' ) )
        {
            $content = '';
            // Loop through variations
            foreach ( $product->get_children() as $key => $variation_id ) {
                $variation       = wc_get_product($variation_id);
                $sales_price_to  = $variation->get_date_on_sale_to();

                if( ! empty($sales_price_to) ){
                    $date_to  = date( 'j.M.Y', $sales_price_to->getTimestamp() );
                    $class    = $key == 0 ? 'class="active"' : '';
                    $content .= '<i data-id="'.$variation_id.'" data-order="'.($key + 1).'" '.$class.'>'. $date_to .'</i>';
                }
            }
            if( ! empty($content) ){
                return $price . ' <span class="notice-price">(on offer until ' . $content .')</span>';
            }
        }
    }
    return $price;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.