【问题标题】:Woocommerce: Remove item from cart hookWoocommerce:从购物车挂钩中删除商品
【发布时间】:2021-07-26 14:44:45
【问题描述】:

我有以下钩子工作正常,它确实在结帐页面中添加了删除图标以允许从购物车中删除项目。但是,它确实在购物车页面中隐藏了产品名称。如何使其仅在结帐页面而不在购物车中起作用,或者如何阻止此挂钩在购物车页面中隐藏产品名称?

add_filter('woocommerce_cart_item_name', 'njengah_filter_wc_cart_item_remove_link', 10, 3);

function njengah_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key)
{
if (is_checkout()) {
        $product_name .= apply_filters('woocommerce_cart_item_remove_link', sprintf(
        '<a href="%s" rel="nofollow" class="remove" style="float:left;">&times;</a>',
        esc_url(wc_get_cart_remove_url($cart_item_key)),
        __('Remove this item', 'woocommerce'),
        esc_attr($cart_item['product_id']),
        esc_attr($cart_item['data']->get_sku())
        ), $cart_item_key);

        return $product_name;
}
}

【问题讨论】:

    标签: wordpress woocommerce filter hook


    【解决方案1】:

    使用过滤器时必须返回。

    add_filter('woocommerce_cart_item_name', 'njengah_filter_wc_cart_item_remove_link', 10, 3);
    
    function njengah_filter_wc_cart_item_remove_link($product_name, $cart_item, $cart_item_key){
        if (is_checkout()) {
            $product_name .= apply_filters('woocommerce_cart_item_remove_link', sprintf(
            '<a href="%s" rel="nofollow" class="remove" style="float:left;">&times;</a>',
            esc_url(wc_get_cart_remove_url($cart_item_key)),
            __('Remove this item', 'woocommerce'),
            esc_attr($cart_item['product_id']),
            esc_attr($cart_item['data']->get_sku())
            ), $cart_item_key);
        }
        return $product_name;
    }
    

    【讨论】:

    • 哇!小错误,感谢您的快速响应。
    • 欢迎...很高兴为您提供帮助。
    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 1970-01-01
    相关资源
    最近更新 更多