首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Woocommerce中显示日期的售后价格文本

在Woocommerce中显示日期的售后价格文本
EN

Stack Overflow用户
提问于 2018-12-07 10:38:37
回答 1查看 371关注 0票数 2

我正在使用Customs product price on sale displayed with date limit in Woocommerce 3+做一个自定义格式的价格显示在销售的产品有一个限制期。

现在,我也试图包括“从”日期。如何在此代码中获取并包含from date?

任何一条赛道都是非常有用和被欣赏的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-07 11:34:41

下面的代码将同时处理销售日期、销售日期和价格自定义显示(同时要求日期和日期)。

代码语言:javascript
复制
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_from = $product->get_date_on_sale_from();
            $sales_price_to = $product->get_date_on_sale_to();
            if( ! empty($sales_price_from) && ! empty($sales_price_to) ){
                $replacement = ' </ins> <span class="notice-price">(on offer from ' . date( 'j.M.Y', $sales_price_from->getTimestamp() ) . ' until ';
                return str_replace( '</ins>', $replacement . date( 'j.M.Y', $sales_price_to->getTimestamp() ) . ')</span>', $price );
            }
        }
        // Variable products
        else if ( $product->is_type( 'variable' ) )
        {
            $from = $to = '';
            // Loop through variations
            foreach ( $product->get_children() as $key => $variation_id ) {
                $variation        = wc_get_product($variation_id);
                $sales_price_from = $variation->get_date_on_sale_from();
                $sales_price_to   = $variation->get_date_on_sale_to();

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53667817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档