PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我可以在没有任何WooCommerce页面的情况下使用WooCommerce功能吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我在WordPress网站上安装了WooCommerce.之后,我创建了一个与WooCommerce无关的自定义页面模板.在此模板中,我想使用WooCommerce wc_price()函数,但是它不起作用.这是我尝试过的:

global $woocommerce;
$woocommerce->wc_price(12);
--
WC()->wc_price(12);

但这两种都不起作用.那么,如何在自定义模板中使用此功能(如果可能)?

解决方法:

您可以简单地使用不带WC对象的函数.下面是该函数的定义.因此您可以简单地使用wc_price(12)

function wc_price( $price, $args = array() ) {
    $args = apply_filters(
        'wc_price_args', wp_parse_args(
            $args, array(
                'ex_tax_label'       => false,
                'currency'           => '',
                'decimal_separator'  => wc_get_price_decimal_separator(),
                'thousand_separator' => wc_get_price_thousand_separator(),
                'decimals'           => wc_get_price_decimals(),
                'price_format'       => get_woocommerce_price_format(),
            )
        )
    );

    $unformatted_price = $price;
    $negative          = $price < 0;
    $price             = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
    $price             = apply_filters( 'formatted_woocommerce_price', number_format( $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] ), $price, $args['decimals'], $args['decimal_separator'], $args['thousand_separator'] );

    if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $args['decimals'] > 0 ) {
        $price = wc_trim_zeros( $price );
    }

    $formatted_price = ( $negative ? '-' : '' ) . sprintf( $args['price_format'], '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $args['currency'] ) . '</span>', $price );
    $return          = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';

    if ( $args['ex_tax_label'] && wc_tax_enabled() ) {
        $return .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
    }


    return apply_filters( 'wc_price', $return, $price, $args, $unformatted_price );
}

大佬总结

以上是大佬教程为你收集整理的我可以在没有任何WooCommerce页面的情况下使用WooCommerce功能吗?全部内容,希望文章能够帮你解决我可以在没有任何WooCommerce页面的情况下使用WooCommerce功能吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: