【问题标题】:Wordpress - Woocommerece remove "Added to Cart" messageWordpress - Woocommerce 删除“已添加到购物车”消息
【发布时间】:2012-12-24 18:04:40
【问题描述】:

我希望在将商品添加到购物车后删除“产品已成功添加到购物车”的措辞和区域。我只是想要什么都没有,没有信息,也没有信息的空间。

这里是网站:http://www.tinytreasurehunts.com 代码在 woocommerece-functions.php 中

有什么想法吗?

【问题讨论】:

  • 请记住,当您自动更新 WooCommerce 时,它​​将覆盖您的所有更改。尽可能使用它的模板系统,以免在事后意识到它的麻烦。

标签: php css wordpress woocommerce


【解决方案1】:

要在 PHP 级别解决此问题,请将以下模板文件(和结构)添加到您的主题中:
/wp-content/themes/YOUR-THEME/woocommerce/shop/messages.php:

<?php
/**
 * Show messages
 *
 * @author      brasofilo
 * @package     WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

if ( ! $messages ) return;

foreach ( $messages as $message ) : 
    // The message does not contain the "add to cart" string, so print the message
    // http://stackoverflow.com/q/4366730/1287812
    if ( strpos( $message, 'added to your cart' ) === false ) :
        ?>
            <div class="woocommerce-message"><?php echo wp_kses_post( $message ); ?></div>
        <?php 
    endif;
endforeach;

见:Template Structure + overriding templates via a theme

【讨论】:

  • 请注意,模板结构现在不同了,因此您需要匹配它 - 现在是 /wp-content/themes/YOUR-THEME/woocommerce/notices/success.php
【解决方案2】:

使用其中一种:

旧版本

$woocommerce->clear_messages(); 

2.3 版

wc_clear_notices();

【讨论】:

  • 这个函数应该在哪里调用?
【解决方案3】:

只需使用简单的 CSS:

.single-product .woocommerce-message {
    display: none !important;
}

或者,如果您熟悉functions.php,这是一个更好的解决方案:

add_filter( 'wc_add_to_cart_message_html', '__return_null' );

来源是here,以及编辑此类消息的方法,以防您希望打印自己的版本

【讨论】:

    【解决方案4】:

    使用 CSS 并将 ID 或关联类的显示设置为无。

     
    .page-id-522 .woocommerce_message {
         display: none;
    }
    

    这特定于页面 id 522。确保这不会同时隐藏其他有用的消息,例如信用卡拒绝等。

    【讨论】:

    • 我尝试添加它,但不认为它在正确的位置。你在哪里为插件添加 CSS?
    • 您需要添加它并确保它低于这些元素的其他 CSS 定义。看一下 CSS 的优先顺序。另外我相信在 Woo 主题配置中可以添加自定义 CSS。在那里试试。
    • 我想值得注意的是,522 将是 Shop 页面的 ID。
    【解决方案5】:

    将此代码添加到您的主题functions.php 文件中。它只会删除该消息。它应该只在可能发生的页面上触发。

    function remove_added_to_cart_notice()
    {
        $notices = WC()->session->get('wc_notices', array());
    
        foreach( $notices['success'] as $key => &$notice){
            if( strpos( $notice, 'has been added' ) !== false){
                $added_to_cart_key = $key;
                break;
            }
        }
        unset( $notices['success'][$added_to_cart_key] );
    
        WC()->session->set('wc_notices', $notices);
    }
    add_action('woocommerce_before_single_product','remove_added_to_cart_notice',1);
    add_action('woocommerce_shortcode_before_product_cat_loop','remove_added_to_cart_notice',1);
    add_action('woocommerce_before_shop_loop','remove_added_to_cart_notice',1);
    

    我已将这个答案从我自己的答案中粘贴到Remove/Hide Woocommerce Added to Cart Message but Keep/Display Coupon Applied Message

    【讨论】:

      【解决方案6】:

      WooCommerce 2.1.6 版更新

      模板位于新目录和文件中。与上面相同的代码和解决方案。

      /wp-content/plugins/woocommerce/templates/notices/success.php

      【讨论】:

        【解决方案7】:

        在主题文件末尾或子主题中使用 .post .woocommerce_message{display:none;}。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-16
          • 1970-01-01
          • 2014-11-10
          • 2016-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多