首页 > 解决方案 > 将小计添加到结帐按钮

问题描述

我正在尝试通过以下代码将小计添加到结帐按钮

add_filter( 'gettext', function( $translated_text ) {

        if ( 'Checkout' === $translated_text ) {
        $subtotal = WC()->cart->get_subtotal();
         $translated_text = "EXPRESS " . $translated_text . " -$" . $subtotal;

    }
    return $translated_text;
} );

但是,我在日志中看到以下错误 Got error 'PHP message: PHP Fatal error: Uncaught Error: Call to a member function get_subtotal() on null in ....file name.....

标签: wordpresswoocommerce

解决方案


需要添加:

if ( ! isset( WC()->cart ) ) return;

完整代码:

add_filter( 'gettext', function( $translated_text ) {
        if ( 'Checkout' === $translated_text ) {
        if ( ! isset( WC()->cart ) ) return;
        $subtotal = WC()->cart->get_subtotal();
        $translated_text = "EXPRESS " . $translated_text . " -  $" . $subtotal;
    }
    return $translated_text;
} );

推荐阅读