2017-01-20 6 views
1

すべての商品を1つのカスタムページにリダイレクトしようとしていますが、チェックアウトに行く3つの商品を除いて(この部分は動作します)。条件付きカートに入れる商品IDに基づいてリダイレクトする

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ファイルに行くか:私は少しあなたのコードを変更する必要があり下回ります。

このコードはテスト済みであり、動作します。

関連する問題