Woocommerce添加到购物车重定向到父类别

时间:2015-08-05 16:26:29

标签: php wordpress redirect woocommerce

我已经尝试了几天,我刚刚找到了这段代码:

//This is to redirect add to cart button to restaurant page add_filter
('add_to_cart_redirect', 'redirect_to_previousCat');
function redirect_to_previousCat( $url ) {if ( isset( $_POST['add-to-cart'] ) )
{  
    $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['add-to-cart'] );
    $terms = get_the_terms( $product_id, 'product_cat' );
    foreach($terms as $term) {
        $product_cat_slug = $term->slug;      
        break; 
    }    

    if( $product_cat_slug ){
        $url = user_trailingslashit(get_permalink() . '/restaurant/' . $product_cat_slug );    
    }  
    }
return $url;
}

我的目标是将添加到购物车按钮重定向到其

  

/餐厅/父类别的蛞蝓

但代码所做的是将其重定向到/ restaurant / product-category /

我真的需要一些帮助。

1 个答案:

答案 0 :(得分:0)

我相信你错过了get_term_link()功能。

因此,您可以将代码修改为:

if( $product_cat_slug ){
   $url = get_term_link( $product_cat_slug, 'product_cat' );    
}