【问题标题】:Remove attributes from product links in cart (Woocommerce - get_permalink)从购物车中的产品链接中删除属性(Woocommerce - get_permalink)
【发布时间】:2015-01-20 12:32:46
【问题描述】:

在购物车页面中的 woocommerce 产品上呼应 the_permalink 将创建一个链接,其中包含链接中包含的属性选择,如下所示:

http://ourdemo.com/product/product-test-2/?attribute_pa_frame=polished-chrome

我们想从链接中删除这些属性(出于各种原因),但是似乎 the_permalink 在产品 ID 上运行时会自动返回它们。

查看文档我似乎找不到不返回属性的参数?有没有其他方法可以在没有任何参数的情况下获取基本永久链接?

谢谢

【问题讨论】:

    标签: wordpress attributes woocommerce permalinks cart


    【解决方案1】:

    问题在于$_product->get_permalink($cart_item) 会在相关产品是变体时自动将属性添加到永久链接。

    我不明白您为什么要摆脱这种行为,但可以通过将 $_product->get_permalink() 方法切换到 WordPress 的默认 get_permalink() 函数来实现。对于非变体产品,无论如何,该方法只是一个“包装器”。

    如果你没有显示缩略图,那么你可以通过过滤器切换标题链接:

    function so_remove_attributes_from_permalink( $name, $cart_item, $cart_item_key ){
    
        $_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        if ( ! $_product->is_visible() ){
            $name = sprintf( '<a href="%s">%s</a>', get_permalink( $cart_item['product_id'] ), $_product->get_title(), $cart_item, $cart_item_key );
        }
    
        return $name;
    }
    add_filter( 'woocommerce_cart_item_name', 'so_remove_attributes_from_permalink', 10, 3 );
    

    如果您在购物车中显示缩略图,那么我认为您需要覆盖 cart/cart.php 模板并修改引用

    $_product-&gt;get_permalink( $cart_item )

    get_permalink( $cart_item['product_id'] )

    【讨论】:

    • 嘿@helgatheviking .. 我已经阅读了您网站上的上一篇教程,然后按照您的 woocommerce 回答 :) 另一个很好的答案!
    • @Rohil_PHPBeginner 谢谢!我一直想在我的网站上写更多,但我不知道该写什么。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2020-02-17
    相关资源
    最近更新 更多