2017-01-20 9 views
1

Я пытаюсь перенаправить все продукты на одну пользовательскую страницу ЗА ИСКЛЮЧЕНИЕМ 3 продуктов, которые идут в Checkout (эта часть работает).Условно Добавить в корзину перенаправить на основе идентификаторов продуктов

function my_custom_add_to_cart_redirect($url) { 

     if (! isset($_REQUEST['add-to-cart']) || ! is_numeric($_REQUEST['add-to-cart'])) { 
      $url = get_permalink(16); // URL page ID to redirect for all pages but below mentioned   
      return $url; 
     } 

     $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); 

     // Only redirect the product IDs in the array to the checkout 
     if (in_array($product_id, array(999, 997, 872))) { 
      $url = WC()->cart->get_checkout_url(); 
     } 

     return $url; 
    } 
    add_filter('woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect'); 

ответ

1

Чтобы сделать ваш код работающим, как ожидалось, очень просто. Ниже я изменить немного код:

add_filter('woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect', 10, 1); 
function my_custom_add_to_cart_redirect($url) { 

    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); 

    // Only redirect the product IDs in the array to the checkout 
    if (in_array($product_id, array(999, 997, 872))) { 
     // This is more correct to get the checkout URL 
     $url = get_permalink(get_option('woocommerce_checkout_page_id')); 
    } else { 
     // All other products that are not in your array will be redirected to this URL 
     $url = get_permalink(16); // URL page ID to redirect for all pages but below mentioned 
    } 
    return $url; 
} 

Этот код идет в function.php файле Вашего активного ребенка темы (или темы), или также в любой файл плагина.

Этот код проверен и работает.