在functions.php中WooCommerce'if(is_cart())'不起作用?

时间:2016-05-08 08:54:02

标签: php wordpress woocommerce checkout cart

我已经在我的functions.php中添加了以下代码,因此我可以更改WooCommerce标准'添加到购物车通知'。

通知确实发生了变化,但if ( is_cart() )似乎无效。它在购物车页面上输出FALSE。

我必须监督某事......?

add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );

function yw_add_to_cart_message($message, $product_id = null) {

    $titles[] = get_the_title( $product_id );

    $titles = array_filter( $titles );

    if ( is_cart() ) {
        $cart_link  = '<div class="uk-width-medium-1-5 uk-text-right"><a href="' . WC_Cart::get_checkout_url() . '"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</a></div>';
    } else {
        $cart_link  = '<div class="uk-width-medium-1-5 uk-text-right"><a href="' . WC_Cart::get_cart_url() . '"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</a></div>';
    }

    $added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div>' . $cart_link . '</div>';

    $message = sprintf( '%s', $added_text );

    return $message;
}

1 个答案:

答案 0 :(得分:1)

您必须使用get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes'代替is_cart()

add_filter ( 'wc_add_to_cart_message', 'yw_add_to_cart_message', 10, 2 );

function yw_add_to_cart_message($message, $product_id = null) {

    $titles[] = get_the_title( $product_id );

    $titles = array_filter( $titles );

    if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
        $cart_link  = '<a href="' . WC_Cart::get_checkout_url() . '"><i class="uk-icon-check-square-o"></i> ' . __( 'Checkout', 'woocommerce' ) . '</a>';
    } else {
        $cart_link  = '<a href="' . WC_Cart::get_cart_url() . '"><i class="uk-icon-shopping-cart"></i> ' . __( 'View Cart', 'woocommerce' ) . '</a>';
    }

    $added_text = '<div class="uk-grid uk-grid-collapse" data-uk-grid-margin><div class="uk-width-medium-4-5">' . sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ) . '</div><div class="uk-width-medium-1-5 uk-text-right">' . $cart_link . '</div></div>';

    $message = sprintf( '%s', $added_text );

    return $message;
}

相关主题:HereHere