首页 > 解决方案 > 在 Woocommerce 3 中自定义购物车项目删除通知

问题描述

我正在使用 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;
}

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

标签: wordpresswoocommercecartproductnotice

解决方案


更新

唯一可用的钩子是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;
}

这是您将得到的:

在此处输入图像描述


推荐阅读