在Woocommerce中为特定产品自定义添加到购物车按钮URL?

时间:2017-05-27 14:26:29

标签: wordpress woocommerce

我希望用户在点击“添加到购物车”按钮时定向到某个网址。目前我有这个:

/**
 * Set a custom add to cart URL to redirect to
 * @return string
 */
  function custom_add_to_cart_redirect() { 
return 'http://www.yourdomain.com/your-page/'; 
}
add_filter( 'woocommerce_add_to_cart_redirect', 
'custom_add_to_cart_redirect' );

但是,这会重定向所有添加到购物车按钮,如何制作特定于产品的网址?感谢

2 个答案:

答案 0 :(得分:2)

add_filter( 'woocommerce_add_to_cart_redirect', 'productbase_redirect_on_add_to_cart' );
function productbase_redirect_on_add_to_cart() {

    //Get product ID
    if ( isset( $_POST['add-to-cart'] ) ) {
        $product_id = (int) $_POST['add-to-cart'] ;             
    }
    // set redirect with product id here

}

答案 1 :(得分:1)

当满足这些特定条件时,您需要使用一些条件逻辑来更改返回的URL。当添加到购物车的产品的ID为999时,以下内容将更改URL。

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );

function custom_add_to_cart_redirect( $url ) {

    // The product ID you want a specific redirect for
    $matched_id = 999; 

    // If product ID matches, modify redirect
    if ( isset( $_POST['add-to-cart'] ) ) && $matched_id = (int) $_POST['add-to-cart'] ) {            
      $url = 'http://www.yourdomain.com/your-page/'; 
    }
    // always return a value
    return $url;

}