【问题标题】:Change remove cart item url in Woocommerce在 Woocommerce 中更改删除购物车项目 url
【发布时间】:2018-11-23 18:50:19
【问题描述】:

我的简单问题是如何使这段代码正常工作

esc_url( WC()->cart->get_cart_url->get_remove_url( $cart_item_key ) ),

上面我已经尝试过,当前代码在下面

 esc_url( $woocommerce->cart->get_remove_url( $cart_item_key ) ),

所以改为我当前的网址:example.com/?removed_item=1

应该类似于example.com/cart/?removed_item=1

感谢建议

【问题讨论】:

    标签: php wordpress url woocommerce cart


    【解决方案1】:

    WC_Cart get_remove_url() 方法已弃用并由wc_get_cart_remove_url() 函数替换。

    可以这样使用:

    // Loop through cart items
    foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        // Get the remove Url for the current cart item
        $remove_url = wc_get_cart_remove_url( $cart_item_key );
    }
    

    这会给你一个类似的网址:https://www.example.com/cart/?removed_item=1…


    现在要对这个 Url 进行更改,您可以使用 woocommerce_get_remove_url 专用过滤器挂钩,如下例所示:

    add_filter( 'woocommerce_get_remove_url', 'custom_item_remove_url', 10, 1 );
    function custom_item_remove_url( $remove_url ) {
        $cart_page_url   = wc_get_page_permalink( 'cart' );
        $replacement_url = wc_get_page_permalink( 'shop' ); // Shop page
    
        // Change URL to shop page + remove Url query vars
        $remove_url = str_replace($cart_page_url, $replacement_url, $remove_url);
    
        return $remove_url;
    }
    

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

    这会给你一个类似的网址:https://www.example.com/shop/?removed_item=1…

    【讨论】:

    • 而不是商店,我想重定向到购物车,我已经更改了您的代码,但它不起作用
    • 在我的迷你购物车上,当我删除购物车项目时,它没有重定向到查看购物车页面这是您的代码的问题,URL 显示在底部,但它没有重定向到查看购物车页面
    • @R.K.Bhardwaj 对于 minicart 是不可能的,因为它没有重定向并且由 ajax 管理。
    • 不幸的是,此代码不起作用。 “此网站出现严重错误”
    • add_filter('woocommerce_get_remove_url', 'custom_item_remove_url', 10, 1);功能 custom_item_remove_url( $remove_url ) { $cart_page_url = wc_get_page_permalink( 'cart' ); $replacement_url = wc_get_page_permalink('结帐'); // 商店页面 // 将 URL 更改为商店页面 + 删除 URL 查询变量 $remove_url = str_replace($cart_page_url, $replacement_url, $remove_url);返回 $remove_url; }
    猜你喜欢
    • 1970-01-01
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    相关资源
    最近更新 更多