首页 文章

从购物车中的产品链接中删除属性(Woocommerce - get_permalink)

提问于
浏览
1

在购物车页面中回应woocommerce产品上的the_permalink将创建链接中包含的属性选择链接,例如:

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

我们希望将这些属性从链接中取出(出于各种原因),但是看起来似乎是_permalink,当在产品ID上运行时会自动返回它们 .

看文档我似乎无法找到一个不返回属性的参数?有没有其他方法来获得没有任何参数的基本永久链接?

谢谢

1 回答

  • 1

    麻烦的是,只要有问题的产品是变体, $_product->get_permalink($cart_item) 就会自动将属性添加到固定链接 .

    我不希望摆脱这种行为,但可以通过将 $_product->get_permalink() 方法切换到WordPress的默认 get_permalink() 函数来实现 . 对于非变异产品,无论如何,该方法只是一个"wrapper" .

    如果您没有显示缩略图,则可以通过过滤器切换 Headers 链接:

    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->get_permalink( $cart_item )

    get_permalink( $cart_item['product_id'] )

相关问题