首页 > 解决方案 > 在 Woocommerce 3+ 中显示带有日期限制的海关产品销售价格

问题描述

我有代码在打折时在价格之后打印文本,现在使用此代码时出现错误,这是我以前没有得到的。它说:“PHP 警告:date() 期望参数 2 为整数”,它指的是这一行:

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

关于如何解决它的任何想法?

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 );

        }
    }
}

一直在尝试修复它,但我不能。

标签: phpwordpressdatewoocommerceproduct

解决方案


自 Woocommerce 3 以来,您的代码已经过时了,因为很多事情都发生了变化。

我已经完全重新审视了您的代码,使其适用于 Woocommerce 3+ 版本:

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;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


推荐阅读