簡體   English   中英

將Woocommerce格式化的產品價格添加到主題的產品導航中

[英]Add Woocommerce formatted product price to theme's product navigation

剛安裝了帶有BeTheme和Woocommerce的Wordpress並遇到了以下問題:進入單個產品頁面時,存在Next-Prev按鈕,其中顯示有關下一個上一產品的一些信息-名稱,上載日期,圖片。 現在我也想增加價格,但是StackOverflow中找不到的東西對我有用。

這是我的代碼,想要添加價格而不是日期。

如果需要可以提供theme-shortcodes.php文件的代碼。

先感謝您。

    if( is_object( $post ) ){
        // move this DOM element with JS
        $output .= '<a class="fixed-nav fixed-nav-'. $next_prev .' format-'. get_post_format( $post ) .'" href="'. get_permalink( $post ) .'">';

            $output .= '<span class="arrow"><i class="'. $icon .'"></i></span>';

            $output .= '<div class="photo">';
                $output .= get_the_post_thumbnail( $post->ID, 'blog-navi' );
            $output .= '</div>';

            $output .= '<div class="desc">';
                $output .= '<h6>'. get_the_title( $post ) .'</h6>';
                $output .= '<span class="date"><i class="icon-clock"></i>'. get_the_date(get_option('date_format'), $post->ID) .'</span>';
            $output .= '</div>';

        $output .= '</a>';
    }

您可以使用以下功能創建產品對象:

$product = wc_get_product( $post->ID );

之后,您將可以訪問所有產品的數據。 您可以在此處找到所有可用的方法,但您需要的是:

$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$price = $product->get_price();

並回顯價格變量,您要顯示哪個價格。

最好的辦法是使用專用的get_price_html()方法WC_Product對象實例獲取有效的格式化產品價格

我也重新審視了您的代碼:

if( is_a( $post, 'WP_Post' ) && 'product' === get_post_type( $post ) ){
    global $product;

    if( ! is_a( $product, 'WP_Product' ) ){
        $product = wc_get_product( $post->ID );
    }

    // move this DOM element with JS
    $output .= '<a class="fixed-nav fixed-nav-'. $next_prev .' format-'. get_post_format( $post ) .'" href="'. get_permalink( $post ) .'">
        <span class="arrow"><i class="'. $icon .'"></i></span>
        <div class="photo">' . get_the_post_thumbnail( $post->ID, 'blog-navi' ). '</div>
        <div class="desc">
            <h6>'. get_the_title( $post ) .'</h6>
            <span class="price">'. $product->get_price_html() .'</span>
        </div>  
    </a>';
}

經過測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM