简体   繁体   English

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

[英]WooCommerce 'if ( is_cart() )' in functions.php not working?

I've added the code below within my functions.php so I can change the WooCommerce standard 'Add to cart notice'. 我已经在我的functions.php中添加了以下代码,因此可以更改WooCommerce标准的“添加到购物车通知”。

The notice does change but the if ( is_cart() ) does not seem to work. 该通知确实发生了变化,但是if ( is_cart() )似乎不起作用。 It outputs FALSE on the cart page. 它在购物车页面上输出FALSE。

I must have overseen something..? 我一定有监督的事..?

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;
}

You have to use get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' instead of is_cart() : 您必须使用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;
}

Related threads: Here and Here 相关主题: 这里这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM