在Woocommerce中更改删除购物车商品网址

时间:2018-11-23 18:50:20

标签: php wordpress url woocommerce cart

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

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

感谢您的建议

1 个答案:

答案 0 :(得分:2)

  

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…