【问题标题】:Customize cart item removed notice in Woocommerce 3在 Woocommerce 3 中自定义购物车项目删除通知
【发布时间】:2019-01-17 09:43:49
【问题描述】:

我使用的是 WooCommerce 3.4.4 版本,我正在尝试编辑通知消息,当您按“X”按钮从购物车页面中删除产品时。

目前通知为"<product name>" removed. Undo? 我想删除引号并将消息更改为 Product name has been removed. [Undo button]

我已经设法删除了产品名称中的引号

add_filter( 'woocommerce_cart_item_removed_title', 'removed_from_cart', 10, 2);
function removed_from_cart( $message, $product_data ) {
    $product = get_the_title($product_data['product_id']);
    $message = $product;
    return $message;
}

但是我对如何进行其余的编辑有点困惑。任何帮助表示赞赏。

【问题讨论】:

    标签: wordpress woocommerce cart product notice


    【解决方案1】:

    更新

    唯一可用的钩子是您已经在使用的woocommerce_cart_item_removed_title。并在引号之间显示产品名称。您还可以使用gettex 过滤器挂钩来删除“撤消”文本后的?

    add_filter( 'woocommerce_cart_item_removed_title', 'removed_from_cart_title', 12, 2);
    function removed_from_cart_title( $message, $cart_item ) {
        $product = wc_get_product( $cart_item['product_id'] );
    
        if( $product )
            $message = sprintf( __('Product %s has been'), $product->get_name() );
    
        return $message;
    }
    
    add_filter('gettext', 'cart_undo_translation', 35, 3 );
    function cart_undo_translation( $translation, $text, $domain ) {
    
        if( $text === 'Undo?' ) {
            $translation =  __( 'Undo', $domain );
        }
        return $translation;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    但是你不能改变或添加button标签类<a>html标签……
    改为使用现有的restore-item标签类为其添加一些自定义 CSS 样式。

    下面是一些 CSS 样式示例,您可以将其添加到活动子主题的 styles.css 文件中:

    .woocommerce-message .restore-item, {
        float: right;
        padding: 0 0 0 1em;
        background: 0 0;
        color: #fff;
        box-shadow: none;
        line-height: 1.618;
        border-width: 0 0 0 1px;
        border-left-style: solid;
        border-left-color: rgba(255,255,255,.25)!important;
        border-radius: 0;
    }
    .woocommerce-message .restore-item:hover {
        background: 0 0;
        color: #fff;
        opacity: .8;
    }
    

    这是你将得到的:

    【讨论】:

    • 有没有办法把 Undo 后面的问号改成句号或者把整个 Undo 包裹在一个带有按钮类的 标签中?
    • @user2515606 新代码更新。它完全符合您的预期。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多