2017-05-27 187 views
0

我希望用戶在點擊「添加到購物車」按鈕時被定向到某個URL。目前我有這樣的:自定義添加到購物車在Woocommerce中特定產品的按鈕URL?

/** 
* 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'); 

但是,這重定向所有添加到購物車按鈕,我怎樣才能使產品特定的URL?謝謝

回答

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 

} 
0

您需要使用一些條件邏輯來修改返回的url,當滿足這些條件時。當添加到購物車的產品的ID爲999時,以下內容將更改網址。

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; 

} 
相關問題